Skip to content

Commit 3aa3a22

Browse files
committed
fs: generalize dir_exists to file_exists + use
+ use with src/playback
1 parent 96b5986 commit 3aa3a22

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/playback.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@
3737

3838
#include "playback.h"
3939

40-
#include <fcntl.h> // for S_IFDIR
40+
#include <errno.h> // for errno
4141
#include <stdbool.h> // for bool, false, true
42-
#include <stdio.h> // for snprintf, perror
42+
#include <stdio.h> // for snprintf
4343
#include <stdlib.h> // for free
4444
#include <string.h> // for strchr, strcmp, strdup, strstr
45-
#include <sys/stat.h> // for stat
4645

47-
#include "debug.h" // for log_msg, LOG_LEVEL_ERROR, LOG_LEVEL_INFO
46+
#include "debug.h" // for LOG_LEVEL_ERROR, LOG_LEVEL_INFO, MSG
4847
#include "keyboard_control.h" // for keycontrol_register_key, K_DOWN, K_LEFT
4948
#include "utils/color_out.h" // for color_printf, TBOLD, TERM_BOLD, TERM_R...
49+
#include "utils/fs.h" // for dir_exists
50+
#include "utils/misc.h" // for ug_strerror
5051
#include "utils/text.h" // for wrap_paragraph
5152

5253
struct module;
@@ -78,16 +79,15 @@ int playback_set_device(char *device_string, size_t buf_len, const char *optarg)
7879
*strchr(path, ':') = '\0';
7980
}
8081

81-
struct stat sb;
82-
if (stat(path, &sb) == -1) {
83-
perror(MOD_NAME "stat");
84-
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Cannot access file or directory '%s'\n", path);
85-
log_msg(LOG_LEVEL_INFO, MOD_NAME "Use '--playback help' to see usage.\n");
82+
if (!file_exists(path, FT_ANY)) {
83+
MSG(ERROR, "Cannot access file or directory: %s (%s)\n", path,
84+
ug_strerror(errno));
85+
MSG(INFO, "Use '--playback help' to see usage.\n");
8686
free(path);
8787
return -1;
8888
}
89-
90-
if (sb.st_mode & S_IFDIR || strstr(path, "video.info") != NULL) {
89+
if (file_exists(path, FT_DIRECTORY) ||
90+
strstr(path, "video.info") != NULL) {
9191
is_import = true;
9292
}
9393

src/utils/fs.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,22 @@ get_install_root(char exec_path[static MAX_PATH_SIZE])
192192
return true;
193193
}
194194

195-
static bool
196-
dir_exists(const char *path)
195+
bool
196+
file_exists(const char *path, enum check_file_type type)
197197
{
198198
struct stat sb;
199199
if (stat(path, &sb) == -1) {
200200
return false;
201201
}
202-
return S_ISDIR(sb.st_mode);
202+
switch (type) {
203+
case FT_ANY:
204+
return true;
205+
case FT_REGULAR:
206+
return S_ISREG(sb.st_mode);
207+
case FT_DIRECTORY:
208+
return S_ISDIR(sb.st_mode);
209+
}
210+
abort();
203211
}
204212

205213
/**
@@ -224,14 +232,14 @@ get_data_path()
224232
suffix) >= len) {
225233
abort(); // path truncated
226234
}
227-
if (dir_exists(path)) {
235+
if (file_exists(path, FT_DIRECTORY)) {
228236
MSG(VERBOSE, "Using data path %s\n", path);
229237
return path;
230238
}
231239
}
232240

233241
snprintf_ch(path, SRCDIR "%s", suffix);
234-
if (dir_exists(path)) {
242+
if (!file_exists(path, FT_DIRECTORY)) {
235243
MSG(VERBOSE, "Using data path %s\n", path);
236244
return path;
237245
}

src/utils/fs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@
5656
#include <cstdio>
5757
extern "C" {
5858
#else
59+
#include <stdbool.h>
5960
#include <stdio.h>
6061
#endif
6162

63+
enum check_file_type {
64+
FT_ANY,
65+
FT_REGULAR,
66+
FT_DIRECTORY,
67+
};
68+
bool file_exists(const char *path, enum check_file_type type);
6269
const char *get_temp_dir(void);
6370
FILE *get_temp_file(const char **filename);
6471
const char *get_data_path(void);

0 commit comments

Comments
 (0)