This commit is contained in:
libretroadmin 2023-07-17 17:51:04 +02:00
parent 24a9210b56
commit 7edbfaf17b
5 changed files with 143 additions and 200 deletions

View file

@ -179,43 +179,6 @@ static int file_archive_parse_file_init(file_archive_transfer_t *state,
return state->backend->archive_parse_file_init(state, path);
}
/**
* file_archive_decompress_data_to_file:
* @path : filename path of archive.
* @size : output file size
* @checksum : CRC32 checksum from input data.
*
* Write data to file.
*
* Returns: true (1) on success, otherwise false (0).
**/
static int file_archive_decompress_data_to_file(
file_archive_transfer_t *transfer,
file_archive_file_handle_t *handle,
const char *path,
uint32_t size,
uint32_t checksum)
{
if (!handle)
return 0;
#if 0
handle->real_checksum = transfer->backend->stream_crc_calculate(
0, handle->data, size);
if (handle->real_checksum != checksum)
{
/* File CRC difers from archive CRC. */
printf("File CRC differs from archive CRC. File: 0x%x, Archive: 0x%x.\n",
(unsigned)handle->real_checksum, (unsigned)checksum);
}
#endif
if (!filestream_write_file(path, handle->data, size))
return 0;
return 1;
}
void file_archive_parse_file_iterate_stop(file_archive_transfer_t *state)
{
if (!state || !state->archive_file)
@ -512,9 +475,7 @@ bool file_archive_perform_mode(const char *path, const char *valid_exts,
userdata->transfer->context, &handle);
}while (ret == 0);
if (ret == -1 || !file_archive_decompress_data_to_file(
userdata->transfer, &handle, path,
size, crc32))
if (ret == -1 || !filestream_write_file(path, handle.data, size))
return false;
return true;

View file

@ -407,14 +407,16 @@ static void config_file_add_sub_conf(config_file_t *conf, char *path,
conf->path);
}
void config_file_add_reference(config_file_t *conf, char *path)
size_t config_file_add_reference(config_file_t *conf, char *path)
{
size_t len;
/* It is expected that the conf has it's path already set */
char short_path[PATH_MAX_LENGTH];
if (!conf->references)
conf->references = path_linked_list_new();
fill_pathname_abbreviated_or_relative(short_path, conf->path, path, sizeof(short_path));
len = fill_pathname_abbreviated_or_relative(short_path, conf->path, path, sizeof(short_path));
path_linked_list_add_path(conf->references, short_path);
return len;
}
static int config_file_load_internal(
@ -516,11 +518,9 @@ static bool config_file_parse_line(config_file_t *conf,
{
char *path = NULL;
bool include_found = string_starts_with_size(comment,
"include ",
STRLEN_CONST("include "));
"include ", STRLEN_CONST("include "));
bool reference_found = string_starts_with_size(comment,
"reference ",
STRLEN_CONST("reference "));
"reference ", STRLEN_CONST("reference "));
/* All comments except those starting with the include or
* reference directive are ignored */
@ -1331,69 +1331,66 @@ void config_set_path(config_file_t *conf, const char *entry, const char *val)
#endif
}
void config_set_double(config_file_t *conf, const char *key, double val)
size_t config_set_double(config_file_t *conf, const char *key, double val)
{
char buf[320];
#ifdef __cplusplus
snprintf(buf, sizeof(buf), "%f", (float)val);
size_t len = snprintf(buf, sizeof(buf), "%f", (float)val);
#elif defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
snprintf(buf, sizeof(buf), "%lf", val);
size_t len = snprintf(buf, sizeof(buf), "%lf", val);
#else
snprintf(buf, sizeof(buf), "%f", (float)val);
size_t len = snprintf(buf, sizeof(buf), "%f", (float)val);
#endif
config_set_string(conf, key, buf);
return len;
}
void config_set_float(config_file_t *conf, const char *key, float val)
size_t config_set_float(config_file_t *conf, const char *key, float val)
{
char buf[64];
snprintf(buf, sizeof(buf), "%f", val);
size_t len = snprintf(buf, sizeof(buf), "%f", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_int(config_file_t *conf, const char *key, int val)
size_t config_set_int(config_file_t *conf, const char *key, int val)
{
char buf[16];
snprintf(buf, sizeof(buf), "%d", val);
size_t len = snprintf(buf, sizeof(buf), "%d", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_uint(config_file_t *conf, const char *key, unsigned int val)
size_t config_set_uint(config_file_t *conf, const char *key, unsigned int val)
{
char buf[16];
snprintf(buf, sizeof(buf), "%u", val);
size_t len = snprintf(buf, sizeof(buf), "%u", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_hex(config_file_t *conf, const char *key, unsigned val)
size_t config_set_hex(config_file_t *conf, const char *key, unsigned val)
{
char buf[16];
snprintf(buf, sizeof(buf), "%x", val);
size_t len = snprintf(buf, sizeof(buf), "%x", val);
config_set_string(conf, key, buf);
return len;
}
void config_set_uint64(config_file_t *conf, const char *key, uint64_t val)
size_t config_set_uint64(config_file_t *conf, const char *key, uint64_t val)
{
char buf[32];
snprintf(buf, sizeof(buf), "%" PRIu64, val);
size_t len = snprintf(buf, sizeof(buf), "%" PRIu64, val);
config_set_string(conf, key, buf);
return len;
}
void config_set_char(config_file_t *conf, const char *key, char val)
size_t config_set_char(config_file_t *conf, const char *key, char val)
{
char buf[2];
snprintf(buf, sizeof(buf), "%c", val);
size_t len = snprintf(buf, sizeof(buf), "%c", val);
config_set_string(conf, key, buf);
}
/**
* config_set_bool:
* TODO/FIXME - could be turned into a trivial macro or removed
**/
void config_set_bool(config_file_t *conf, const char *key, bool val)
{
config_set_string(conf, key, val ? "true" : "false");
return len;
}
/**
@ -1406,32 +1403,32 @@ bool config_file_write(config_file_t *conf, const char *path, bool sort)
if (!conf)
return false;
if (!conf->modified)
return true;
if (!string_is_empty(path))
if (conf->modified)
{
void* buf = NULL;
FILE *file = (FILE*)fopen_utf8(path, "wb");
if (!file)
return false;
if (string_is_empty(path))
config_file_dump(conf, stdout, sort);
else
{
void* buf = NULL;
FILE *file = (FILE*)fopen_utf8(path, "wb");
if (!file)
return false;
buf = calloc(1, 0x4000);
setvbuf(file, (char*)buf, _IOFBF, 0x4000);
buf = calloc(1, 0x4000);
setvbuf(file, (char*)buf, _IOFBF, 0x4000);
config_file_dump(conf, file, sort);
config_file_dump(conf, file, sort);
if (file != stdout)
fclose(file);
if (buf)
free(buf);
if (file != stdout)
fclose(file);
if (buf)
free(buf);
/* Only update modified flag if config file
* is actually written to disk */
conf->modified = false;
/* Only update modified flag if config file
* is actually written to disk */
conf->modified = false;
}
}
else
config_file_dump(conf, stdout, sort);
return true;
}

View file

@ -198,9 +198,9 @@ const char *path_get_archive_delim(const char *path)
string_to_lower(buf);
/* Check if this is a '.zip', '.apk' or '.7z' file */
if (string_is_equal(buf, ".zip") ||
string_is_equal(buf, ".apk") ||
string_is_equal(buf + 1, ".7z"))
if ( string_is_equal(buf, ".zip")
|| string_is_equal(buf, ".apk")
|| string_is_equal(buf + 1, ".7z"))
return delim;
}
else if (delim - path > 3)
@ -372,17 +372,12 @@ char *find_last_slash(const char *str)
* Assumes path is a directory. Appends a slash
* if not already there.
**/
void fill_pathname_slash(char *path, size_t size)
size_t fill_pathname_slash(char *path, size_t size)
{
size_t path_len;
const char *last_slash = find_last_slash(path);
if (!last_slash)
{
strlcat(path, PATH_DEFAULT_SLASH(), size);
return;
}
return strlcat(path, PATH_DEFAULT_SLASH(), size);
path_len = strlen(path);
/* Try to preserve slash type. */
if (last_slash != (path + path_len - 1))
@ -390,6 +385,7 @@ void fill_pathname_slash(char *path, size_t size)
path[ path_len] = last_slash[0];
path[++path_len] = '\0';
}
return path_len;
}
/**
@ -412,12 +408,11 @@ void fill_pathname_slash(char *path, size_t size)
size_t fill_pathname_dir(char *in_dir, const char *in_basename,
const char *replace, size_t size)
{
const char *base = NULL;
fill_pathname_slash(in_dir, size);
base = path_basename(in_basename);
strlcat(in_dir, base, size);
return strlcat(in_dir, replace, size);
size_t _len = fill_pathname_slash(in_dir, size);
const char *base = path_basename(in_basename);
_len += strlcpy(in_dir + _len, base, size - _len);
_len += strlcpy(in_dir + _len, replace, size - _len);
return _len;
}
/**
@ -542,14 +537,14 @@ void fill_pathname_parent_dir(char *out_dir,
size_t fill_dated_filename(char *out_filename,
const char *ext, size_t size)
{
time_t cur_time = time(NULL);
size_t _len;
struct tm tm_;
time_t cur_time = time(NULL);
rtime_localtime(&cur_time, &tm_);
strftime(out_filename, size,
_len = strftime(out_filename, size,
"RetroArch-%m%d-%H%M%S", &tm_);
return strlcat(out_filename, ext, size);
_len += strlcpy(out_filename + _len, ext, size - _len);
return _len;
}
/**
@ -570,21 +565,24 @@ size_t fill_dated_filename(char *out_filename,
size_t fill_str_dated_filename(char *out_filename,
const char *in_str, const char *ext, size_t size)
{
char format[NAME_MAX_LENGTH];
struct tm tm_;
char format[NAME_MAX_LENGTH];
size_t _len = 0;
time_t cur_time = time(NULL);
rtime_localtime(&cur_time, &tm_);
strlcpy(out_filename, in_str, size);
_len = strlcpy(out_filename, in_str, size);
if (string_is_empty(ext))
{
strftime(format, sizeof(format), "-%y%m%d-%H%M%S", &tm_);
return strlcat(out_filename, format, size);
_len += strlcpy(out_filename + _len, format, size - _len);
}
strftime(format, sizeof(format), "-%y%m%d-%H%M%S.", &tm_);
strlcat(out_filename, format, size);
return strlcat(out_filename, ext, size);
else
{
strftime(format, sizeof(format), "-%y%m%d-%H%M%S.", &tm_);
_len += strlcpy(out_filename + _len, format, size - _len);
_len += strlcpy(out_filename + _len, ext, size - _len);
}
return _len;
}
/**
@ -654,18 +652,12 @@ void path_parent_dir(char *path, size_t len)
**/
const char *path_basename(const char *path)
{
/* We cut at the first compression-related hash */
const char *delim = path_get_archive_delim(path);
if (delim)
return delim + 1;
{
/* We cut at the last slash */
const char *last = find_last_slash(path);
if (last)
return last + 1;
}
/* We cut either at the first compression-related hash,
* or we cut at the last slash */
const char *ptr = NULL;
if ( (ptr = path_get_archive_delim(path))
|| (ptr = find_last_slash(path)))
return ptr + 1;
return path;
}
@ -698,24 +690,23 @@ const char *path_basename_nocompression(const char *path)
**/
bool path_is_absolute(const char *path)
{
if (string_is_empty(path))
return false;
if (path[0] == '/')
return true;
#if defined(_WIN32)
/* Many roads lead to Rome...
* Note: Drive letter can only be 1 character long */
return ( string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\"))
|| string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/"))
|| string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\")));
#elif defined(__wiiu__) || defined(VITA)
if (!string_is_empty(path))
{
const char *seperator = strchr(path, ':');
return (seperator && (seperator[1] == '/'));
}
if (path[0] == '/')
return true;
#if defined(_WIN32)
/* Many roads lead to Rome...
* Note: Drive letter can only be 1 character long */
return ( string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\"))
|| string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/"))
|| string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\")));
#elif defined(__wiiu__) || defined(VITA)
{
const char *seperator = strchr(path, ':');
return (seperator && (seperator[1] == '/'));
}
#endif
}
return false;
}
@ -741,7 +732,7 @@ char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks)
{
#if !defined(RARCH_CONSOLE) && defined(RARCH_INTERNAL)
#ifdef _WIN32
char *ret = NULL;
char *ret = NULL;
wchar_t *rel_path = utf8_to_utf16_string_alloc(buf);
if (rel_path)
@ -795,13 +786,13 @@ char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks)
{
size_t len;
/* rebase on working directory */
if (!getcwd(tmp, PATH_MAX_LENGTH-1))
if (!getcwd(tmp, PATH_MAX_LENGTH - 1))
return NULL;
len = strlen(tmp);
t += len;
t += len;
if (tmp[len-1] != '/')
if (tmp[len - 1] != '/')
tmp[t++] = '/';
if (string_is_empty(buf))
@ -832,8 +823,8 @@ char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks)
return NULL;
/* delete previous segment in tmp by adjusting size t
* tmp[t-1] == '/', find '/' before that */
t = t-2;
* tmp[t - 1] == '/', find '/' before that */
t -= 2;
while (tmp[t] != '/')
t--;
t++;
@ -845,7 +836,7 @@ char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks)
else
{
/* fail when truncating */
if (t + next-p+1 > PATH_MAX_LENGTH-1)
if (t + next - p + 1 > PATH_MAX_LENGTH - 1)
return NULL;
while (p <= next)
@ -964,11 +955,13 @@ void fill_pathname_resolve_relative(char *out_path,
size_t fill_pathname_join(char *out_path,
const char *dir, const char *path, size_t size)
{
size_t _len = 0;
if (out_path != dir)
strlcpy(out_path, dir, size);
_len = strlcpy(out_path, dir, size);
if (*out_path)
fill_pathname_slash(out_path, size);
return strlcat(out_path, path, size);
_len = fill_pathname_slash(out_path, size);
_len += strlcpy(out_path + _len, path, size - _len);
return _len;
}
/**
@ -1013,7 +1006,8 @@ size_t fill_pathname_join_special(char *out_path,
}
}
return strlcat(out_path, path, size);
len += strlcpy(out_path + len, path, size - len);
return len;
}
size_t fill_pathname_join_special_ext(char *out_path,
@ -1021,12 +1015,12 @@ size_t fill_pathname_join_special_ext(char *out_path,
const char *last, const char *ext,
size_t size)
{
fill_pathname_join(out_path, dir, path, size);
size_t _len = fill_pathname_join(out_path, dir, path, size);
if (*out_path)
fill_pathname_slash(out_path, size);
strlcat(out_path, last, size);
return strlcat(out_path, ext, size);
_len = fill_pathname_slash(out_path, size);
_len += strlcpy(out_path + _len, last, size - _len);
_len += strlcpy(out_path + _len, ext, size - _len);
return _len;
}
/**
@ -1043,19 +1037,19 @@ size_t fill_pathname_join_special_ext(char *out_path,
size_t fill_pathname_join_delim(char *out_path, const char *dir,
const char *path, const char delim, size_t size)
{
size_t copied;
/* behavior of strlcpy is undefined if dst and src overlap */
size_t _len;
/* Behavior of strlcpy is undefined if dst and src overlap */
if (out_path == dir)
copied = strlen(dir);
_len = strlen(dir);
else
copied = strlcpy(out_path, dir, size);
_len = strlcpy(out_path, dir, size);
out_path[copied] = delim;
out_path[copied+1] = '\0';
out_path[_len] = delim;
out_path[_len+1] = '\0';
if (path)
return strlcat(out_path, path, size);
return copied;
return _len;
}
size_t fill_pathname_expand_special(char *out_path,
@ -1163,12 +1157,12 @@ size_t fill_pathname_abbreviate_special(char *out_path,
*
* Leaf function.
*
* Changes the slashes to the correct kind for the os
* Changes the slashes to the correct kind for the OS
* So forward slash on linux and backslash on Windows
**/
void pathname_conform_slashes_to_os(char *path)
{
/* Conform slashes to os standard so we get proper matching */
/* Conform slashes to OS standard so we get proper matching */
char *p;
for (p = path; *p; p++)
if (*p == '/' || *p == '\\')
@ -1186,7 +1180,7 @@ void pathname_conform_slashes_to_os(char *path)
**/
void pathname_make_slashes_portable(char *path)
{
/* Conform slashes to os standard so we get proper matching */
/* Conform slashes to OS standard so we get proper matching */
char *p;
for (p = path; *p; p++)
if (*p == '/' || *p == '\\')

View file

@ -136,7 +136,7 @@ config_file_t *config_file_new_from_path_to_string(const char *path);
**/
void config_file_free(config_file_t *conf);
void config_file_add_reference(config_file_t *conf, char *path);
size_t config_file_add_reference(config_file_t *conf, char *path);
bool config_file_deinitialize(config_file_t *conf);
@ -291,24 +291,17 @@ bool config_get_bool(config_file_t *conf, const char *entry, bool *in);
/* Setters. Similar to the getters.
* Will not write to entry if the entry was obtained from an #include. */
void config_set_double(config_file_t *conf, const char *entry, double value);
void config_set_float(config_file_t *conf, const char *entry, float value);
void config_set_int(config_file_t *conf, const char *entry, int val);
void config_set_hex(config_file_t *conf, const char *entry, unsigned val);
void config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
void config_set_char(config_file_t *conf, const char *entry, char val);
size_t config_set_double(config_file_t *conf, const char *entry, double value);
size_t config_set_float(config_file_t *conf, const char *entry, float value);
size_t config_set_int(config_file_t *conf, const char *entry, int val);
size_t config_set_hex(config_file_t *conf, const char *entry, unsigned val);
size_t config_set_uint64(config_file_t *conf, const char *entry, uint64_t val);
size_t config_set_char(config_file_t *conf, const char *entry, char val);
size_t config_set_uint(config_file_t *conf, const char *key, unsigned int val);
void config_set_path(config_file_t *conf, const char *entry, const char *val);
void config_set_string(config_file_t *conf, const char *entry, const char *val);
void config_unset(config_file_t *conf, const char *key);
void config_set_path(config_file_t *conf, const char *entry, const char *val);
/**
* config_set_bool:
* TODO/FIXME - could be turned into a trivial macro or removed
**/
void config_set_bool(config_file_t *conf, const char *entry, bool val);
void config_set_uint(config_file_t *conf, const char *key, unsigned int val);
/**
* config_file_write:

View file

@ -323,10 +323,10 @@ size_t fill_dated_filename(char *out_filename,
* Hidden non-leaf function cost:
* - Calls time
* - Calls rtime_localtime()
* - Calls strlcpy
* - Calls strlcpy 2x
* - Calls string_is_empty()
* - Calls strftime
* - Calls strlcat at least 2x
* - Calls strlcat
*
* @return Length of the string copied into @out_path
**/
@ -369,7 +369,7 @@ char *find_last_slash(const char *str);
* Hidden non-leaf function cost:
* - Calls fill_pathname_slash()
* - Calls path_basename()
* - Calls strlcat 2x
* - Calls strlcpy 2x
**/
size_t fill_pathname_dir(char *in_dir, const char *in_basename,
const char *replace, size_t size);
@ -470,9 +470,8 @@ void fill_pathname_resolve_relative(char *out_path, const char *in_refpath,
* between directory and path.
*
* Hidden non-leaf function cost:
* - calls strlcpy
* - calls strlcpy at least once
* - calls fill_pathname_slash()
* - calls strlcat
*
* Deprecated. Use fill_pathname_join_special() instead
* if you can ensure @dir != @out_path
@ -499,9 +498,8 @@ size_t fill_pathname_join(char *out_path, const char *dir,
* between directory and path.
*
* Hidden non-leaf function cost:
* - calls strlcpy
* - calls strlcpy 2x
* - calls find_last_slash()
* - calls strlcat
*
* @return Length of the string copied into @out_path
**/
@ -627,7 +625,7 @@ void path_basedir_wrapper(char *path);
* - can call strlcat once if it returns false
* - calls strlen
**/
void fill_pathname_slash(char *path, size_t size);
size_t fill_pathname_slash(char *path, size_t size);
#if !defined(RARCH_CONSOLE) && defined(RARCH_INTERNAL)
void fill_pathname_application_path(char *buf, size_t size);