chiark / gitweb /
verbs: fix typo in error message
[elogind.git] / src / shared / util.c
index 364f61885b00a1b3f9a076a993b982f74468ba7e..06b6077843249ecd1b287aa4b50c097fd0433b49 100644 (file)
@@ -521,7 +521,7 @@ int safe_atod(const char *s, double *ret_d) {
 
 static size_t strcspn_escaped(const char *s, const char *reject) {
         bool escaped = false;
-        size_t n;
+        int n;
 
         for (n=0; s[n]; n++) {
                 if (escaped)
@@ -531,6 +531,7 @@ static size_t strcspn_escaped(const char *s, const char *reject) {
                 else if (strchr(reject, s[n]))
                         break;
         }
+
         /* if s ends in \, return index of previous char */
         return n - escaped;
 }
@@ -566,6 +567,11 @@ const char* split(const char **state, size_t *l, const char *separator, bool quo
                 *state = current++ + *l + 2;
         } else if (quoted) {
                 *l = strcspn_escaped(current, separator);
+                if (current[*l] && !strchr(separator, current[*l])) {
+                        /* unfinished escape */
+                        *state = current;
+                        return NULL;
+                }
                 *state = current + *l;
         } else {
                 *l = strcspn(current, separator);
@@ -1504,7 +1510,7 @@ char *ascii_strlower(char *t) {
         return t;
 }
 
-_pure_ static bool ignore_file_allow_backup(const char *filename) {
+_pure_ static bool hidden_file_allow_backup(const char *filename) {
         assert(filename);
 
         return
@@ -1521,13 +1527,13 @@ _pure_ static bool ignore_file_allow_backup(const char *filename) {
                 endswith(filename, ".swp");
 }
 
-bool ignore_file(const char *filename) {
+bool hidden_file(const char *filename) {
         assert(filename);
 
         if (endswith(filename, "~"))
                 return true;
 
-        return ignore_file_allow_backup(filename);
+        return hidden_file_allow_backup(filename);
 }
 
 int fd_nonblock(int fd, bool nonblock) {
@@ -1621,7 +1627,7 @@ int close_all_fds(const int except[], unsigned n_except) {
         while ((de = readdir(d))) {
                 int fd = -1;
 
-                if (ignore_file(de->d_name))
+                if (hidden_file(de->d_name))
                         continue;
 
                 if (safe_atoi(de->d_name, &fd) < 0)
@@ -2525,7 +2531,7 @@ int dir_is_empty(const char *path) {
                 if (!de)
                         return 1;
 
-                if (!ignore_file(de->d_name))
+                if (!hidden_file(de->d_name))
                         return 0;
         }
 }
@@ -3063,7 +3069,7 @@ static int rm_rf_internal(const char *path, bool only_dirs, bool delete_root, bo
         fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
         if (fd < 0) {
 
-                if (errno != ENOTDIR)
+                if (errno != ENOTDIR && errno != ELOOP)
                         return -errno;
 
                 if (!dangerous) {
@@ -3990,7 +3996,7 @@ const char *default_term_for_tty(const char *tty) {
 bool dirent_is_file(const struct dirent *de) {
         assert(de);
 
-        if (ignore_file(de->d_name))
+        if (hidden_file(de->d_name))
                 return false;
 
         if (de->d_type != DT_REG &&
@@ -4009,7 +4015,7 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
             de->d_type != DT_UNKNOWN)
                 return false;
 
-        if (ignore_file_allow_backup(de->d_name))
+        if (hidden_file_allow_backup(de->d_name))
                 return false;
 
         return endswith(de->d_name, suffix);
@@ -4251,6 +4257,23 @@ bool machine_name_is_valid(const char *s) {
         return true;
 }
 
+bool image_name_is_valid(const char *s) {
+        if (!filename_is_valid(s))
+                return false;
+
+        if (string_has_cc(s, NULL))
+                return false;
+
+        if (!utf8_is_valid(s))
+                return false;
+
+        /* Temporary files for atomically creating new files */
+        if (startswith(s, ".#"))
+                return false;
+
+        return true;
+}
+
 int pipe_eof(int fd) {
         struct pollfd pollfd = {
                 .fd = fd,
@@ -5903,7 +5926,7 @@ int on_ac_power(void) {
                 if (!de)
                         break;
 
-                if (ignore_file(de->d_name))
+                if (hidden_file(de->d_name))
                         continue;
 
                 device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
@@ -6986,18 +7009,18 @@ int tempfn_xxxxxx(const char *p, char **ret) {
          *         /foo/bar/waldo
          *
          * Into this:
-         *         /foo/bar/.waldoXXXXXX
+         *         /foo/bar/.#waldoXXXXXX
          */
 
         fn = basename(p);
         if (!filename_is_valid(fn))
                 return -EINVAL;
 
-        t = new(char, strlen(p) + 1 + 6 + 1);
+        t = new(char, strlen(p) + 2 + 6 + 1);
         if (!t)
                 return -ENOMEM;
 
-        strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn), "XXXXXX");
+        strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), "XXXXXX");
 
         *ret = path_kill_slashes(t);
         return 0;
@@ -7017,18 +7040,18 @@ int tempfn_random(const char *p, char **ret) {
          *         /foo/bar/waldo
          *
          * Into this:
-         *         /foo/bar/.waldobaa2a261115984a9
+         *         /foo/bar/.#waldobaa2a261115984a9
          */
 
         fn = basename(p);
         if (!filename_is_valid(fn))
                 return -EINVAL;
 
-        t = new(char, strlen(p) + 1 + 16 + 1);
+        t = new(char, strlen(p) + 2 + 16 + 1);
         if (!t)
                 return -ENOMEM;
 
-        x = stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn);
+        x = stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn);
 
         u = random_u64();
         for (i = 0; i < 16; i++) {
@@ -7053,14 +7076,14 @@ int tempfn_random_child(const char *p, char **ret) {
         /* Turns this:
          *         /foo/bar/waldo
          * Into this:
-         *         /foo/bar/waldo/.3c2b6219aa75d7d0
+         *         /foo/bar/waldo/.#3c2b6219aa75d7d0
          */
 
-        t = new(char, strlen(p) + 2 + 16 + 1);
+        t = new(char, strlen(p) + 3 + 16 + 1);
         if (!t)
                 return -ENOMEM;
 
-        x = stpcpy(stpcpy(t, p), "/.");
+        x = stpcpy(stpcpy(t, p), "/.#");
 
         u = random_u64();
         for (i = 0; i < 16; i++) {