diff options
Diffstat (limited to 'src/file-utils.c')
-rw-r--r-- | src/file-utils.c | 282 |
1 files changed, 37 insertions, 245 deletions
diff --git a/src/file-utils.c b/src/file-utils.c index 64e3b30..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> @@ -122,100 +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) { @@ -241,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); @@ -262,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) { @@ -396,22 +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) @@ -521,26 +416,16 @@ build_uri (const char *base, ...) 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 @@ -626,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; @@ -641,10 +527,16 @@ 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; } @@ -667,7 +559,7 @@ 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* @@ -995,35 +887,6 @@ file_list__get_index_from_pattern (const char *line, } 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, int field_n) @@ -1149,28 +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) @@ -1193,55 +1034,6 @@ 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) @@ -1249,9 +1041,18 @@ uricmp (const char *uri1, 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; @@ -1259,9 +1060,9 @@ 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)); @@ -1299,15 +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) |