"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "buffer.c" between
mtools-4.0.35.tar.bz2 and mtools-4.0.36.tar.bz2

About: Mtools is a collection of utilities to access MS-DOS disks from Unix without mounting them.

buffer.c  (mtools-4.0.35.tar.bz2):buffer.c  (mtools-4.0.36.tar.bz2)
skipping to change at line 26 skipping to change at line 26
* *
* Buffer read/write module * Buffer read/write module
*/ */
#include "sysincludes.h" #include "sysincludes.h"
#include "msdos.h" #include "msdos.h"
#include "mtools.h" #include "mtools.h"
#include "buffer.h" #include "buffer.h"
typedef struct Buffer_t { typedef struct Buffer_t {
Class_t *Class; struct Stream_t head;
int refs;
Stream_t *Next;
Stream_t *Buffer;
size_t size; /* size of read/write buffer */ size_t size; /* size of read/write buffer */
int dirty; /* is the buffer dirty? */ int dirty; /* is the buffer dirty? */
size_t sectorSize; /* sector size: all operations happen size_t sectorSize; /* sector size: all operations happen
* in multiples of this */ * in multiples of this */
size_t cylinderSize; /* cylinder size: preferred alignment, size_t cylinderSize; /* cylinder size: preferred alignment,
* but for efficiency, less data may be read */ * but for efficiency, less data may be read */
int ever_dirty; /* was the buffer ever dirty? */ int ever_dirty; /* was the buffer ever dirty? */
size_t dirty_pos; size_t dirty_pos;
skipping to change at line 72 skipping to change at line 69
/* /*
* Flush a dirty buffer to disk. Resets Buffer->dirty to zero. * Flush a dirty buffer to disk. Resets Buffer->dirty to zero.
* All errors are fatal. * All errors are fatal.
*/ */
static int _buf_flush(Buffer_t *Buffer) static int _buf_flush(Buffer_t *Buffer)
{ {
ssize_t ret; ssize_t ret;
if (!Buffer->Next || !Buffer->dirty) #ifdef HAVE_ASSERT_H
assert(Buffer->head.Next != NULL);
#endif
if (!Buffer->dirty)
return 0; return 0;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "write %08x -- %02x %08x %08x\n", fprintf(stderr, "write %08x -- %02x %08x %08x\n",
Buffer, Buffer,
(unsigned char) Buffer->buf[0], (unsigned char) Buffer->buf[0],
Buffer->current + Buffer->dirty_pos, Buffer->current + Buffer->dirty_pos,
Buffer->dirty_end - Buffer->dirty_pos); Buffer->dirty_end - Buffer->dirty_pos);
#endif #endif
ret = force_write(Buffer->Next, ret = force_pwrite(Buffer->head.Next,
Buffer->buf + Buffer->dirty_pos, Buffer->buf + Buffer->dirty_pos,
Buffer->current + (mt_off_t) Buffer->dirty_pos, Buffer->current + (mt_off_t) Buffer->dirty_pos,
Buffer->dirty_end - Buffer->dirty_pos); Buffer->dirty_end - Buffer->dirty_pos);
if(ret != (signed int) (Buffer->dirty_end - Buffer->dirty_pos)) { if(ret < 0) {
if(ret < 0) perror("buffer_flush: write");
perror("buffer_flush: write"); return -1;
else }
fprintf(stderr,"buffer_flush: short write\n");
if((size_t) ret != Buffer->dirty_end - Buffer->dirty_pos) {
fprintf(stderr,"buffer_flush: short write\n");
return -1; return -1;
} }
Buffer->dirty = 0; Buffer->dirty = 0;
Buffer->dirty_end = 0; Buffer->dirty_end = 0;
Buffer->dirty_pos = 0; Buffer->dirty_pos = 0;
return 0; return 0;
} }
static int invalidate_buffer(Buffer_t *Buffer, mt_off_t start) static int invalidate_buffer(Buffer_t *Buffer, mt_off_t start)
{ {
skipping to change at line 149 skipping to change at line 152
return APPEND; return APPEND;
} else { } else {
if(invalidate_buffer(This, start) < 0) if(invalidate_buffer(This, start) < 0)
return ERROR; return ERROR;
maximize(*len, This->cylinderSize - OFFSET); maximize(*len, This->cylinderSize - OFFSET);
maximize(*len, pos_to_next_full_cyl(This, This->current)); maximize(*len, pos_to_next_full_cyl(This, This->current));
return OUTSIDE; return OUTSIDE;
} }
} }
static ssize_t buf_read(Stream_t *Stream, char *buf, mt_off_t start, size_t len) static ssize_t buf_pread(Stream_t *Stream, char *buf,
mt_off_t start, size_t len)
{ {
size_t length; size_t length;
size_t offset; size_t offset;
char *disk_ptr; char *disk_ptr;
ssize_t ret; ssize_t ret;
DeclareThis(Buffer_t); DeclareThis(Buffer_t);
if(!len) if(!len)
return 0; return 0;
/*fprintf(stderr, "buf read %x %x %x\n", Stream, start, len);*/ /*fprintf(stderr, "buf read %x %x %x\n", Stream, start, len);*/
switch(isInBuffer(This, start, &len)) { switch(isInBuffer(This, start, &len)) {
case OUTSIDE: case OUTSIDE:
case APPEND: case APPEND:
/* always load until the end of the cylinder */ /* always load until the end of the cylinder */
length = pos_to_next_full_cyl(This, cur_end(This)); length = pos_to_next_full_cyl(This, cur_end(This));
maximize(length, This->size - This->cur_size); maximize(length, This->size - This->cur_size);
/* read it! */ /* read it! */
ret=READS(This->Next, ret=PREADS(This->head.Next,
This->buf + This->cur_size, This->buf + This->cur_size,
This->current + (mt_off_t) This->cur_size, This->current + (mt_off_t) This->cur_size,
length); length);
if ( ret < 0 ) if ( ret < 0 )
return ret; return ret;
This->cur_size += (size_t) ret; This->cur_size += (size_t) ret;
if (This->current+(mt_off_t)This->cur_size < start) { if (This->current+(mt_off_t)This->cur_size < start) {
fprintf(stderr, "Short buffer fill\n"); fprintf(stderr, "Short buffer fill\n");
exit(1); exit(1);
} }
break; break;
case INSIDE: case INSIDE:
/* nothing to do */ /* nothing to do */
skipping to change at line 195 skipping to change at line 199
return -1; return -1;
} }
offset = OFFSET; offset = OFFSET;
disk_ptr = This->buf + offset; disk_ptr = This->buf + offset;
maximize(len, This->cur_size - offset); maximize(len, This->cur_size - offset);
memcpy(buf, disk_ptr, len); memcpy(buf, disk_ptr, len);
return (ssize_t) len; return (ssize_t) len;
} }
static ssize_t buf_write(Stream_t *Stream, char *buf, static ssize_t buf_pwrite(Stream_t *Stream, char *buf,
mt_off_t start, size_t len) mt_off_t start, size_t len)
{ {
char *disk_ptr; char *disk_ptr;
DeclareThis(Buffer_t); DeclareThis(Buffer_t);
size_t offset=0; size_t offset=0;
if(!len) if(!len)
return 0; return 0;
This->ever_dirty = 1; This->ever_dirty = 1;
skipping to change at line 230 skipping to change at line 234
#endif #endif
if(start % (mt_off_t) This->cylinderSize || if(start % (mt_off_t) This->cylinderSize ||
len < This->sectorSize) { len < This->sectorSize) {
size_t readSize; size_t readSize;
ssize_t ret; ssize_t ret;
size_t bytes_read; size_t bytes_read;
readSize = This->cylinderSize - readSize = This->cylinderSize -
(size_t)(This->current % (mt_off_t) This- >cylinderSize); (size_t)(This->current % (mt_off_t) This- >cylinderSize);
ret=READS(This->Next, This->buf, (mt_off_t)This-> ret=PREADS(This->head.Next, This->buf,
current, readSize); (mt_off_t)This->current, readSize);
/* read it! */ /* read it! */
if ( ret < 0 ) if ( ret < 0 )
return ret; return ret;
bytes_read = (size_t) ret; bytes_read = (size_t) ret;
if(bytes_read % This->sectorSize) { if(bytes_read % This->sectorSize) {
fprintf(stderr, "Weird: read size (%zd) not a m ultiple of sector size (%d)\n", bytes_read, (int) This->sectorSize); fprintf(stderr, "Weird: read size (%zd) not a m ultiple of sector size (%d)\n", bytes_read, (int) This->sectorSize);
bytes_read -= bytes_read % This->sectorSize; bytes_read -= bytes_read % This->sectorSize;
if(bytes_read == 0) { if(bytes_read == 0) {
fprintf(stderr, "Nothing left\n"); fprintf(stderr, "Nothing left\n");
exit(1); exit(1);
skipping to change at line 261 skipping to change at line 266
} }
/* FALL THROUGH */ /* FALL THROUGH */
case APPEND: case APPEND:
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "append\n"); fprintf(stderr, "append\n");
#endif #endif
len = ROUND_DOWN(len, This->sectorSize); len = ROUND_DOWN(len, This->sectorSize);
offset = OFFSET; offset = OFFSET;
maximize(len, This->size - offset); maximize(len, This->size - offset);
This->cur_size += len; This->cur_size += len;
if(This->Next->Class->pre_allocate) if(This->head.Next->Class->pre_allocate)
PRE_ALLOCATE(This->Next, cur_end(This)); PRE_ALLOCATE(This->head.Next, cur_end(This));
break; break;
case INSIDE: case INSIDE:
/* nothing to do */ /* nothing to do */
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "inside\n"); fprintf(stderr, "inside\n");
#endif #endif
offset = OFFSET; offset = OFFSET;
maximize(len, This->cur_size - offset); maximize(len, This->cur_size - offset);
break; break;
case ERROR: case ERROR:
skipping to change at line 339 skipping to change at line 344
{ {
DeclareThis(Buffer_t); DeclareThis(Buffer_t);
if(This->buf) if(This->buf)
free(This->buf); free(This->buf);
This->buf = 0; This->buf = 0;
return 0; return 0;
} }
static Class_t BufferClass = { static Class_t BufferClass = {
buf_read, 0,
buf_write, 0,
buf_pread,
buf_pwrite,
buf_flush, buf_flush,
buf_free, buf_free,
0, /* set_geom */ 0, /* set_geom */
get_data_pass_through, /* get_data */ get_data_pass_through, /* get_data */
0, /* pre-allocate */ 0, /* pre-allocate */
get_dosConvert_pass_through, /* dos convert */ get_dosConvert_pass_through, /* dos convert */
0, /* discard */ 0, /* discard */
}; };
Stream_t *buf_init(Stream_t *Next, size_t size, Stream_t *buf_init(Stream_t *Next, size_t size,
size_t cylinderSize, size_t cylinderSize,
size_t sectorSize) size_t sectorSize)
{ {
Buffer_t *Buffer; Buffer_t *Buffer;
Stream_t *Stream;
#ifdef HAVE_ASSERT_H #ifdef HAVE_ASSERT_H
assert(size != 0); assert(size != 0);
assert(cylinderSize != 0); assert(cylinderSize != 0);
assert(sectorSize != 0); assert(sectorSize != 0);
assert(Next != NULL);
#endif #endif
if(size % cylinderSize != 0) { if(size % cylinderSize != 0) {
fprintf(stderr, "size not multiple of cylinder size\n"); fprintf(stderr, "size not multiple of cylinder size\n");
exit(1); exit(1);
} }
if(cylinderSize % sectorSize != 0) { if(cylinderSize % sectorSize != 0) {
fprintf(stderr, "cylinder size not multiple of sector size\n"); fprintf(stderr, "cylinder size not multiple of sector size\n");
exit(1); exit(1);
} }
if(Next->Buffer){ Buffer = New(Buffer_t);
Next->refs--; if(!Buffer)
Next->Buffer->refs++;
return Next->Buffer;
}
Stream = (Stream_t *) malloc (sizeof(Buffer_t));
if(!Stream)
return 0; return 0;
Buffer = (Buffer_t *) Stream; init_head(&Buffer->head, &BufferClass, Next);
Buffer->buf = malloc(size); Buffer->buf = malloc(size);
if ( !Buffer->buf){ if ( !Buffer->buf){
Free(Stream); Free(Buffer);
return 0; return 0;
} }
Buffer->size = size; Buffer->size = size;
Buffer->dirty = 0; Buffer->dirty = 0;
Buffer->cylinderSize = cylinderSize; Buffer->cylinderSize = cylinderSize;
Buffer->sectorSize = sectorSize; Buffer->sectorSize = sectorSize;
Buffer->ever_dirty = 0; Buffer->ever_dirty = 0;
Buffer->dirty_pos = 0; Buffer->dirty_pos = 0;
Buffer->dirty_end = 0; Buffer->dirty_end = 0;
Buffer->current = 0L; Buffer->current = 0L;
Buffer->cur_size = 0; /* buffer currently empty */ Buffer->cur_size = 0; /* buffer currently empty */
Buffer->Next = Next; return &Buffer->head;
Buffer->Class = &BufferClass;
Buffer->refs = 1;
Buffer->Buffer = 0;
Buffer->Next->Buffer = (Stream_t *) Buffer;
return Stream;
} }
 End of changes. 15 change blocks. 
44 lines changed or deleted 39 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)