2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-11-24 17:26:11 quinn
8 * Mostly about making some ISAM stuff in the config file optional.
10 * Revision 1.11 1995/11/13 09:32:43 quinn
13 * Revision 1.10 1995/09/04 12:33:22 adam
14 * Various cleanup. YAZ util used instead.
16 * Revision 1.9 1994/11/04 14:26:39 quinn
19 * Revision 1.8 1994/10/05 16:56:42 quinn
22 * Revision 1.7 1994/09/19 14:12:37 quinn
25 * Revision 1.6 1994/09/14 13:10:15 quinn
26 * Corrected some bugs in the init-phase
28 * Revision 1.5 1994/09/12 08:01:51 quinn
31 * Revision 1.4 1994/09/01 14:51:07 quinn
32 * Allowed mf_write to write beyond eof+1.
34 * Revision 1.3 1994/08/24 09:37:17 quinn
35 * Changed reaction to read return values.
37 * Revision 1.2 1994/08/23 14:50:48 quinn
40 * Revision 1.1 1994/08/23 14:41:33 quinn
41 * First functional version.
47 * TODO: The size estimates in init may not be accurate due to
48 * only partially written final blocks.
51 #include <sys/types.h>
62 static MFile_area_struct *open_areas = 0;
63 static MFile_area_struct *default_area = 0;
65 static int scan_areadef(MFile_area ma, const char *name)
68 * If no definition is given, use current directory, unlimited.
70 const char *ad = res_get_def(common_resource, name, ".:-1b");
71 int offset = 0, rs, size, multi, rd;
72 char dirname[FILENAME_MAX+1], unit;
73 mf_dir **dp = &ma->dirs, *dir = *dp;
77 logf (LOG_FATAL, "Could not find resource '%s'", name);
82 rs = sscanf(ad + offset, "%[^:]:%d%c %n", dirname, &size, &unit, &rd);
87 logf (LOG_FATAL, "Illegal directory description: %s", ad + offset);
92 case 'B': case 'b': multi = 1; break;
93 case 'K': case 'k': multi = 1024; break;
94 case 'M': case 'm': multi = 1048576; break;
96 logf (LOG_FATAL, "Illegal unit: %c in %s", unit, ad + offset);
99 *dp = dir = xmalloc(sizeof(mf_dir));
101 strcpy(dir->name, dirname);
102 dir->max_bytes = dir->avail_bytes = size * multi;
109 static int file_position(MFile mf, int pos, int offset)
111 int off = 0, c = mf->cur_file, ps;
113 if ((c > 0 && pos <= mf->files[c-1].top) ||
114 (c < mf->no_files -1 && pos > mf->files[c].top))
117 while (mf->files[c].top >= 0 && mf->files[c].top < pos)
119 off += mf->files[c].blocks;
122 assert(c < mf->no_files);
125 off = c ? (mf->files[c-1].top + 1) : 0;
126 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
127 mf->wr ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
129 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
132 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
135 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
142 static int cmp_part_file(const void *p1, const void *p2)
144 return ((part_file *)p1)->number - ((part_file *)p2)->number;
148 * Create a new area, cotaining metafiles in directories.
149 * Find the part-files in each directory, and inventory the existing metafiles.
151 MFile_area mf_init(const char *name)
153 MFile_area ma = xmalloc(sizeof(MFile_area_struct)), mp;
156 part_file *part_f = 0;
160 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
162 logf (LOG_DEBUG, "mf_init(%s)", name);
163 for (mp = open_areas; mp; mp = mp->next)
164 if (!strcmp(name, mp->name))
166 strcpy(ma->name, name);
167 ma->next = open_areas;
171 if (scan_areadef(ma, name) < 0)
173 logf (LOG_FATAL, "Failed to access description of '%s'", name);
176 /* look at each directory */
177 for (dirp = ma->dirs; dirp; dirp = dirp->next)
179 if (!(dd = opendir(dirp->name)))
181 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", dirp->name);
184 /* look at each file */
185 while ((dent = readdir(dd)))
187 if (*dent->d_name == '.')
189 if (sscanf(dent->d_name, "%[^.].mf.%d", metaname, &number) != 2)
191 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
194 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
197 if (!strcmp(meta_f->name, metaname))
199 part_f = &meta_f->files[meta_f->no_files++];
206 meta_f = xmalloc(sizeof(*meta_f));
208 meta_f->next = ma->mfiles;
210 meta_f->cur_file = -1;
212 strcpy(meta_f->name, metaname);
213 part_f = &meta_f->files[0];
214 meta_f->no_files = 1;
216 part_f->number = number;
219 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
220 part_f->path = xstrdup(tmpnam);
222 if ((fd = open(part_f->path, O_RDONLY)) < 0)
224 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
228 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
230 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
235 if (dirp->max_bytes >= 0)
236 dirp->avail_bytes -= part_f->bytes;
240 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
242 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
244 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
252 * If !ma, Use MF_DEFAULT_AREA.
254 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
256 struct meta_file *new;
258 char tmp[FILENAME_MAX+1];
261 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
262 wflag ? "RW" : "RDONLY");
265 if (!default_area && !(default_area = mf_init(MF_DEFAULT_AREA)))
267 logf (LOG_FATAL, "Failed to open default area.");
272 for (new = ma->mfiles; new; new = new->next)
273 if (!strcmp(name, new->name))
280 new = xmalloc(sizeof(*new));
281 strcpy(new->name, name);
282 /* allocate one, empty file */
284 new->files[0].bytes = new->files[0].blocks = 0;
285 new->files[0].top = -1;
286 new->files[0].number = 0;
287 new->files[0].fd = -1;
288 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
289 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
290 new->min_bytes_creat; dp = dp->next);
293 logf (LOG_FATAL, "Insufficient space for new mfile.");
296 new->files[0].dir = dp;
297 sprintf(tmp, "%s/%s.mf.%d", dp->name, new->name, 0);
298 new->files[0].path = xstrdup(tmp);
303 for (i = 0; i < new->no_files; i++)
305 if (new->files[i].bytes % block_size)
306 new->files[i].bytes += block_size - new->files[i].bytes %
308 new->files[i].blocks = new->files[i].bytes / block_size;
312 new->blocksize = block_size;
313 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
318 for (i = 0; i < new->no_files; i++)
320 new->files[i].blocks = new->files[i].bytes / new->blocksize;
321 if (i == new->no_files)
322 new->files[i].top = -1;
324 new->files[i].top = i ? (new->files[i-1].top + new->files[i].blocks)
325 : (new->files[i].blocks - 1);
333 int mf_close(MFile mf)
337 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
339 for (i = 0; i < mf->no_files; i++)
340 if (mf->files[i].fd >= 0)
342 close(mf->files[i].fd);
343 mf->files[i].fd = -1;
350 * Read one block from a metafile. Interface mirrors bfile.
352 int mf_read(MFile mf, int no, int offset, int num, void *buf)
356 if (file_position(mf, no, offset) < 0)
358 toread = num ? num : mf->blocksize;
359 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
361 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
362 mf->files[mf->cur_file].path);
365 else if (rd < toread)
374 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
376 int ps, nblocks, towrite;
378 char tmp[FILENAME_MAX+1];
379 unsigned char dummych = '\xff';
381 if ((ps = file_position(mf, no, offset)) < 0)
383 /* file needs to grow */
384 while (ps >= mf->files[mf->cur_file].blocks)
386 logf (LOG_DEBUG, "File grows");
387 /* file overflow - allocate new file */
388 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
389 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
390 mf->files[mf->cur_file].dir->avail_bytes)
393 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
396 logf (LOG_DEBUG, "Capping off file %s at pos %d",
397 mf->files[mf->cur_file].path, nblocks);
398 if ((ps = file_position(mf,
399 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
400 mf->files[mf->cur_file].blocks + nblocks, 0)) < 0)
402 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
404 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
407 mf->files[mf->cur_file].blocks += nblocks;
408 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
409 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
413 logf (LOG_DEBUG, "Creating new file.");
414 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
415 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
418 logf (LOG_FATAL, "Cannot allocate more space for %s",
422 mf->files[mf->cur_file].top = (mf->cur_file ?
423 mf->files[mf->cur_file-1].top : -1) +
424 mf->files[mf->cur_file].blocks;
425 mf->files[++(mf->cur_file)].top = -1;
426 mf->files[mf->cur_file].dir = dp;
427 mf->files[mf->cur_file].number =
428 mf->files[mf->cur_file-1].number + 1;
429 mf->files[mf->cur_file].blocks =
430 mf->files[mf->cur_file].bytes = 0;
431 mf->files[mf->cur_file].fd = -1;
432 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
433 mf->files[mf->cur_file].number);
434 mf->files[mf->cur_file].path = xstrdup(tmp);
436 /* open new file and position at beginning */
437 if ((ps = file_position(mf, no, offset)) < 0)
442 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
443 mf->files[mf->cur_file].blocks += nblocks;
444 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
445 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
446 mf->files[mf->cur_file].dir->avail_bytes -=
447 nblocks * mf->blocksize;
450 towrite = num ? num : mf->blocksize;
451 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
453 logf (LOG_FATAL|LOG_ERRNO, "Write failed");
460 * Destroy a metafile, unlinking component files. File must be open.
462 int mf_unlink(MFile mf)
469 * Unlink the file by name, rather than MFile-handle. File should be closed.
471 int mf_unlink_name(MFile_area ma, const char *name)