Skip to content

Commit

Permalink
uxplay.cpp check files referenced in -key etc for write access
Browse files Browse the repository at this point in the history
  • Loading branch information
fduncanh committed Dec 7, 2023
1 parent ee3f67a commit 40da5d2
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion uxplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ void log(int level, const char* format, ...) {
#define LOGW(...) log(LOGGER_WARNING, __VA_ARGS__)
#define LOGE(...) log(LOGGER_ERR, __VA_ARGS__)

bool file_has_write_access (const char * filename) {
bool exists = false;
bool write = false;
#ifdef _WIN32
if ((exists = _access(filename, 0) != -1)) {
write = (_access(filename, 2) != -1);
}
#else
if ((exists = access(filename, F_OK) != -1)) {
write = (access(filename, W_OK) != -1);
}
#endif
if (!exists) {
FILE *fp = fopen(filename, "w");
if (fp) {
write = true;
fclose(fp);
remove(filename);
}
}
return write;
}

/* 95 byte png file with a 1x1 white square (single pixel): placeholder for coverart*/
static const unsigned char empty_image[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
Expand Down Expand Up @@ -950,6 +973,11 @@ static void parse_arguments (int argc, char *argv[]) {
video_dumpfile_name.erase();
video_dumpfile_name.append(argv[i]);
}
const char *fn = video_dumpfile_name.c_str();
if (!file_has_write_access(fn)) {
fprintf(stderr, "%s cannot be written to:\noption \"-vdmp <fn>\" must be to a file with write access\n", fn);
exit(1);
}
}
} else if (arg == "-admp") {
dump_audio = true;
Expand All @@ -969,11 +997,21 @@ static void parse_arguments (int argc, char *argv[]) {
audio_dumpfile_name.erase();
audio_dumpfile_name.append(argv[i]);
}
const char *fn = audio_dumpfile_name.c_str();
if (!file_has_write_access(fn)) {
fprintf(stderr, "%s cannot be written to:\noption \"-admp <fn>\" must be to a file with write access\n", fn);
exit(1);
}
}
} else if (arg == "-ca" ) {
if (option_has_value(i, argc, arg, argv[i+1])) {
coverart_filename.erase();
coverart_filename.append(argv[++i]);
const char *fn = coverart_filename.c_str();
if (!file_has_write_access(fn)) {
fprintf(stderr, "%s cannot be written to:\noption \"-ca <fn>\" must be to a file with write access\n", fn);
exit(1);
}
} else {
fprintf(stderr,"option -ca must be followed by a filename for cover-art output\n");
exit(1);
Expand Down Expand Up @@ -1010,6 +1048,11 @@ static void parse_arguments (int argc, char *argv[]) {
keyfile.erase();
if (i < argc - 1 && *argv[i+1] != '-') {
keyfile.append(argv[++i]);
const char * fn = keyfile.c_str();
if (!file_has_write_access(fn)) {
fprintf(stderr, "%s cannot be written to:\noption \"-key <fn>\" must be to a file with write access\n", fn);
exit(1);
}
} else {
fprintf(stderr, "option \"-key <fn>\" requires a path <fn> to a file for persistent key storage\n");
exit(1);
Expand All @@ -1018,7 +1061,12 @@ static void parse_arguments (int argc, char *argv[]) {
dacpfile.erase();
if (i < argc - 1 && *argv[i+1] != '-') {
dacpfile.append(argv[++i]);
} else {
const char *fn = dacpfile.c_str();
if (!file_has_write_access(fn)) {
fprintf(stderr, "%s cannot be written to:\noption \"-dacp <fn>\" must be to a file with write access\n", fn);
exit(1);
}
} else {
dacpfile.append(get_homedir());
dacpfile.append("/.uxplay.dacp");
}
Expand Down

0 comments on commit 40da5d2

Please sign in to comment.