Skip to content

Commit

Permalink
refactor: Clean up get_win_timezone_name() and _found_desktop_ini()
Browse files Browse the repository at this point in the history
  • Loading branch information
abelcheung committed Nov 26, 2023
1 parent 13989e2 commit 8603abd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
37 changes: 23 additions & 14 deletions src/utils-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ char *
get_win_timezone_name (void)
{
TIME_ZONE_INFORMATION tzinfo;
wchar_t *name;
WCHAR *name = NULL;
DWORD id;
char *ret;
GError *err = NULL;
char *result = NULL;
GError *err = NULL;

id = GetTimeZoneInformation (&tzinfo);
g_debug ("%s(): GetTimeZoneInformation() = %lu", __func__, id);

switch (id)
{
Expand All @@ -83,20 +84,27 @@ get_win_timezone_name (void)
name = tzinfo.DaylightName;
break;
default:
ret = g_win32_error_message (GetLastError ());
g_critical ("%s", ret);
g_free (ret);
return g_strdup (_("(Failed to retrieve timezone name)"));
{
char *msg = g_win32_error_message (GetLastError ());
g_critical ("%s(): %s", __func__, msg);
g_free (msg);
}
break;
}

ret = g_utf16_to_utf8 ( (const gunichar2 *) name, -1, NULL, NULL, &err);
if (err == NULL)
return ret;
if (name) {
result = g_utf16_to_utf8 ((const gunichar2 *) name,
-1, NULL, NULL, &err);
if (err) {
g_critical ("%s(): %s", __func__, err->message);
g_clear_error (&err);
}
}

g_warning ("%s", err->message);
g_error_free (err);
return NULL;
if (result)
return result;
else
return g_strdup (_("(Failed to retrieve timezone name)"));
}


Expand Down Expand Up @@ -232,7 +240,8 @@ windows_product_name (void)
&str_size
);

g_debug ("1st RegGetValueW(ProductName): status = %li, str_size = %lu", status, str_size);
g_debug ("1st RegGetValueW(ProductName): status = %li, str_size = %lu",
status, str_size);

if ((status != ERROR_SUCCESS) || (str_size > G_MAXSIZE))
return NULL;
Expand Down
32 changes: 17 additions & 15 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,34 +943,36 @@ _populate_index_file_list (GSList **list,
}


/*! Search for desktop.ini in folder for hint of recycle bin */
/**
* @brief Search for desktop.ini in folder for hint of recycle bin
* @param path The searched path
* @return `TRUE` if `desktop.ini` found to contain recycle bin
* identifier, `FALSE` otherwise
*/
static gboolean
found_desktop_ini (const char *path)
_found_desktop_ini (const char *path)
{
char *filename, *content, *found;

filename = g_build_filename (path, "desktop.ini", NULL);
if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
goto desktop_ini_error;
{
g_free (filename);
return FALSE;
}

/* assume desktop.ini is ASCII and not something spurious */
if (!g_file_get_contents (filename, &content, NULL, NULL))
goto desktop_ini_error;
if (g_file_get_contents (filename, &content, NULL, NULL))
/* Don't bother parsing, we don't use the content at all */
found = strstr (content, RECYCLE_BIN_CLSID);

/* Don't bother parsing, we don't use the content at all */
found = strstr (content, RECYCLE_BIN_CLSID);
g_free (content);
g_free (filename);
return (found != NULL);

desktop_ini_error:
g_free (filename);
return FALSE;
}


static _os_guess
guess_windows_ver (const metarecord meta)
_guess_windows_ver (const metarecord meta)
{
if (meta.type == RECYCLE_BIN_TYPE_DIR) {
/*
Expand Down Expand Up @@ -1037,7 +1039,7 @@ check_file_args (const char *path,
* last ditch effort: search for desktop.ini. Just print empty content
* representing empty recycle bin if found.
*/
if ( !*list && !found_desktop_ini (path) )
if ( !*list && !_found_desktop_ini (path) )
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT,
_("No files with name pattern '%s' "
Expand Down Expand Up @@ -1174,7 +1176,7 @@ _print_csv_header (metarecord meta)
else
#endif
{
_os_guess g = guess_windows_ver (meta);
_os_guess g = _guess_windows_ver (meta);

if (g == OS_GUESS_UNKNOWN)
g_print ("%s", _("OS detection failed"));
Expand Down

0 comments on commit 8603abd

Please sign in to comment.