void fsUnmount(const char *name) { struct FILESYSTEM_IMAGE *next, *old; char *path; if (d->fs.mount == NULL) return; path = utilPathTranslate(name); if (strcmp(d->fs.mount->file->file, path) == 0) { next = d->fs.mount->next; fsFileClose(d->fs.mount->file); free(d->fs.mount->dir); free(d->fs.mount); d->fs.mount = next; free(path); return; } old = d->fs.mount; next = d->fs.mount->next; while (next != NULL) { if (strcmp(next->file->file, path) == 0) { old->next = next->next; free(next->dir); fsFileClose(next->file); free(next); free(path); return; } next = next->next; } free(path); return; }
void *dynlibOpen(const char *fname) { char *fname_n; char path[DARNIT_PATH_MAX]; FILESYSTEM_FILE *f; if (fname == NULL) return NULL; /* Find a file that matches the path */ if (strlen(d->fs.data_dir) + 2 + strlen(fname) > DARNIT_PATH_MAX) return NULL; if (strlen(d->fs.write_dir) + 2 + strlen(fname) > DARNIT_PATH_MAX) return NULL; /* First, look in write-dir */ sprintf(path, "%s/%s", d->fs.write_dir, fname); if (!(f = fsFileOpen(path, "r"))) { /* Nope. Look in data dir instead */ sprintf(path, "%s/%s", d->fs.data_dir, fname); if (!(f = fsFileOpen(path, "r"))) /* File doesn't seem to exist */ return NULL; } fsFileClose(f); #ifdef _WIN32 fname_n = utilPathTranslate(path); HINSTANCE *lib; lib = malloc(sizeof(HINSTANCE)); *lib = LoadLibrary(fname_n); #else void *lib; fname_n = malloc(strlen(path) + 1); sprintf(fname_n, "%s", path); lib = dlopen(fname_n, RTLD_NOW | RTLD_GLOBAL); #endif free(fname_n); return lib; }
int fsScanRealDir(const char *path, DIR_LIST **list, int rw) { DIR_LIST *tmp; int i = 0; #ifdef _WIN32 WIN32_FIND_DATA ffd; HANDLE hFind = INVALID_HANDLE_VALUE; //DWORD dError = 0; char dir[MAX_PATH], *new_path; if (strlen(path) > MAX_PATH - 3) return 0; new_path = utilPathTranslate(path); sprintf(dir, "%s\\*", new_path); free(new_path); if ((hFind = FindFirstFile(dir, &ffd)) == INVALID_HANDLE_VALUE) return 0; do { tmp = malloc(sizeof(DIR_LIST)); tmp->fname = malloc(strlen(ffd.cFileName) + 1); strcpy(tmp->fname, ffd.cFileName); fsPathMakeNative(tmp->fname); tmp->next = *list; *list = tmp; tmp->writeable = rw; tmp->in_file_image = 0; if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { tmp->directory = 1; tmp->file = 0; } else { tmp->directory = 0; tmp->file = 1; } i++; } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); #else DIR *dir; struct dirent *dir_ent; struct stat stat_b; char path_trans[DARNIT_PATH_MAX]; if (!(dir = opendir(path))) return 0; for (dir_ent = readdir(dir); dir_ent; dir_ent = readdir(dir)) { if (dir_ent->d_name[0] == '.') continue; sprintf(path_trans, "%s/%s", path, dir_ent->d_name); stat(path_trans, &stat_b); if (!S_ISDIR(stat_b.st_mode) && !S_ISREG(stat_b.st_mode)) continue; tmp = malloc(sizeof(DIR_LIST)); tmp->fname = malloc(strlen(dir_ent->d_name) + 1); strcpy(tmp->fname, dir_ent->d_name); tmp->writeable = rw; tmp->in_file_image = 0; tmp->directory = S_ISDIR(stat_b.st_mode); tmp->file = S_ISREG(stat_b.st_mode); tmp->next = *list; *list = tmp; i++; } closedir(dir); #endif return i; }
FILESYSTEM_FILE *fsFileOpen(const char *name, const char *mode) { char path[DARNIT_PATH_MAX]; char *path_new; FILE *fp; int write = 0; if (strlen(d->fs.data_dir) + 2 + strlen(name) > DARNIT_PATH_MAX) return NULL; if (strlen(d->fs.write_dir) + 2 + strlen(name) > DARNIT_PATH_MAX) return NULL; if (*name != '/') { if (strstr(mode, "w") || strstr(mode, "a") || strstr(mode, "+")) write = 1; /* Write-dir up next... */ sprintf(path, "%s/%s", d->fs.write_dir, name); path_new = utilPathTranslate(path); if ((fp = fopen(path_new, mode)) == NULL); else { if (write) return fsFileNew(path_new, mode, fp, -1, 0); return fsFileNew(path_new, mode, fp, fsFILELenghtGet(fp), 0); } free(path_new); } if (*name == '/' || name[1] == ':') { /* Path is absolute, skip all FS stuff */ if (strstr(mode, "w") || strstr(mode, "a") || strstr(mode, "+")) write = 1; path_new = utilPathTranslate(name); if (!(fp = fopen(path_new, mode))); else { if (write) return fsFileNew(path_new, mode, fp, -1, 0); return fsFileNew(path_new, mode, fp, fsFILELenghtGet(fp), 0); } free(path_new); return NULL; } /* Try read-only locations */ else if (!strstr(mode, "w") && !strstr(mode, "a") && !strstr(mode, "+")) { /* Mkay, that didn't work. I guess we'll try open it directly */ path_new = utilPathTranslate(name); if ((fp = fopen(path_new, mode)) == NULL); else /* W00t! */ return fsFileNew(path_new, mode, fp, (write) ? -1 : fsFILELenghtGet(fp), 0); free(path_new); /* Look in data containers */ path_new = malloc(strlen(name) + 1); sprintf(path_new, "%s", name); if ((fp = fsContainerFileInternalGet(path_new)) == NULL); else { return fsFileNew(path_new, mode, fsFILEDup(fsContainerFS(fp)), fsContainerFILELength(fp, name), fsContainerFILEStart(fp, name)); } free(path_new); /* Build data-dir path */ sprintf(path, "%s/%s", d->fs.data_dir, name); path_new = utilPathTranslate(path); if ((fp = fopen(path_new, mode)) == NULL); else return fsFileNew(path_new, mode, fp, fsFILELenghtGet(fp), 0); free(path_new); } return NULL; }