Introduce plat_file_check and use it for ROM existence checking

This commit is contained in:
Cacodemon345
2025-09-20 00:30:29 +06:00
parent 9cdb597760
commit fdbf4a066b
4 changed files with 59 additions and 9 deletions

View File

@@ -150,6 +150,7 @@ extern void plat_get_temp_dir(char *outbuf, uint8_t len);
extern void plat_get_vmm_dir(char *outbuf, size_t len);
extern void plat_init_rom_paths(void);
extern int plat_dir_check(char *path);
extern int plat_file_check(const char *path);
extern int plat_dir_create(char *path);
extern void *plat_mmap(size_t size, uint8_t executable);
extern void plat_munmap(void *ptr, size_t size);

View File

@@ -28,6 +28,7 @@
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include <stdbool.h>
#define HAVE_STDARG_H
#include <86box/86box.h>
#include "cpu.h"
@@ -129,6 +130,34 @@ rom_get_full_path(char *dest, const char *fn)
}
}
bool
rom_fcheck(const char *fn)
{
char temp[1024];
bool exists = false;
if (fn == NULL)
return false;
if (strstr(fn, "roms/") == fn) {
/* Relative path */
for (rom_path_t *rom_path = &rom_paths; rom_path != NULL; rom_path = rom_path->next) {
path_append_filename(temp, rom_path->path, fn + 5);
exists = plat_file_check(temp);
if (exists) {
return exists;
}
}
return exists;
} else {
/* Absolute path */
return plat_file_check(fn);
}
}
FILE *
rom_fopen(const char *fn, char *mode)
{
@@ -186,15 +215,7 @@ rom_getfile(char *fn, char *s, int size)
int
rom_present(const char *fn)
{
FILE *fp;
fp = rom_fopen(fn, "rb");
if (fp != NULL) {
(void) fclose(fp);
return 1;
}
return 0;
return rom_fcheck(fn);
}
uint8_t

View File

@@ -64,6 +64,8 @@
# include <sys/mman.h>
#endif
#include <sys/stat.h>
#ifdef Q_OS_OPENBSD
# include <pthread_np.h>
#endif
@@ -250,6 +252,22 @@ plat_dir_check(char *path)
return fi.isDir() ? 1 : 0;
}
int
plat_file_check(const char *path)
{
#ifdef _WIN32
auto data = QString::fromUtf8(path).toStdWString();
auto res = GetFileAttributesW(data.c_str());
return (res != INVALID_FILE_ATTRIBUTES && !(res & FILE_ATTRIBUTE_DIRECTORY));
#else
struct stat dummy;
if (stat(path, &dummy) < 0) {
return 0;
}
return !S_ISDIR(dummy.st_mode);
#endif
}
int
plat_getcwd(char *bufp, int max)
{

View File

@@ -412,6 +412,16 @@ plat_dir_check(char *path)
return S_ISDIR(dummy.st_mode);
}
int
plat_file_check(const char *path)
{
struct stat dummy;
if (stat(path, &dummy) < 0) {
return 0;
}
return !S_ISDIR(dummy.st_mode);
}
int
plat_dir_create(char *path)
{