diff options
Diffstat (limited to 'source/blender/blenlib/intern/storage.c')
-rw-r--r-- | source/blender/blenlib/intern/storage.c | 287 |
1 files changed, 11 insertions, 276 deletions
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index f3ecc799e1e..7fdf6ec8101 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -35,11 +35,6 @@ #include <stdio.h> #include <stdlib.h> -#ifndef WIN32 -# include <dirent.h> -#endif - -#include <time.h> #include <sys/stat.h> #if defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || defined(__sun) @@ -79,17 +74,13 @@ /* lib includes */ #include "MEM_guardedalloc.h" -#include "DNA_listBase.h" - -#include "BLI_listbase.h" +#include "BLI_utildefines.h" #include "BLI_linklist.h" #include "BLI_string.h" #include "BLI_fileops.h" #include "BLI_fileops_types.h" #include "BLI_path_util.h" -#include "../imbuf/IMB_imbuf.h" - /** * Copies the current working directory into *dir (max size maxncpy), and * returns a pointer to same. @@ -107,42 +98,6 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy) return getcwd(dir, maxncpy); } -/* - * Ordering function for sorting lists of files/directories. Returns -1 if - * entry1 belongs before entry2, 0 if they are equal, 1 if they should be swapped. - */ -static int bli_compare(struct direntry *entry1, struct direntry *entry2) -{ - /* type is equal to stat.st_mode */ - - /* directories come before non-directories */ - if (S_ISDIR(entry1->type)) { - if (S_ISDIR(entry2->type) == 0) return (-1); - } - else { - if (S_ISDIR(entry2->type)) return (1); - } - /* non-regular files come after regular files */ - if (S_ISREG(entry1->type)) { - if (S_ISREG(entry2->type) == 0) return (-1); - } - else { - if (S_ISREG(entry2->type)) return (1); - } - /* arbitrary, but consistent, ordering of different types of non-regular files */ - if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1); - if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1); - - /* OK, now we know their S_IFMT fields are the same, go on to a name comparison */ - /* make sure "." and ".." are always first */ - if (strcmp(entry1->relname, ".") == 0) return (-1); - if (strcmp(entry2->relname, ".") == 0) return (1); - if (strcmp(entry1->relname, "..") == 0) return (-1); - if (strcmp(entry2->relname, "..") == 0) return (1); - - return (BLI_natstrcmp(entry1->relname, entry2->relname)); -} - /** * Returns the number of free bytes on the volume containing the specified pathname. */ /* Not actually used anywhere. @@ -204,230 +159,6 @@ double BLI_dir_free_space(const char *dir) #endif } -struct BuildDirCtx { - struct direntry *files; /* array[nrfiles] */ - int nrfiles; -}; - -/** - * Scans the directory named *dirname and appends entries for its contents to files. - */ -static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname) -{ - struct ListBase dirbase = {NULL, NULL}; - int newnum = 0; - DIR *dir; - - if ((dir = opendir(dirname)) != NULL) { - - const struct dirent *fname; - while ((fname = readdir(dir)) != NULL) { - struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink)); - if (dlink != NULL) { - dlink->name = BLI_strdup(fname->d_name); - BLI_addhead(&dirbase, dlink); - newnum++; - } - } - - if (newnum) { - - if (dir_ctx->files) { - void * const tmp = realloc(dir_ctx->files, (dir_ctx->nrfiles + newnum) * sizeof(struct direntry)); - if (tmp) { - dir_ctx->files = (struct direntry *)tmp; - } - else { /* realloc fail */ - free(dir_ctx->files); - dir_ctx->files = NULL; - } - } - - if (dir_ctx->files == NULL) - dir_ctx->files = (struct direntry *)malloc(newnum * sizeof(struct direntry)); - - if (dir_ctx->files) { - struct dirlink * dlink = (struct dirlink *) dirbase.first; - struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles]; - while (dlink) { - char fullname[PATH_MAX]; - memset(file, 0, sizeof(struct direntry)); - file->relname = dlink->name; - file->path = BLI_strdupcat(dirname, dlink->name); - BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name); - BLI_stat(fullname, &file->s); - file->type = file->s.st_mode; - file->flags = 0; - dir_ctx->nrfiles++; - file++; - dlink = dlink->next; - } - } - else { - printf("Couldn't get memory for dir\n"); - exit(1); - } - - BLI_freelist(&dirbase); - if (dir_ctx->files) { - qsort(dir_ctx->files, dir_ctx->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare); - } - } - else { - printf("%s empty directory\n", dirname); - } - - closedir(dir); - } - else { - printf("%s non-existant directory\n", dirname); - } -} - -/** - * Fills in the "mode[123]", "size" and "string" fields in the elements of the files - * array with descriptive details about each item. "string" will have a format similar to "ls -l". - */ -static void bli_adddirstrings(struct BuildDirCtx *dir_ctx) -{ - const char *types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"}; - /* symbolic display, indexed by mode field value */ - int num; -#ifdef WIN32 - __int64 st_size; -#else - off_t st_size; - int mode; -#endif - - struct direntry *file; - struct tm *tm; - time_t zero = 0; - - for (num = 0, file = dir_ctx->files; num < dir_ctx->nrfiles; num++, file++) { - - - /* Mode */ -#ifdef WIN32 - BLI_strncpy(file->mode1, types[0], sizeof(file->mode1)); - BLI_strncpy(file->mode2, types[0], sizeof(file->mode2)); - BLI_strncpy(file->mode3, types[0], sizeof(file->mode3)); -#else - mode = file->s.st_mode; - - BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1)); - BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2)); - BLI_strncpy(file->mode3, types[(mode & 0007)], sizeof(file->mode3)); - - if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2] == '-')) file->mode2[2] = 'l'; - - if (mode & (S_ISUID | S_ISGID)) { - if (file->mode1[2] == 'x') file->mode1[2] = 's'; - else file->mode1[2] = 'S'; - - if (file->mode2[2] == 'x') file->mode2[2] = 's'; - } - - if (mode & S_ISVTX) { - if (file->mode3[2] == 'x') file->mode3[2] = 't'; - else file->mode3[2] = 'T'; - } -#endif - - - /* User */ -#ifdef WIN32 - strcpy(file->owner, "user"); -#else - { - struct passwd *pwuser; - pwuser = getpwuid(file->s.st_uid); - if (pwuser) { - BLI_strncpy(file->owner, pwuser->pw_name, sizeof(file->owner)); - } - else { - BLI_snprintf(file->owner, sizeof(file->owner), "%u", file->s.st_uid); - } - } -#endif - - - /* Time */ - tm = localtime(&file->s.st_mtime); - // prevent impossible dates in windows - if (tm == NULL) tm = localtime(&zero); - strftime(file->time, sizeof(file->time), "%H:%M", tm); - strftime(file->date, sizeof(file->date), "%d-%b-%y", tm); - - - /* Size */ - /* - * Seems st_size is signed 32-bit value in *nix and Windows. This - * will buy us some time until files get bigger than 4GB or until - * everyone starts using __USE_FILE_OFFSET64 or equivalent. - */ - st_size = file->s.st_size; - - if (st_size > 1024 * 1024 * 1024) { - BLI_snprintf(file->size, sizeof(file->size), "%.2f GiB", ((double)st_size) / (1024 * 1024 * 1024)); - } - else if (st_size > 1024 * 1024) { - BLI_snprintf(file->size, sizeof(file->size), "%.1f MiB", ((double)st_size) / (1024 * 1024)); - } - else if (st_size > 1024) { - BLI_snprintf(file->size, sizeof(file->size), "%d KiB", (int)(st_size / 1024)); - } - else { - BLI_snprintf(file->size, sizeof(file->size), "%d B", (int)st_size); - } - } -} - -/** - * Scans the contents of the directory named *dirname, and allocates and fills in an - * array of entries describing them in *filelist. The length of the array is the function result. - */ -unsigned int BLI_dir_contents(const char *dirname, struct direntry **filelist) -{ - struct BuildDirCtx dir_ctx; - - dir_ctx.nrfiles = 0; - dir_ctx.files = NULL; - - bli_builddir(&dir_ctx, dirname); - bli_adddirstrings(&dir_ctx); - - if (dir_ctx.files) { - *filelist = dir_ctx.files; - } - else { - // keep blender happy. Blender stores this in a variable - // where 0 has special meaning..... - *filelist = malloc(sizeof(struct direntry)); - } - - return dir_ctx.nrfiles; -} - -/* frees storage for an array of direntries, including the array itself. */ -void BLI_free_filelist(struct direntry *filelist, unsigned int nrentries) -{ - unsigned int i; - for (i = 0; i < nrentries; ++i) { - struct direntry * const entry = filelist + i; - if (entry->image) { - IMB_freeImBuf(entry->image); - } - if (entry->relname) - MEM_freeN(entry->relname); - if (entry->path) - MEM_freeN(entry->path); - /* entry->poin assumed not to point to anything needing freeing here */ - } - - free(filelist); -} - /** * Returns the file size of an opened file descriptor. @@ -495,6 +226,8 @@ int BLI_exists(const char *name) if (res == -1) return(0); #else struct stat st; + BLI_assert(name); + BLI_assert(!BLI_path_is_rel(name)); if (stat(name, &st)) return(0); #endif return(st.st_mode); @@ -554,7 +287,7 @@ bool BLI_is_file(const char *path) LinkNode *BLI_file_read_as_lines(const char *name) { FILE *fp = BLI_fopen(name, "r"); - LinkNode *lines = NULL; + LinkNodePair lines = {NULL, NULL}; char *buf; size_t size; @@ -564,6 +297,11 @@ LinkNode *BLI_file_read_as_lines(const char *name) size = (size_t)ftell(fp); fseek(fp, 0, SEEK_SET); + if (UNLIKELY(size == (size_t)-1)) { + fclose(fp); + return NULL; + } + buf = MEM_mallocN(size, "file_as_lines"); if (buf) { size_t i, last = 0; @@ -578,7 +316,7 @@ LinkNode *BLI_file_read_as_lines(const char *name) if (i == size || buf[i] == '\n') { char *line = BLI_strdupn(&buf[last], i - last); - BLI_linklist_prepend(&lines, line); + BLI_linklist_append(&lines, line); /* faster to build singly-linked list in reverse order */ /* alternatively, could process buffer in reverse order so * list ends up right way round to start with */ @@ -591,9 +329,7 @@ LinkNode *BLI_file_read_as_lines(const char *name) fclose(fp); - /* get them the right way round */ - BLI_linklist_reverse(&lines); - return lines; + return lines.list; } /* @@ -636,4 +372,3 @@ bool BLI_file_older(const char *file1, const char *file2) #endif return (st1.st_mtime < st2.st_mtime); } - |