- 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, "Creating new file.");
- for (dp = mf->ma->dirs; dp && dp->avail_bytes < mf->min_bytes_creat;
- dp = dp->next);
+ yaz_log(YLOG_FATAL, "mf_write: %s error (1)", mf->name);
+ ret = -1;
+ goto out;
+ }
+ /* file needs to grow */
+ while (ps >= mf->files[mf->cur_file].blocks)
+ {
+ mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
+ mf->blocksize;
+ /* file overflow - allocate new file */
+ if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
+ needed > mf->files[mf->cur_file].dir->avail_bytes)
+ {
+ /* cap off file? */
+ if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
+ mf->blocksize)) > 0)
+ {
+ yaz_log(YLOG_DEBUG, "Capping off file %s at pos " ZINT_FORMAT,
+ 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 - 1, 0)) < 0)
+ {
+ yaz_log(YLOG_FATAL, "mf_write: %s error (2)",
+ mf->name);
+ ret = -1;
+ goto out;
+ }
+ yaz_log(YLOG_DEBUG, "ps = " ZINT_FORMAT, ps);
+ if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
+ {
+ yaz_log(YLOG_ERRNO|YLOG_FATAL, "mf_write: %s error (3)",
+ mf->name);
+ ret = -1;
+ goto out;
+ }
+ 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 */
+ yaz_log(YLOG_DEBUG, "Creating new file.");
+ for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
+ dp->avail_bytes < needed; dp = dp->next);