X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=bfile%2Fmfile.c;h=714bba87879141199629dfb2eba076a0598c4505;hb=25711769a3fb5c6bf0ab3eb9634bf74ba07dc48d;hp=5f0dd22af37a9dfdf284d14d2685be3b7ebbee6a;hpb=6a294d61f599405cbf16151b3fb436e454014693;p=idzebra-moved-to-github.git diff --git a/bfile/mfile.c b/bfile/mfile.c index 5f0dd22..714bba8 100644 --- a/bfile/mfile.c +++ b/bfile/mfile.c @@ -4,7 +4,19 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: mfile.c,v $ - * Revision 1.3 1994-08-24 09:37:17 quinn + * Revision 1.7 1994-09-19 14:12:37 quinn + * dunno. + * + * Revision 1.6 1994/09/14 13:10:15 quinn + * Corrected some bugs in the init-phase + * + * Revision 1.5 1994/09/12 08:01:51 quinn + * Small + * + * Revision 1.4 1994/09/01 14:51:07 quinn + * Allowed mf_write to write beyond eof+1. + * + * Revision 1.3 1994/08/24 09:37:17 quinn * Changed reaction to read return values. * * Revision 1.2 1994/08/23 14:50:48 quinn @@ -15,6 +27,12 @@ * */ + + /* + * TODO: The size estimates in init may not be accurate due to + * only partially written final blocks. + */ + #include #include #include @@ -69,7 +87,7 @@ static int scan_areadef(MFile_area ma, const char *name) return 0; } -static int file_position(MFile mf, int pos) +static int file_position(MFile mf, int pos, int offset) { int off = 0, c = mf->cur_file, ps; @@ -92,7 +110,8 @@ static int file_position(MFile mf, int pos) log(LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path); return -1; } - if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize, SEEK_SET) < 0) + if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset, + SEEK_SET) < 0) { log(LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path); return -1; @@ -121,6 +140,7 @@ MFile_area mf_init(const char *name) int fd, number; char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1]; + log(LOG_DEBUG, "mf_init(%s)", name); for (mp = open_areas; mp; mp = mp->next) if (!strcmp(name, mp->name)) abort(); @@ -216,6 +236,8 @@ MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag) char tmp[FILENAME_MAX+1]; mf_dir *dp; + log(LOG_LOG, "mf_open(%s bs=%d, %s)", name, block_size, + wflag ? "RW" : "RDONLY"); if (!ma) { if (!default_area && !(default_area = mf_init(MF_DEFAULT_AREA))) @@ -256,6 +278,13 @@ MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag) } else { + for (i = 0; i < new->no_files; i++) + { + if (new->files[i].bytes % block_size) + new->files[i].bytes += block_size - new->files[i].bytes % + block_size; + new->files[i].blocks = new->files[i].bytes / block_size; + } assert(!new->open); } new->blocksize = block_size; @@ -283,6 +312,7 @@ int mf_close(MFile mf) { int i; + log(LOG_DEBUG, "mf_close()"); assert(mf->open); for (i = 0; i < mf->no_files; i++) if (mf->files[i].fd >= 0) @@ -296,16 +326,18 @@ int mf_close(MFile mf) */ int mf_read(MFile mf, int no, int offset, int num, void *buf) { - int rd; + int rd, toread; - if (file_position(mf, no) < 0) + if (file_position(mf, no, offset) < 0) exit(1); - if ((rd = read(mf->files[mf->cur_file].fd, buf, mf->blocksize)) < 0) + toread = num ? num : mf->blocksize; + if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0) { - log(LOG_FATAL|LOG_ERRNO, "Read failed"); + log(LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)", + mf->files[mf->cur_file].path); exit(1); } - else if (rd < mf->blocksize) + else if (rd < toread) return 0; else return 1; @@ -316,21 +348,42 @@ int mf_read(MFile mf, int no, int offset, int num, void *buf) */ int mf_write(MFile mf, int no, int offset, int num, const void *buf) { - int ps; + int ps, nblocks, towrite; mf_dir *dp; char tmp[FILENAME_MAX+1]; + unsigned char dummych = '\xff'; - if ((ps = file_position(mf, no)) < 0) + if ((ps = file_position(mf, no, offset)) < 0) exit(1); - assert(ps <= mf->files[mf->cur_file].blocks); - /* extend table */ - if (ps == mf->files[mf->cur_file].blocks) + /* file needs to grow */ + while (ps >= mf->files[mf->cur_file].blocks) { - assert(mf->cur_file == mf->no_files - 1); - /* create new file */ - if (mf->files[mf->cur_file].dir->avail_bytes < - mf->blocksize) - { + log(LOG_DEBUG, "File grows"); + /* file overflow - allocate new file */ + if ((ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize > + mf->files[mf->cur_file].dir->avail_bytes) + { + /* cap off file? */ + if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes / + mf->blocksize) > 0) + { + log(LOG_DEBUG, "Capping off file %s at pos %d", + mf->files[mf->cur_file].path, nblocks); + if ((ps = file_position(mf, + (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) + + mf->files[mf->cur_file].blocks + nblocks, 0)) < 0) + exit(1); + if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1) + { + log(LOG_ERRNO|LOG_FATAL, "write dummy"); + exit(1); + } + mf->files[mf->cur_file].blocks += nblocks; + mf->files[mf->cur_file].bytes += nblocks * mf->blocksize; + mf->files[mf->cur_file].dir->avail_bytes -= nblocks * + mf->blocksize; + } + /* get other bit */ log(LOG_DEBUG, "Creating new file."); for (dp = mf->ma->dirs; dp && dp->avail_bytes < mf->min_bytes_creat; dp = dp->next); @@ -339,7 +392,9 @@ int mf_write(MFile mf, int no, int offset, int num, const void *buf) log(LOG_FATAL, "Cannot allocate more space for %s", mf->name); exit(1); } - mf->files[mf->cur_file].top = no - 1; + mf->files[mf->cur_file].top = (mf->cur_file ? + mf->files[mf->cur_file-1].top : -1) + + mf->files[mf->cur_file].blocks; mf->files[++(mf->cur_file)].top = -1; mf->files[mf->cur_file].dir = dp; mf->files[mf->cur_file].number = @@ -352,14 +407,19 @@ int mf_write(MFile mf, int no, int offset, int num, const void *buf) mf->files[mf->cur_file].path = xstrdup(tmp); mf->no_files++; /* open new file and position at beginning */ - if (file_position(mf, no) < 0) + if ((ps = file_position(mf, no, offset)) < 0) exit(1); } - mf->files[mf->cur_file].blocks++; - mf->files[mf->cur_file].bytes += mf->blocksize; - mf->files[mf->cur_file].dir->avail_bytes -= mf->blocksize; + else + { + nblocks = ps - mf->files[mf->cur_file].blocks + 1; + mf->files[mf->cur_file].blocks += nblocks; + mf->files[mf->cur_file].bytes += nblocks * mf->blocksize; + mf->files[mf->cur_file].dir->avail_bytes -= nblocks * mf->blocksize; + } } - if (write(mf->files[mf->cur_file].fd, buf, mf->blocksize) < mf->blocksize) + towrite = num ? num : mf->blocksize; + if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite) { log(LOG_FATAL|LOG_ERRNO, "Write failed"); exit(1);