1 /*
2 * This file is part of libbluray
3 * Copyright (C) 2014 VideoLAN
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see
17 * <http://www.gnu.org/licenses/>.
18 */
19
20 #if HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "file.h"
25
26 #include "util/logging.h"
27 #include "util/macro.h"
28 #include "util/strutl.h"
29
30 #include <stdio.h> // SEEK_*
31 #include <string.h> // strchr
32
33
file_size(AACS_FILE_H * fp)34 int64_t file_size(AACS_FILE_H *fp)
35 {
36 int64_t pos = file_tell(fp);
37 int64_t res1 = file_seek(fp, 0, SEEK_END);
38 int64_t length = file_tell(fp);
39 int64_t res2 = file_seek(fp, pos, SEEK_SET);
40
41 if (res1 < 0 || res2 < 0 || pos < 0 || length < 0) {
42 return -1;
43 }
44
45 return length;
46 }
47
file_mkdirs(const char * path)48 int file_mkdirs(const char *path)
49 {
50 int result = 0;
51 char *dir = str_dup(path);
52 char *end = dir;
53 char *p;
54
55 if (!dir) {
56 return -1;
57 }
58
59 /* strip file name */
60 if (!(end = strrchr(end, DIR_SEP_CHAR))) {
61 X_FREE(dir);
62 return -1;
63 }
64 *end = 0;
65
66 /* tokenize, stop to first existing dir */
67 while ((p = strrchr(dir, DIR_SEP_CHAR))) {
68 if (!file_path_exists(dir)) {
69 break;
70 }
71 *p = 0;
72 }
73
74 /* create missing dirs */
75 p = dir;
76 while (p < end) {
77
78 /* concatenate next non-existing dir */
79 while (*p) p++;
80 if (p >= end) break;
81 *p = DIR_SEP_CHAR;
82
83 result = file_mkdir(dir);
84 if (result < 0) {
85 BD_DEBUG(DBG_FILE | DBG_CRIT, "Error creating directory %s\n", dir);
86 break;
87 }
88 BD_DEBUG(DBG_FILE, " created directory %s\n", dir);
89 }
90
91 X_FREE(dir);
92 return result;
93 }
94