diff options
Diffstat (limited to 'src/file-utils.c')
-rw-r--r-- | src/file-utils.c | 349 |
1 files changed, 38 insertions, 311 deletions
diff --git a/src/file-utils.c b/src/file-utils.c index a967cb4..44fed53 100644 --- a/src/file-utils.c +++ b/src/file-utils.c @@ -27,9 +27,11 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <strings.h> #include <ctype.h> #include <time.h> #include <unistd.h> +#include <strings.h> #include <sys/param.h> #include <sys/stat.h> #include <sys/time.h> @@ -42,10 +44,8 @@ #include "glib-utils.h" #include "fr-init.h" - #define SPECIAL_DIR(x) ((strcmp ((x), "..") == 0) || (strcmp ((x), ".") == 0)) - gboolean uri_exists (const char *uri) { @@ -62,7 +62,6 @@ uri_exists (const char *uri) return exists; } - static gboolean uri_is_filetype (const char *uri, GFileType file_type) @@ -94,21 +93,18 @@ uri_is_filetype (const char *uri, return result; } - gboolean uri_is_file (const char *uri) { return uri_is_filetype (uri, G_FILE_TYPE_REGULAR); } - gboolean uri_is_dir (const char *uri) { return uri_is_filetype (uri, G_FILE_TYPE_DIRECTORY); } - gboolean path_is_dir (const char *path) { @@ -128,103 +124,6 @@ uri_is_local (const char *uri) return strncmp (uri, "file://", 7) == 0; } - -gboolean -dir_is_empty (const char *uri) -{ - GFile *file; - GFileEnumerator *file_enum; - GFileInfo *info; - GError *error = NULL; - int n = 0; - - file = g_file_new_for_uri (uri); - - if (! g_file_query_exists (file, NULL)) { - g_object_unref (file); - return TRUE; - } - - file_enum = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error); - if (error != NULL) { - g_warning ("Failed to enumerate children of %s: %s", uri, error->message); - g_error_free (error); - g_object_unref (file_enum); - g_object_unref (file); - return TRUE; - } - - while ((n == 0) && ((info = g_file_enumerator_next_file (file_enum, NULL, &error)) != NULL)) { - if (error != NULL) { - g_warning ("Encountered error while enumerating children of %s (ignoring): %s", uri, error->message); - g_error_free (error); - } - else if (! SPECIAL_DIR (g_file_info_get_name (info))) - n++; - g_object_unref (info); - } - - g_object_unref (file); - g_object_unref (file_enum); - - return (n == 0); -} - - -gboolean -dir_contains_one_object (const char *uri) -{ - GFile *file; - GFileEnumerator *file_enum; - GFileInfo *info; - GError *err = NULL; - int n = 0; - - file = g_file_new_for_uri (uri); - - if (! g_file_query_exists (file, NULL)) { - g_object_unref (file); - return FALSE; - } - - file_enum = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &err); - if (err != NULL) { - g_warning ("Failed to enumerate children of %s: %s", uri, err->message); - g_error_free (err); - g_object_unref (file_enum); - g_object_unref (file); - return FALSE; - } - - while ((info = g_file_enumerator_next_file (file_enum, NULL, &err)) != NULL) { - const char *name; - - if (err != NULL) { - g_warning ("Encountered error while enumerating children of %s, ignoring: %s", uri, err->message); - g_error_free (err); - g_object_unref (info); - continue; - } - - name = g_file_info_get_name (info); - if (strcmp (name, ".") == 0 || strcmp (name, "..") == 0) { - g_object_unref (info); - continue; - } - - g_object_unref (info); - - if (++n > 1) - break; - } - - g_object_unref (file); - g_object_unref (file_enum); - - return (n == 1); -} - - char * get_dir_content_if_unique (const char *uri) { @@ -250,6 +149,7 @@ get_dir_content_if_unique (const char *uri) while ((info = g_file_enumerator_next_file (file_enum, NULL, &err)) != NULL) { const char *name; + char *new_name; if (err != NULL) { g_warning ("Failed to get info while enumerating children: %s", err->message); @@ -271,8 +171,10 @@ get_dir_content_if_unique (const char *uri) break; } - content_uri = build_uri (uri, name, NULL); + new_name = g_uri_escape_string (name, NULL, TRUE); + content_uri = build_uri (uri, new_name, NULL); g_object_unref (info); + g_free (new_name); } if (err != NULL) { @@ -286,7 +188,6 @@ get_dir_content_if_unique (const char *uri) return content_uri; } - /* Check whether the dirname is contained in filename */ gboolean path_in_path (const char *dirname, @@ -318,7 +219,6 @@ path_in_path (const char *dirname, && (filename[separator_position] == '/')); } - goffset get_file_size (const char *uri) { @@ -346,7 +246,6 @@ get_file_size (const char *uri) return size; } - goffset get_file_size_for_path (const char *path) { @@ -360,7 +259,6 @@ get_file_size_for_path (const char *path) return result; } - static time_t get_file_time_type (const char *uri, const char *type) @@ -390,14 +288,12 @@ get_file_time_type (const char *uri, return result; } - time_t get_file_mtime (const char *uri) { return get_file_time_type (uri, G_FILE_ATTRIBUTE_TIME_MODIFIED); } - time_t get_file_mtime_for_path (const char *path) { @@ -411,25 +307,6 @@ get_file_mtime_for_path (const char *path) return result; } - -time_t -get_file_ctime (const char *uri) -{ - return get_file_time_type (uri, G_FILE_ATTRIBUTE_TIME_CREATED); -} - - -gboolean -file_is_hidden (const gchar *name) -{ - if (name[0] != '.') return FALSE; - if (name[1] == '\0') return FALSE; - if ((name[1] == '.') && (name[2] == '\0')) return FALSE; - - return TRUE; -} - - /* like g_path_get_basename but does not warn about NULL and does not * alloc a new string. */ const gchar* file_name_from_path(const gchar *file_name) @@ -455,7 +332,6 @@ const gchar* file_name_from_path(const gchar *file_name) return base + 1; } - char * dir_name_from_path (const gchar *path) { @@ -479,7 +355,6 @@ dir_name_from_path (const gchar *path) return g_strndup (path + base + 1, last_char - base); } - gchar * remove_level_from_path (const gchar *path) { @@ -503,7 +378,6 @@ remove_level_from_path (const gchar *path) return new_path; } - char * remove_ending_separator (const char *path) { @@ -519,7 +393,6 @@ remove_ending_separator (const char *path) return g_strndup (path, copy_len); } - char * build_uri (const char *base, ...) { @@ -540,33 +413,21 @@ build_uri (const char *base, ...) return g_string_free (uri, FALSE); } - gchar * remove_extension_from_path (const gchar *path) { - int len; - int p; - const char *ptr = path; - char *new_path; - - if (! path) - return NULL; + const char *ext; - len = strlen (path); - if (len == 1) - return g_strdup (path); - - p = len - 1; - while ((p > 0) && (ptr[p] != '.')) - p--; - if (p == 0) - p = len; - new_path = g_strndup (path, (guint) p); + if (path == NULL) + return NULL; - return new_path; + ext = get_archive_filename_extension (path); + if (ext == NULL || strlen (ext) == strlen (path)) + return g_strdup (path); + else + return g_strndup (path, strlen (path) - strlen (ext)); } - gboolean make_directory_tree (GFile *dir, mode_t mode, @@ -603,7 +464,6 @@ make_directory_tree (GFile *dir, return success; } - gboolean ensure_dir_exists (const char *uri, mode_t mode, @@ -629,7 +489,6 @@ ensure_dir_exists (const char *uri, return TRUE; } - gboolean make_directory_tree_from_path (const char *path, mode_t mode, @@ -645,7 +504,6 @@ make_directory_tree_from_path (const char *path, return result; } - const char * get_file_extension (const char *filename) { @@ -653,6 +511,7 @@ get_file_extension (const char *filename) int len; int p; const char *ext; + const char *tar_exts[] = {".7z", ".br", ".bz", ".bz2", ".gz", ".lrz", ".lz", ".lzma", ".lzo", ".xz", ".Z", ".xst", NULL}; if (filename == NULL) return NULL; @@ -668,15 +527,20 @@ get_file_extension (const char *filename) return NULL; ext = filename + p; + p = 0; if (ext - 4 > filename) { const char *test = ext - 4; - if (strncmp (test, ".tar", 4) == 0) - ext = ext - 4; + if (strncmp (test, ".tar", 4) == 0) { + while (tar_exts[p] != NULL) { + if (strcmp (ext, tar_exts[p]) == 0) + ext = ext - 4; + p++; + } + } } return ext; } - gboolean file_extension_is (const char *filename, const char *ext) @@ -691,15 +555,13 @@ file_extension_is (const char *filename, return strcasecmp (filename + filename_l - ext_l, ext) == 0; } - gboolean is_mime_type (const char *mime_type, const char *pattern) { - return (strcasecmp (mime_type, pattern) == 0); + return g_content_type_equals (mime_type, pattern); } - const char* get_file_mime_type (const char *uri, gboolean fast_file_type) @@ -729,7 +591,6 @@ get_file_mime_type (const char *uri, return result; } - const char* get_file_mime_type_for_path (const char *filename, gboolean fast_file_type) @@ -744,7 +605,6 @@ get_file_mime_type_for_path (const char *filename, return mime_type; } - void path_list_free (GList *path_list) { @@ -753,7 +613,6 @@ path_list_free (GList *path_list) g_list_free_full (path_list, g_free); } - GList * path_list_dup (GList *path_list) { @@ -766,7 +625,6 @@ path_list_dup (GList *path_list) return g_list_reverse (new_list); } - guint64 get_dest_free_space (const char *path) { @@ -790,7 +648,6 @@ get_dest_free_space (const char *path) return freespace; } - static gboolean delete_directory_recursive (GFile *dir, GError **error) @@ -841,7 +698,6 @@ delete_directory_recursive (GFile *dir, return ! error_occurred; } - gboolean remove_directory (const char *uri) { @@ -860,7 +716,6 @@ remove_directory (const char *uri) return result; } - gboolean remove_local_directory (const char *path) { @@ -877,10 +732,8 @@ remove_local_directory (const char *path) return result; } - static const char *try_folder[] = { "cache", "~", "tmp", NULL }; - static char * ith_temp_folder_to_try (int n) { @@ -897,7 +750,6 @@ ith_temp_folder_to_try (int n) return g_strdup (folder); } - char * get_temp_work_dir (const char *parent_folder) { @@ -942,7 +794,6 @@ get_temp_work_dir (const char *parent_folder) return result; } - gboolean is_temp_work_dir (const char *dir) { @@ -967,7 +818,6 @@ is_temp_work_dir (const char *dir) return FALSE; } - gboolean is_temp_dir (const char *dir) { @@ -981,10 +831,8 @@ is_temp_dir (const char *dir) return is_temp_work_dir (dir); } - /* file list utils */ - gboolean file_list__match_pattern (const char *line, const char *pattern) @@ -1018,7 +866,6 @@ file_list__match_pattern (const char *line, return (*p == 0); } - int file_list__get_index_from_pattern (const char *line, const char *pattern) @@ -1039,37 +886,6 @@ file_list__get_index_from_pattern (const char *line, return -1; } - -char* -file_list__get_next_field (const char *line, - int start_from, - int field_n) -{ - const char *f_start, *f_end; - - line = line + start_from; - - f_start = line; - while ((*f_start == ' ') && (*f_start != *line)) - f_start++; - f_end = f_start; - - while ((field_n > 0) && (*f_end != 0)) { - if (*f_end == ' ') { - field_n--; - if (field_n != 0) { - while ((*f_end == ' ') && (*f_end != *line)) - f_end++; - f_start = f_end; - } - } else - f_end++; - } - - return g_strndup (f_start, f_end - f_start); -} - - char* file_list__get_prev_field (const char *line, int start_from, @@ -1097,7 +913,6 @@ file_list__get_prev_field (const char *line, return g_strndup (f_start + 1, f_end - f_start); } - gboolean check_permissions (const char *uri, int mode) @@ -1113,7 +928,6 @@ check_permissions (const char *uri, return result; } - gboolean check_file_permissions (GFile *file, int mode) @@ -1156,7 +970,6 @@ check_file_permissions (GFile *file, return result; } - gboolean is_program_in_path (const char *filename) { @@ -1183,7 +996,6 @@ is_program_in_path (const char *filename) return result; } - gboolean is_program_available (const char *filename, gboolean check) @@ -1191,7 +1003,6 @@ is_program_available (const char *filename, return ! check || is_program_in_path (filename); } - const char * get_home_uri (void) { @@ -1201,31 +1012,6 @@ get_home_uri (void) return home_uri; } - -char * -get_home_relative_uri (const char *partial_uri) -{ - return g_strconcat (get_home_uri (), - "/", - partial_uri, - NULL); -} - - -GFile * -get_home_relative_file (const char *partial_uri) -{ - GFile *file; - char *uri; - - uri = g_strconcat (get_home_uri (), "/", partial_uri, NULL); - file = g_file_new_for_uri (uri); - g_free (uri); - - return file; -} - - GFile * get_user_config_subdirectory (const char *child_name, gboolean create_child) @@ -1248,70 +1034,25 @@ get_user_config_subdirectory (const char *child_name, return file; } - -const char * -remove_host_from_uri (const char *uri) -{ - const char *idx, *sep; - - if (uri == NULL) - return NULL; - - idx = strstr (uri, "://"); - if (idx == NULL) - return uri; - idx += 3; - if (*idx == '\0') - return "/"; - sep = strstr (idx, "/"); - if (sep == NULL) - return idx; - return sep; -} - - -char * -get_uri_host (const char *uri) -{ - const char *idx; - - idx = strstr (uri, "://"); - if (idx == NULL) - return NULL; - idx = strstr (idx + 3, "/"); - if (idx == NULL) - return NULL; - return g_strndup (uri, (idx - uri)); -} - - -char * -get_uri_root (const char *uri) -{ - char *host; - char *root; - - host = get_uri_host (uri); - if (host == NULL) - return NULL; - root = g_strconcat (host, "/", NULL); - g_free (host); - - return root; -} - - int uricmp (const char *uri1, const char *uri2) { - return strcmp_null_tolerant (uri1, uri2); + return g_strcmp0 (uri1, uri2); } - +/** + * get_alternative_uri: + * @folder_uri: The URI of the containing folder + * @escaped_name: The URI-escaped name of the member to find a name for + * + * Tries to find an unused name for @escaped_name in @folder_uri. + * + * Returns: The full URI for the free slot (including directory) + */ char * -get_alternative_uri (const char *folder, - const char *name) +get_alternative_uri (const char *folder_uri, + const char *escaped_name) { char *new_uri = NULL; int n = 1; @@ -1319,16 +1060,15 @@ get_alternative_uri (const char *folder, do { g_free (new_uri); if (n == 1) - new_uri = g_strconcat (folder, "/", name, NULL); + new_uri = g_strconcat (folder_uri, "/", escaped_name, NULL); else - new_uri = g_strdup_printf ("%s/%s%%20(%d)", folder, name, n); + new_uri = g_strdup_printf ("%s/%s%%20(%d)", folder_uri, escaped_name, n); n++; } while (uri_exists (new_uri)); return new_uri; } - char * get_alternative_uri_for_uri (const char *uri) { @@ -1342,7 +1082,6 @@ get_alternative_uri_for_uri (const char *uri) return new_uri; } - GList * gio_file_list_dup (GList *l) { @@ -1352,7 +1091,6 @@ gio_file_list_dup (GList *l) return g_list_reverse (r); } - void gio_file_list_free (GList *l) { @@ -1362,17 +1100,6 @@ gio_file_list_free (GList *l) g_list_free (l); } - -GList * -gio_file_list_new_from_uri_list (GList *uris) -{ - GList *r = NULL, *scan; - for (scan = uris; scan; scan = scan->next) - r = g_list_prepend (r, g_file_new_for_uri ((char*)scan->data)); - return g_list_reverse (r); -} - - void g_key_file_save (GKeyFile *key_file, GFile *file) |