]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
path-util: split filename_is_valid() in two
authorLennart Poettering <lennart@poettering.net>
Fri, 3 Mar 2023 17:15:39 +0000 (18:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Jun 2023 16:45:01 +0000 (18:45 +0200)
Add a helper filename_part_is_valid() which does half of what
filename_is_valid() does: it checks for valid chars and length, but does
not filter out ".", ".." and "", as these are OK as parts of filenames,
just not alone.

src/basic/path-util.c
src/basic/path-util.h

index e1cc26f618bbe8f7abc19953e9f83d5106065f79..b97239624911bb24f35bb1de5e3cb5ee7a19c968 100644 (file)
@@ -1124,13 +1124,13 @@ int path_extract_directory(const char *path, char **ret) {
         return 0;
 }
 
-bool filename_is_valid(const char *p) {
+bool filename_part_is_valid(const char *p) {
         const char *e;
 
-        if (isempty(p))
-                return false;
+        /* Checks f the specified string is OK to be *part* of a filename. This is different from
+         * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
 
-        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
+        if (!p)
                 return false;
 
         e = strchrnul(p, '/');
@@ -1143,6 +1143,17 @@ bool filename_is_valid(const char *p) {
         return true;
 }
 
+bool filename_is_valid(const char *p) {
+
+        if (isempty(p))
+                return false;
+
+        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
+                return false;
+
+        return filename_part_is_valid(p);
+}
+
 bool path_is_valid_full(const char *p, bool accept_dot_dot) {
         if (isempty(p))
                 return false;
index 66879d1932abfcd81325efaaf755b72dd7c0caf3..af7f229908d48c49f61fce20792fd2739540bcff 100644 (file)
@@ -163,6 +163,7 @@ const char *last_path_component(const char *path);
 int path_extract_filename(const char *path, char **ret);
 int path_extract_directory(const char *path, char **ret);
 
+bool filename_part_is_valid(const char *p) _pure_;
 bool filename_is_valid(const char *p) _pure_;
 bool path_is_valid_full(const char *p, bool accept_dot_dot) _pure_;
 static inline bool path_is_valid(const char *p) {