/src/vlc/src/misc/filesystem.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: LGPL-2.1-or-later |
2 | | /***************************************************************************** |
3 | | * filesystem.c: filesystem helpers |
4 | | ***************************************************************************** |
5 | | * Copyright © 2024 VLC authors, VideoLAN and Videolabs |
6 | | * |
7 | | * Authors: Gabriel Lafond Thenaille <gabriel@videolabs.io> |
8 | | *****************************************************************************/ |
9 | | |
10 | | #ifdef HAVE_CONFIG_H |
11 | | # include "config.h" |
12 | | #endif |
13 | | |
14 | | #include <vlc_common.h> |
15 | | #include <vlc_fs.h> |
16 | | |
17 | | /** |
18 | | * Create all directories in the given path if missing. |
19 | | */ |
20 | | int vlc_mkdir_parent(const char *dirname, mode_t mode) |
21 | 0 | { |
22 | 0 | int ret = vlc_mkdir(dirname, mode); |
23 | 0 | if (ret == 0 || errno == EEXIST) { |
24 | 0 | return 0; |
25 | 0 | } else if (errno != ENOENT) { |
26 | 0 | return -1; |
27 | 0 | } |
28 | | |
29 | 0 | char *path = strdup(dirname); |
30 | 0 | if (path == NULL) { |
31 | 0 | return -1; |
32 | 0 | } |
33 | | |
34 | 0 | char *ptr = path + 1; |
35 | 0 | while (*ptr) { |
36 | 0 | ptr = strchr(ptr, DIR_SEP_CHAR); |
37 | 0 | if (ptr == NULL) { |
38 | 0 | break; |
39 | 0 | } |
40 | 0 | *ptr = '\0'; |
41 | 0 | if (vlc_mkdir(path, mode) != 0) { |
42 | 0 | if (errno != EEXIST) { |
43 | 0 | free(path); |
44 | 0 | return -1; |
45 | 0 | } |
46 | 0 | } |
47 | 0 | *ptr = DIR_SEP_CHAR; |
48 | 0 | ptr++; |
49 | 0 | } |
50 | 0 | ret = vlc_mkdir(path, mode); |
51 | 0 | if (errno == EEXIST) { |
52 | 0 | ret = 0; |
53 | 0 | } |
54 | 0 | free(path); |
55 | 0 | return ret; |
56 | 0 | } |