]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
cg_build_path adds needless "/" character
authorIvana Hutarova Varekova <varekova@redhat.com>
Fri, 22 Jun 2012 13:38:34 +0000 (15:38 +0200)
committerIvana Hutarova Varekova <varekova@redhat.com>
Fri, 22 Jun 2012 13:38:34 +0000 (15:38 +0200)
    cg_build_path adds needless "/" character in cases when the input path
    ends/starts with character "/" itself. This character is added now only
in cases it is wanted and it was not set.

    Changelog
    - incorporate Jan's feedback - fix typo in patch description, use snprintf instead of sprintf
    - deal with the situation if directory name is empty as well
    - move the path concatenation functionality to a separate function

    Example:
    Old version:
    cg_build_path("", path, memory);
    returns /sys/fs/cgroup/memory//
    cg_build_path("/3", path, memory);
    returns /sys/fs/cgroup/memory//3/
    cg_build_path("3/", path, memory);
    returns /sys/fs/cgroup/memory/3//

    New version:
    cg_build_path("", path, memory);
    returns /sys/fs/cgroup/memory/
    cg_build_path("/3", path, memory);
    returns /sys/fs/cgroup/memory/3/
    cg_build_path("3/", path, memory);
    returns /sys/fs/cgroup/memory/3/

Signed-off-by: Ivana Hutarova Varekova <varekova@redhat.com>
Acked-by: Dhaval Giani <dhaval.giani@gmail.com>
src/api.c

index 29de77708c13f9283cefb91849163d331084026d..f050df708253fe353b5589798f26aaed72efc5e8 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -1099,6 +1099,20 @@ static inline pid_t cg_gettid(void)
        return syscall(__NR_gettid);
 }
 
+static char *cg_concat_path(const char *pref, const char *suf, char *path)
+{
+       if ((suf[strlen(suf)-1] == '/') ||
+               ((strlen(suf) == 0) && (pref[strlen(pref)-1] == '/'))) {
+               snprintf(path, FILENAME_MAX, "%s%s", pref,
+                       suf+((suf[0] == '/') ? 1 : 0));
+       } else {
+               snprintf(path, FILENAME_MAX, "%s%s/", pref,
+                       suf+((suf[0] == '/') ? 1 : 0));
+       }
+       path[FILENAME_MAX-1] = '\0';
+       return path;
+}
+
 
 /* Call with cg_mount_table_lock taken */
 /* path value have to have size at least FILENAME_MAX */
@@ -1125,9 +1139,7 @@ static char *cg_build_path_locked(const char *name, char *path,
 
                                /* FIXME: missing OOM check here! */
 
-                               snprintf(path, FILENAME_MAX, "%s%s/",
-                                       tmp, name);
-                               path[FILENAME_MAX-1] = '\0';
+                               cg_concat_path(tmp, name, path);
                                free(tmp);
                        }
                        return path;