1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (file_path_io.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <file/file_path.h>
32 #include <compat/strl.h>
33 #include <compat/posix_string.h>
34 #include <retro_miscellaneous.h>
35 #include <string/stdstring.h>
37 #include <vfs/vfs_implementation.h>
42 #include <unistd.h> /* stat() is defined here */
45 /* TODO/FIXME - globals */
46 static retro_vfs_stat_t path_stat_cb = retro_vfs_stat_impl;
47 static retro_vfs_mkdir_t path_mkdir_cb = retro_vfs_mkdir_impl;
49 void path_vfs_init(const struct retro_vfs_interface_info* vfs_info)
51 const struct retro_vfs_interface*
52 vfs_iface = vfs_info->iface;
54 path_stat_cb = retro_vfs_stat_impl;
55 path_mkdir_cb = retro_vfs_mkdir_impl;
57 if (vfs_info->required_interface_version < PATH_REQUIRED_VFS_VERSION || !vfs_iface)
60 path_stat_cb = vfs_iface->stat;
61 path_mkdir_cb = vfs_iface->mkdir;
64 int path_stat(const char *path)
66 return path_stat_cb(path, NULL);
73 * Checks if path is a directory.
75 * @return true if path is a directory, otherwise false.
77 bool path_is_directory(const char *path)
79 return (path_stat_cb(path, NULL) & RETRO_VFS_STAT_IS_DIRECTORY) != 0;
82 bool path_is_character_special(const char *path)
84 return (path_stat_cb(path, NULL) & RETRO_VFS_STAT_IS_CHARACTER_SPECIAL) != 0;
87 bool path_is_valid(const char *path)
89 return (path_stat_cb(path, NULL) & RETRO_VFS_STAT_IS_VALID) != 0;
92 int32_t path_get_size(const char *path)
95 if (path_stat_cb(path, &filesize) != 0)
105 * Create directory on filesystem.
107 * Recursive function.
109 * @return true if directory could be created, otherwise false.
111 bool path_mkdir(const char *dir)
113 bool norecurse = false;
114 char *basedir = NULL;
119 /* Use heap. Real chance of stack
120 * overflow if we recurse too hard. */
121 if (!(basedir = strdup(dir)))
124 path_parent_dir(basedir, strlen(basedir));
126 if (!*basedir || !strcmp(basedir, dir))
132 if ( path_is_directory(basedir)
133 || path_mkdir(basedir))
140 int ret = path_mkdir_cb(dir);
142 /* Don't treat this as an error. */
143 if (ret == -2 && path_is_directory(dir))