libaacs | branch: master | npzacs <npz...@gmail.com> | Mon Feb 22 23:07:34 2016 +0200| [946811b6338f7d8a2fac8e4ed8b017efd0ab90e6] | committer: npzacs
Merge file_mkdirs() from libbluray > http://git.videolan.org/gitweb.cgi/libaacs.git/?a=commit;h=946811b6338f7d8a2fac8e4ed8b017efd0ab90e6 --- src/file/file.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ src/file/file.h | 2 ++ src/file/file_posix.c | 6 ++++++ src/file/file_win32.c | 13 +++++++++++++ 4 files changed, 73 insertions(+) diff --git a/src/file/file.c b/src/file/file.c index f94b592..00c7550 100644 --- a/src/file/file.c +++ b/src/file/file.c @@ -23,7 +23,12 @@ #include "file.h" +#include "util/logging.h" +#include "util/macro.h" +#include "util/strutl.h" + #include <stdio.h> // SEEK_* +#include <string.h> // strchr int64_t file_size(AACS_FILE_H *fp) @@ -39,3 +44,50 @@ int64_t file_size(AACS_FILE_H *fp) return length; } + +int file_mkdirs(const char *path) +{ + int result = 0; + char *dir = str_dup(path); + char *end = dir; + char *p; + + if (!dir) { + return -1; + } + + /* strip file name */ + if (!(end = strrchr(end, DIR_SEP_CHAR))) { + X_FREE(dir); + return -1; + } + *end = 0; + + /* tokenize, stop to first existing dir */ + while ((p = strrchr(dir, DIR_SEP_CHAR))) { + if (!file_path_exists(dir)) { + break; + } + *p = 0; + } + + /* create missing dirs */ + p = dir; + while (p < end) { + + /* concatenate next non-existing dir */ + while (*p) p++; + if (p >= end) break; + *p = DIR_SEP_CHAR; + + result = file_mkdir(dir); + if (result < 0) { + BD_DEBUG(DBG_FILE | DBG_CRIT, "Error creating directory %s\n", dir); + break; + } + BD_DEBUG(DBG_FILE, " created directory %s\n", dir); + } + + X_FREE(dir); + return result; +} diff --git a/src/file/file.h b/src/file/file.h index 8dd9021..7bf8f2c 100644 --- a/src/file/file.h +++ b/src/file/file.h @@ -51,6 +51,8 @@ BD_PRIVATE extern AACS_FILE_H *(*file_open)(const char* filename, const char *mo */ BD_PRIVATE int file_unlink(const char *file); +BD_PRIVATE int file_path_exists(const char *path); BD_PRIVATE int file_mkdir(const char *dir); +BD_PRIVATE int file_mkdirs(const char *path); #endif /* FILE_H_ */ diff --git a/src/file/file_posix.c b/src/file/file_posix.c index e10b1b6..07dd8a5 100644 --- a/src/file/file_posix.c +++ b/src/file/file_posix.c @@ -139,6 +139,12 @@ int file_unlink(const char *file) return remove(file); } +int file_path_exists(const char *path) +{ + struct stat s; + return stat(path, &s); +} + int file_mkdir(const char *dir) { return mkdir(dir, S_IRWXU); diff --git a/src/file/file_win32.c b/src/file/file_win32.c index 70727d4..f8072a6 100644 --- a/src/file/file_win32.c +++ b/src/file/file_win32.c @@ -121,6 +121,19 @@ int file_unlink(const char *file) return _wremove(wfile); } +int file_path_exists(const char *path) +{ + wchar_t wpath[MAX_PATH]; + + MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH); + + DWORD dwAttrib = GetFileAttributesW(wpath); + if (dwAttrib != INVALID_FILE_ATTRIBUTES) { + return 0; + } + return -1; +} + int file_mkdir(const char *dir) { wchar_t wdir[MAX_PATH]; _______________________________________________ libaacs-devel mailing list libaacs-devel@videolan.org https://mailman.videolan.org/listinfo/libaacs-devel