Commit 946811b6 authored by npzacs's avatar npzacs
Browse files

Merge file_mkdirs() from libbluray

parent aaec3fbb
......@@ -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;
}
......@@ -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_ */
......@@ -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);
......
......@@ -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];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment