retro_dirent.c 7.9 KB
Newer Older
Libretro-Admin's avatar
Libretro-Admin committed
1
/* Copyright  (C) 2010-2017 The RetroArch team
Libretro-Admin's avatar
Libretro-Admin committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * ---------------------------------------------------------------------------------------
 * The following license statement only applies to this file (retro_dirent.c).
 * ---------------------------------------------------------------------------------------
 *
 * Permission is hereby granted, free of charge,
 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
24
25
#include <stdlib.h>
#include <string.h>
Libretro-Admin's avatar
Libretro-Admin committed
26

Libretro-Admin's avatar
Updates    
Libretro-Admin committed
27
28
#include <retro_common.h>

Libretro-Admin's avatar
Libretro-Admin committed
29
30
#include <boolean.h>
#include <retro_dirent.h>
31
32
33
#include <encodings/utf.h>
#include <compat/strl.h>
#include <string/stdstring.h>
Libretro-Admin's avatar
Libretro-Admin committed
34

Libretro-Admin's avatar
Libretro-Admin committed
35
#if defined(_WIN32)
Libretro-Admin's avatar
Libretro-Admin committed
36
37
38
#  ifdef _MSC_VER
#    define setmode _setmode
#  endif
Libretro-Admin's avatar
Libretro-Admin committed
39
#include <sys/stat.h>
Libretro-Admin's avatar
Libretro-Admin committed
40
41
42
43
44
45
46
47
48
#  ifdef _XBOX
#    include <xtl.h>
#    define INVALID_FILE_ATTRIBUTES -1
#  else
#    include <io.h>
#    include <fcntl.h>
#    include <direct.h>
#    include <windows.h>
#  endif
Libretro-Admin's avatar
Libretro-Admin committed
49
#elif defined(VITA)
Libretro-Admin's avatar
Libretro-Admin committed
50
51
#  include <psp2/io/fcntl.h>
#  include <psp2/io/dirent.h>
Libretro-Admin's avatar
Libretro-Admin committed
52
#include <psp2/io/stat.h>
Libretro-Admin's avatar
Libretro-Admin committed
53
#else
Libretro-Admin's avatar
Libretro-Admin committed
54
55
56
57
58
59
60
#  if defined(PSP)
#    include <pspiofilemgr.h>
#  endif
#  include <sys/types.h>
#  include <sys/stat.h>
#  include <dirent.h>
#  include <unistd.h>
Libretro-Admin's avatar
Libretro-Admin committed
61
62
63
64
65
66
#endif

#ifdef __CELLOS_LV2__
#include <cell/cell_fs.h>
#endif

Libretro-Admin's avatar
Libretro-Admin committed
67
68
69
70
#if (defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)) || defined(__QNX__) || defined(PSP)
#include <unistd.h> /* stat() is defined here */
#endif

71
72
73
74
75
76
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif

Libretro-Admin's avatar
Libretro-Admin committed
77
78
79
struct RDIR
{
#if defined(_WIN32)
80
#if defined(LEGACY_WIN32)
Libretro-Admin's avatar
Libretro-Admin committed
81
   WIN32_FIND_DATA entry;
82
83
84
#else
   WIN32_FIND_DATAW entry;
#endif
Libretro-Admin's avatar
Libretro-Admin committed
85
   HANDLE directory;
Libretro-Admin's avatar
Updates    
Libretro-Admin committed
86
   bool next;
Libretro-Admin's avatar
Libretro-Admin committed
87
   char path[PATH_MAX_LENGTH];
Libretro-Admin's avatar
Libretro-Admin committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#elif defined(VITA) || defined(PSP)
   SceUID directory;
   SceIoDirent entry;
#elif defined(__CELLOS_LV2__)
   CellFsErrno error;
   int directory;
   CellFsDirent entry;
#else
   DIR *directory;
   const struct dirent *entry;
#endif
};

struct RDIR *retro_opendir(const char *name)
{
Libretro-Admin's avatar
Libretro-Admin committed
103
#if defined(_WIN32)
Libretro-Admin's avatar
Libretro-Admin committed
104
   char path_buf[1024];
105
106
107
   char *path_local   = NULL;
   wchar_t *path_wide = NULL;
   unsigned path_len;
Libretro-Admin's avatar
Libretro-Admin committed
108
#endif
109
   struct RDIR *rdir  = (struct RDIR*)calloc(1, sizeof(*rdir));
Libretro-Admin's avatar
Libretro-Admin committed
110
111
112
113
114

   if (!rdir)
      return NULL;

#if defined(_WIN32)
115
116
117
   (void)path_wide;
   (void)path_local;

Libretro-Admin's avatar
Libretro-Admin committed
118
   path_buf[0] = '\0';
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
   path_len = strlen(name);

   /* Non-NT platforms don't like extra slashes in the path */
   if (name[path_len - 1] == '\\')
      snprintf(path_buf, sizeof(path_buf), "%s*", name);
   else
      snprintf(path_buf, sizeof(path_buf), "%s\\*", name);

#if defined(LEGACY_WIN32)
   path_local      = utf8_to_local_string_alloc(path_buf);
   rdir->directory = FindFirstFile(path_local, &rdir->entry);

   if (path_local)
      free(path_local);
#else
   path_wide       = utf8_to_utf16_string_alloc(path_buf);
   rdir->directory = FindFirstFileW(path_wide, &rdir->entry);

   if (path_wide)
      free(path_wide);
#endif

Libretro-Admin's avatar
Libretro-Admin committed
141
142
#elif defined(VITA) || defined(PSP)
   rdir->directory = sceIoDopen(name);
Libretro-Admin's avatar
Libretro-Admin committed
143
#elif defined(_3DS)
144
   rdir->directory = !string_is_empty(name) ? opendir(name) : NULL;
Libretro-Admin's avatar
Libretro-Admin committed
145
   rdir->entry     = NULL;
Libretro-Admin's avatar
Libretro-Admin committed
146
#elif defined(__CELLOS_LV2__)
147
   rdir->error     = cellFsOpendir(name, &rdir->directory);
Libretro-Admin's avatar
Libretro-Admin committed
148
149
150
151
152
#else
   rdir->directory = opendir(name);
   rdir->entry     = NULL;
#endif

153
154
155
156
157
   if (rdir->directory)
      return rdir;

   free(rdir);
   return NULL;
Libretro-Admin's avatar
Libretro-Admin committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
}

bool retro_dirent_error(struct RDIR *rdir)
{
#if defined(_WIN32)
   return (rdir->directory == INVALID_HANDLE_VALUE);
#elif defined(VITA) || defined(PSP)
   return (rdir->directory < 0);
#elif defined(__CELLOS_LV2__)
   return (rdir->error != CELL_FS_SUCCEEDED);
#else
   return !(rdir->directory);
#endif
}

int retro_readdir(struct RDIR *rdir)
{
#if defined(_WIN32)
Libretro-Admin's avatar
Updates    
Libretro-Admin committed
176
   if(rdir->next)
177
#if defined(LEGACY_WIN32)
Libretro-Admin's avatar
Updates    
Libretro-Admin committed
178
      return (FindNextFile(rdir->directory, &rdir->entry) != 0);
179
180
181
#else
      return (FindNextFileW(rdir->directory, &rdir->entry) != 0);
#endif
Libretro-Admin's avatar
Libretro-Admin committed
182
183
184

   rdir->next = true;
   return (rdir->directory != INVALID_HANDLE_VALUE);
Libretro-Admin's avatar
Libretro-Admin committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#elif defined(VITA) || defined(PSP)
   return (sceIoDread(rdir->directory, &rdir->entry) > 0);
#elif defined(__CELLOS_LV2__)
   uint64_t nread;
   rdir->error = cellFsReaddir(rdir->directory, &rdir->entry, &nread);
   return (nread != 0);
#else
   return ((rdir->entry = readdir(rdir->directory)) != NULL);
#endif
}

const char *retro_dirent_get_name(struct RDIR *rdir)
{
#if defined(_WIN32)
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#if defined(LEGACY_WIN32)
   char *name_local = local_to_utf8_string_alloc(rdir->entry.cFileName);
   memset(rdir->entry.cFileName, 0, sizeof(rdir->entry.cFileName));
   strlcpy(rdir->entry.cFileName, name_local, sizeof(rdir->entry.cFileName));

   if (name_local)
      free(name_local);
#else
   char *name = utf16_to_utf8_string_alloc(rdir->entry.cFileName);
   memset(rdir->entry.cFileName, 0, sizeof(rdir->entry.cFileName));
   strlcpy((char*)rdir->entry.cFileName, name, sizeof(rdir->entry.cFileName));

   if (name)
      free(name);
#endif
   return (char*)rdir->entry.cFileName;
Libretro-Admin's avatar
Libretro-Admin committed
215
216
217
218
#elif defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__)
   return rdir->entry.d_name;
#else

219
   return rdir->entry->d_name;
Libretro-Admin's avatar
Libretro-Admin committed
220
221
222
#endif
}

Libretro-Admin's avatar
Libretro-Admin committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 *
 * retro_dirent_is_dir:
 * @rdir         : pointer to the directory entry.
 * @path         : path to the directory entry.
 *
 * Is the directory listing entry a directory?
 *
 * Returns: true if directory listing entry is
 * a directory, false if not.
 */
bool retro_dirent_is_dir(struct RDIR *rdir, const char *path)
{
#if defined(_WIN32)
Libretro-Admin's avatar
Libretro-Admin committed
237
   const WIN32_FIND_DATA *entry = (const WIN32_FIND_DATA*)&rdir->entry;
Libretro-Admin's avatar
Libretro-Admin committed
238
239
240
241
242
243
   return entry->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
#elif defined(PSP) || defined(VITA)
   const SceIoDirent *entry = (const SceIoDirent*)&rdir->entry;
#if defined(PSP)
   return (entry->d_stat.st_attr & FIO_SO_IFDIR) == FIO_SO_IFDIR;
#elif defined(VITA)
Libretro-Admin's avatar
Updates    
Libretro-Admin committed
244
   return SCE_S_ISDIR(entry->d_stat.st_mode);
Libretro-Admin's avatar
Libretro-Admin committed
245
246
247
248
#endif
#elif defined(__CELLOS_LV2__)
   CellFsDirent *entry = (CellFsDirent*)&rdir->entry;
   return (entry->d_type == CELL_FS_TYPE_DIRECTORY);
249
250
251
#else
   struct stat buf;
#if defined(DT_DIR)
Libretro-Admin's avatar
Libretro-Admin committed
252
253
254
   const struct dirent *entry = (const struct dirent*)rdir->entry;
   if (entry->d_type == DT_DIR)
      return true;
Libretro-Admin's avatar
Libretro-Admin committed
255
   /* This can happen on certain file systems. */
256
257
258
   if (!(entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK))
      return false;
#endif
Libretro-Admin's avatar
Libretro-Admin committed
259
   /* dirent struct doesn't have d_type, do it the slow way ... */
260
261
262
263
264
265
266
267
268
269
270
271
272
   if (stat(path, &buf) < 0)
      return false;
   return S_ISDIR(buf.st_mode);
#endif
}

void retro_dirent_include_hidden(struct RDIR *rdir, bool include_hidden)
{
#ifdef _WIN32
   if (include_hidden)
      rdir->entry.dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN;
   else
      rdir->entry.dwFileAttributes &= ~FILE_ATTRIBUTE_HIDDEN;
Libretro-Admin's avatar
Libretro-Admin committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#endif
}

void retro_closedir(struct RDIR *rdir)
{
   if (!rdir)
      return;

#if defined(_WIN32)
   if (rdir->directory != INVALID_HANDLE_VALUE)
      FindClose(rdir->directory);
#elif defined(VITA) || defined(PSP)
   sceIoDclose(rdir->directory);
#elif defined(__CELLOS_LV2__)
   rdir->error = cellFsClosedir(rdir->directory);
#else
   if (rdir->directory)
      closedir(rdir->directory);
#endif

   free(rdir);
}