1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* Buffer read/write module
*/
#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "buffer.h"
typedef struct Buffer_t {
Class_t *Class;
int refs;
Stream_t *Next;
Stream_t *Buffer;
size_t size; /* size of read/write buffer */
int dirty; /* is the buffer dirty? */
int grain;
int grain2;
int ever_dirty; /* was the buffer ever dirty? */
int dirty_pos;
int dirty_end;
off_t current; /* first sector in buffer */
size_t cur_size; /* the current size */
char *buf; /* disk read/write buffer */
} Buffer_t;
/*
* Flush a dirty buffer to disk. Resets Buffer->dirty to zero.
* All errors are fatal.
*/
static int _buf_flush(Buffer_t *Buffer)
{
int ret;
if (!Buffer->Next || !Buffer->dirty)
return 0;
if(Buffer->current < 0L) {
fprintf(stderr,"Should not happen\n");
return 0;
}
ret = force_write(Buffer->Next,
Buffer->buf + Buffer->dirty_pos,
Buffer->current + Buffer->dirty_pos,
Buffer->dirty_end - Buffer->dirty_pos);
if(ret != Buffer->dirty_end - Buffer->dirty_pos) {
if(ret < 0)
perror("buffer_flush: write");
else
fprintf(stderr,"buffer_flush: short write\n");
return -1;
}
Buffer->dirty = 0;
return 0;
}
static void invalidate_buffer(Buffer_t *Buffer, off_t start)
{
_buf_flush(Buffer);
Buffer->current = start - start % Buffer->grain;
Buffer->cur_size = 0;
}
static int buf_read(Stream_t *Stream, char *buf, off_t start, size_t len)
{
int length, offset;
char *disk_ptr;
int ret;
DeclareThis(Buffer_t);
if(!len)
return 0;
/* a "cache" miss, load buffer */
if (start < This->current || start >= This->current + This->cur_size) {
invalidate_buffer(This, start);
/* always load aligned */
length = This->size - (This->current % This->grain2);
/* read it! */
ret=This->Next->Class->read(This->Next,
This->buf,
This->current,
length);
if ( ret < 0 )
return ret;
This->cur_size = ret;
}
offset = start - This->current;
disk_ptr = This->buf + offset;
maximize(&len, This->cur_size - offset);
memcpy(buf, disk_ptr, len);
return len;
}
static int buf_write(Stream_t *Stream, char *buf, off_t start, size_t len)
{
char *disk_ptr;
DeclareThis(Buffer_t);
int offset, ret;
if(!len)
return 0;
This->ever_dirty = 1;
/* a cache miss... */
if (start < This->current ||
start >= This->current + This->size ||
start > This->current + This->cur_size ||
(start == This->current + This->cur_size &&
(len < This->grain || !(start % This->grain2)))) {
invalidate_buffer(This, start);
if(start % This->grain || len < This->grain) {
/* read it! */
ret=This->Next->Class->read(This->Next,
This->buf,
This->current,
This->grain);
if ( ret < 0 )
return ret;
This->cur_size = ret;
/* for dosemu. Autoextend size */
if(!This->cur_size) {
memset(This->buf, 0, This->grain);
This->cur_size = This->grain;
}
}
}
maximize(&len,This->size - (start % This->size));
offset = start - This->current;
if(len + offset > This->cur_size) {
/* enforce aligned writes */
len -= (len + offset ) % This->grain;
This->cur_size = len + offset;
}
disk_ptr = This->buf + offset;
memcpy(disk_ptr, buf, len);
if(!This->dirty || disk_ptr - This->buf < This->dirty_pos){
This->dirty_pos = disk_ptr - This->buf;
This->dirty_pos -= This->dirty_pos % This->grain;
}
if(!This->dirty || disk_ptr - This->buf + len > This->dirty_end){
This->dirty_end = disk_ptr - This->buf + len;
This->dirty_end += This->grain - 1;
This->dirty_end -= This->dirty_end % This->grain;
}
This->dirty = 1;
return len;
}
static int buf_flush(Stream_t *Stream)
{
int ret;
DeclareThis(Buffer_t);
if (!This->ever_dirty)
return 0;
ret = _buf_flush(This);
This->ever_dirty = 0;
return ret;
}
static Class_t BufferClass = {
buf_read,
buf_write,
buf_flush,
0, /* buf_free */
0, /* set_geom */
get_data_pass_through, /* get_data */
};
Stream_t *buf_init(Stream_t *Next, int size, int grain, int grain2)
{
Buffer_t *Buffer;
Stream_t *Stream;
if(Next->Buffer){
Next->refs--;
Next->Buffer->refs++;
return Next->Buffer;
}
Stream = (Stream_t *) malloc (sizeof(Buffer_t));
Buffer = (Buffer_t *) Stream;
Buffer->buf = malloc(size);
Buffer->size = size;
Buffer->grain = grain;
Buffer->grain2 = grain2;
Buffer->cur_size = 0; /* buffer currently empty */
Buffer->dirty = 0;
Buffer->ever_dirty = 0;
if ( !Buffer->buf){
Free(Stream);
return 0;
}
Buffer->Next = Next;
Buffer->Class = &BufferClass;
Buffer->refs = 1;
Buffer->Buffer = 0;
Buffer->Next->Buffer = (Stream_t *) Buffer;
return Stream;
}
|