[nautilus/antoniof/file-name-widget-patches: 5/5] file-utilities: Deduplicate max filename querying code



commit 116845d4fc46a5134c603e43634f7c2128082971
Author: António Fernandes <antoniof gnome org>
Date:   Thu Aug 2 11:33:48 2018 +0100

    file-utilities: Deduplicate max filename querying code
    
    A method to query maximum filename length has been introduced by
    commit a5520fd8930910ffa686232f3fc92325fe47c64e
    
    However, Ernestas has noticed that another implementation was already
    present in nautilus-file-operations.c
    
    This commit will replace both with a file-utilities function, which
    takes ideas from both implementations.

 src/nautilus-directory.c                   | 30 ----------------
 src/nautilus-directory.h                   | 13 -------
 src/nautilus-file-name-widget-controller.c |  6 ++--
 src/nautilus-file-operations.c             | 56 ++----------------------------
 src/nautilus-file-utilities.c              | 51 +++++++++++++++++++++++++++
 src/nautilus-file-utilities.h              | 15 ++++++++
 6 files changed, 73 insertions(+), 98 deletions(-)
---
diff --git a/src/nautilus-directory.c b/src/nautilus-directory.c
index 56750365c..ee8706bc9 100644
--- a/src/nautilus-directory.c
+++ b/src/nautilus-directory.c
@@ -694,36 +694,6 @@ nautilus_directory_new_file_from_filename (NautilusDirectory *directory,
                                                                                               self_owned);
 }
 
-glong
-nautilus_directory_get_max_child_name_length (NautilusDirectory *self)
-{
-    g_autoptr (GFile) location = NULL;
-    g_autofree gchar *path = NULL;
-    glong path_length;
-    glong name_max;
-    glong path_max;
-    glong result;
-
-    g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (self), -1);
-
-    location = nautilus_directory_get_location (self);
-    path = g_file_get_path (location);
-    path_length = strlen (path);
-
-    g_assert (path != NULL);
-
-    name_max = pathconf (path, _PC_NAME_MAX);
-    if (name_max == -1)
-    {
-        return -1;
-    }
-    path_max = pathconf (path, _PC_PATH_MAX);
-    /* Subtracting one from NAME_MAX, since the length excludes the null terminator. */
-    result = MAX (MIN (path_max - path_length, name_max - 1), 0);
-
-    return result;
-}
-
 static GList *
 nautilus_directory_provider_get_all (void)
 {
diff --git a/src/nautilus-directory.h b/src/nautilus-directory.h
index b9220c8e2..774514b1d 100644
--- a/src/nautilus-directory.h
+++ b/src/nautilus-directory.h
@@ -252,16 +252,3 @@ void               nautilus_directory_dump                     (NautilusDirector
 NautilusFile *     nautilus_directory_new_file_from_filename   (NautilusDirectory *directory,
                                                                 const char        *filename,
                                                                 gboolean           self_owned);
-
-/**
- * nautilus_directory_get_max_child_name_length:
- * @directory: a #NautilusDirectory
- *
- * Gets the maximum file name length for files inside @directory.
- *
- * This call does blocking I/O.
- *
- * Returns: The maximum file name length in bytes, -1 if the maximum length
- *          could not be determined or 0 if @directory path is too long.
- */
-glong nautilus_directory_get_max_child_name_length (NautilusDirectory *directory);
diff --git a/src/nautilus-file-name-widget-controller.c b/src/nautilus-file-name-widget-controller.c
index 30eb4687e..dfcdfbe7c 100644
--- a/src/nautilus-file-name-widget-controller.c
+++ b/src/nautilus-file-name-widget-controller.c
@@ -20,7 +20,7 @@
 #include <glib/gi18n.h>
 
 #include "nautilus-file-name-widget-controller.h"
-
+#include "nautilus-file-utilities.h"
 
 #define FILE_NAME_DUPLICATED_LABEL_TIMEOUT 500
 
@@ -78,11 +78,13 @@ nautilus_file_name_widget_controller_is_name_too_long (NautilusFileNameWidgetCon
 {
     NautilusFileNameWidgetControllerPrivate *priv;
     size_t name_length;
+    g_autoptr (GFile) location = NULL;
     glong max_name_length;
 
     priv = nautilus_file_name_widget_controller_get_instance_private (self);
     name_length = strlen (name);
-    max_name_length = nautilus_directory_get_max_child_name_length (priv->containing_directory);
+    location = nautilus_directory_get_location (priv->containing_directory);
+    max_name_length = nautilus_get_max_child_name_length_for_location (location);
 
     if (max_name_length == -1)
     {
diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c
index 589602cbb..5fa2b32a6 100644
--- a/src/nautilus-file-operations.c
+++ b/src/nautilus-file-operations.c
@@ -4077,56 +4077,6 @@ report_copy_progress (CopyMoveJob  *copy_job,
 }
 #pragma GCC diagnostic pop
 
-static int
-get_max_name_length (GFile *file_dir)
-{
-    int max_length;
-    char *dir;
-    long max_path;
-    long max_name;
-
-    max_length = -1;
-
-    if (!g_file_has_uri_scheme (file_dir, "file"))
-    {
-        return max_length;
-    }
-
-    dir = g_file_get_path (file_dir);
-    if (!dir)
-    {
-        return max_length;
-    }
-
-    max_path = pathconf (dir, _PC_PATH_MAX);
-    max_name = pathconf (dir, _PC_NAME_MAX);
-
-    if (max_name == -1 && max_path == -1)
-    {
-        max_length = -1;
-    }
-    else if (max_name == -1 && max_path != -1)
-    {
-        max_length = max_path - (strlen (dir) + 1);
-    }
-    else if (max_name != -1 && max_path == -1)
-    {
-        max_length = max_name;
-    }
-    else
-    {
-        int leftover;
-
-        leftover = max_path - (strlen (dir) + 1);
-
-        max_length = MIN (leftover, max_name);
-    }
-
-    g_free (dir);
-
-    return max_length;
-}
-
 #define FAT_FORBIDDEN_CHARACTERS "/:;*?\"<>\\|"
 
 static gboolean
@@ -4199,7 +4149,7 @@ get_unique_target_file (GFile      *src,
     NautilusFile *file;
     gboolean ignore_extension;
 
-    max_length = get_max_name_length (dest_dir);
+    max_length = nautilus_get_max_child_name_length_for_location (dest_dir);
 
     file = nautilus_file_get (src);
     ignore_extension = nautilus_file_is_directory (file);
@@ -4267,7 +4217,7 @@ get_target_file_for_link (GFile      *src,
     GFile *dest;
     int max_length;
 
-    max_length = get_max_name_length (dest_dir);
+    max_length = nautilus_get_max_child_name_length_for_location (dest_dir);
 
     dest = NULL;
     info = g_file_query_info (src,
@@ -7281,7 +7231,7 @@ create_task_thread_func (GTask        *task,
 
     handled_invalid_filename = FALSE;
 
-    max_length = get_max_name_length (job->dest_dir);
+    max_length = nautilus_get_max_child_name_length_for_location (job->dest_dir);
 
     verify_destination (common,
                         job->dest_dir,
diff --git a/src/nautilus-file-utilities.c b/src/nautilus-file-utilities.c
index 94198476d..66117891a 100644
--- a/src/nautilus-file-utilities.c
+++ b/src/nautilus-file-utilities.c
@@ -1317,6 +1317,57 @@ nautilus_get_common_filename_prefix_from_filenames (GList *filenames,
     return truncated;
 }
 
+glong
+nautilus_get_max_child_name_length_for_location (GFile *location)
+{
+    g_autofree gchar *path = NULL;
+    glong name_max;
+    glong path_max;
+    glong max_child_name_length;
+
+    g_return_val_if_fail (G_IS_FILE (location), -1);
+
+    if (!g_file_has_uri_scheme (location, "file"))
+    {
+        /* FIXME: Can we query length limits for non-"file://" locations? */
+        return -1;
+    }
+
+    path = g_file_get_path (location);
+
+    g_return_val_if_fail (path != NULL, -1);
+
+    name_max = pathconf (path, _PC_NAME_MAX);
+    path_max = pathconf (path, _PC_PATH_MAX);
+    max_child_name_length = -1;
+
+    if (name_max == -1)
+    {
+        if (path_max != -1)
+        {
+            /* We don't know the name max, but we know the name can't make the
+             * path longer than this.
+             * Subtracting 1 because PATH_MAX includes the terminating null
+             * character, as per limits.h(0P). */
+            max_child_name_length = MAX ((path_max - 1) - strlen (path), 0);
+        }
+    }
+    else
+    {
+        /* No need to subtract 1, because NAME_MAX excludes the terminating null
+         * character, as per limits.h(0P) */
+        max_child_name_length = name_max;
+        if (path_max != -1)
+        {
+            max_child_name_length = CLAMP ((path_max - 1) - strlen (path),
+                                           0,
+                                           max_child_name_length);
+        }
+    }
+
+    return max_child_name_length;
+}
+
 #if !defined (NAUTILUS_OMIT_SELF_CHECK)
 
 void
diff --git a/src/nautilus-file-utilities.h b/src/nautilus-file-utilities.h
index de21f742f..67df0e4d9 100644
--- a/src/nautilus-file-utilities.h
+++ b/src/nautilus-file-utilities.h
@@ -115,6 +115,21 @@ char * nautilus_get_common_filename_prefix (GList *file_list,
 char * nautilus_get_common_filename_prefix_from_filenames (GList *filename_list,
                                                            int    min_required_len);
 
+/**
+ * nautilus_get_max_child_name_for_location:
+ * @location: a #GFile representing a directory
+ *
+ * Gets the maximum file name length for files inside @location.
+ *
+ * This call does blocking I/O.
+ *
+ * Returns: The maximum file name length in bytes (not including the
+ *          terminating null of a filename string), -1 if the maximum length
+ *          could not be determined or 0 if @location path is too long.
+ */
+
+glong nautilus_get_max_child_name_length_for_location (GFile *location);
+
 void nautilus_ensure_extension_points (void);
 void nautilus_ensure_extension_builtins (void);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]