1 /* Copyright  (C) 2010-2019 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (retro_dirent.c).
5  * ---------------------------------------------------------------------------------------
6  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
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.
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <retro_common.h>
28 
29 #include <boolean.h>
30 #include <retro_dirent.h>
31 #define VFS_FRONTEND
32 #include <vfs/vfs_implementation.h>
33 
34 static retro_vfs_opendir_t dirent_opendir_cb = NULL;
35 static retro_vfs_readdir_t dirent_readdir_cb = NULL;
36 static retro_vfs_dirent_get_name_t dirent_dirent_get_name_cb = NULL;
37 static retro_vfs_dirent_is_dir_t dirent_dirent_is_dir_cb = NULL;
38 static retro_vfs_closedir_t dirent_closedir_cb = NULL;
39 
dirent_vfs_init(const struct retro_vfs_interface_info * vfs_info)40 void dirent_vfs_init(const struct retro_vfs_interface_info* vfs_info)
41 {
42    const struct retro_vfs_interface* vfs_iface;
43 
44    dirent_opendir_cb = NULL;
45    dirent_readdir_cb = NULL;
46    dirent_dirent_get_name_cb = NULL;
47    dirent_dirent_is_dir_cb = NULL;
48    dirent_closedir_cb = NULL;
49 
50    vfs_iface = vfs_info->iface;
51 
52    if (vfs_info->required_interface_version < DIRENT_REQUIRED_VFS_VERSION || !vfs_iface)
53       return;
54 
55    dirent_opendir_cb = vfs_iface->opendir;
56    dirent_readdir_cb = vfs_iface->readdir;
57    dirent_dirent_get_name_cb = vfs_iface->dirent_get_name;
58    dirent_dirent_is_dir_cb = vfs_iface->dirent_is_dir;
59    dirent_closedir_cb = vfs_iface->closedir;
60 }
61 
retro_opendir_include_hidden(const char * name,bool include_hidden)62 struct RDIR *retro_opendir_include_hidden(const char *name, bool include_hidden)
63 {
64    if (dirent_opendir_cb != NULL)
65       return (struct RDIR *)dirent_opendir_cb(name, include_hidden);
66    return (struct RDIR *)retro_vfs_opendir_impl(name, include_hidden);
67 }
68 
retro_opendir(const char * name)69 struct RDIR *retro_opendir(const char *name)
70 {
71    return retro_opendir_include_hidden(name, false);
72 }
73 
retro_dirent_error(struct RDIR * rdir)74 bool retro_dirent_error(struct RDIR *rdir)
75 {
76    /* Left for compatibility */
77    return false;
78 }
79 
retro_readdir(struct RDIR * rdir)80 int retro_readdir(struct RDIR *rdir)
81 {
82    if (dirent_readdir_cb != NULL)
83       return dirent_readdir_cb((struct retro_vfs_dir_handle *)rdir);
84    return retro_vfs_readdir_impl((struct retro_vfs_dir_handle *)rdir);
85 }
86 
retro_dirent_get_name(struct RDIR * rdir)87 const char *retro_dirent_get_name(struct RDIR *rdir)
88 {
89    if (dirent_dirent_get_name_cb != NULL)
90       return dirent_dirent_get_name_cb((struct retro_vfs_dir_handle *)rdir);
91    return retro_vfs_dirent_get_name_impl((struct retro_vfs_dir_handle *)rdir);
92 }
93 
94 /**
95  *
96  * retro_dirent_is_dir:
97  * @rdir         : pointer to the directory entry.
98  * @unused       : deprecated, included for compatibility reasons, pass NULL
99  *
100  * Is the directory listing entry a directory?
101  *
102  * Returns: true if directory listing entry is
103  * a directory, false if not.
104  */
retro_dirent_is_dir(struct RDIR * rdir,const char * unused)105 bool retro_dirent_is_dir(struct RDIR *rdir, const char *unused)
106 {
107    if (dirent_dirent_is_dir_cb != NULL)
108       return dirent_dirent_is_dir_cb((struct retro_vfs_dir_handle *)rdir);
109    return retro_vfs_dirent_is_dir_impl((struct retro_vfs_dir_handle *)rdir);
110 }
111 
retro_closedir(struct RDIR * rdir)112 void retro_closedir(struct RDIR *rdir)
113 {
114    if (dirent_closedir_cb != NULL)
115       dirent_closedir_cb((struct retro_vfs_dir_handle *)rdir);
116    else
117       retro_vfs_closedir_impl((struct retro_vfs_dir_handle *)rdir);
118 }
119