void FioOpenFile(int slot, const char *filename) { FILE *f; #if defined(LIMITED_FDS) FioFreeHandle(); #endif /* LIMITED_FDS */ f = FioFOpenFile(filename); if (f == NULL) usererror("Cannot open file '%s'", filename); uint32 pos = ftell(f); FioCloseFile(slot); // if file was opened before, close it _fio.handles[slot] = f; _fio.filenames[slot] = filename; /* Store the filename without path and extension */ const char *t = strrchr(filename, PATHSEPCHAR); _fio.shortnames[slot] = strdup(t == NULL ? filename : t); char *t2 = strrchr(_fio.shortnames[slot], '.'); if (t2 != NULL) *t2 = '\0'; strtolower(_fio.shortnames[slot]); #if defined(LIMITED_FDS) _fio.usage_count[slot] = 0; _fio.open_handles++; #endif /* LIMITED_FDS */ FioSeekToFile(slot, pos); }
void FioCloseAll() { int i; for (i = 0; i != lengthof(_fio.handles); i++) FioCloseFile(i); }
static void FioFreeHandle() { /* If we are about to open a file that will exceed the limit, close a file */ if (_fio.open_handles + 1 == LIMITED_FDS) { uint i, count; int slot; count = UINT_MAX; slot = -1; /* Find the file that is used the least */ for (i = 0; i < lengthof(_fio.handles); i++) { if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) { count = _fio.usage_count[i]; slot = i; } } assert(slot != -1); DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filenames[slot], slot); FioCloseFile(slot); } }
void FioCloseAll() { for (int i = 0; i != lengthof(_fio.handles); i++) { FioCloseFile(i); } }