[systemd-commits] 2 commits - src/shared src/test
Lennart Poettering
lennart at kemper.freedesktop.org
Sat May 24 20:21:50 PDT 2014
src/shared/path-util.c | 15 +++++----------
src/test/test-path-util.c | 22 ++++++++++++++++++++++
2 files changed, 27 insertions(+), 10 deletions(-)
New commits:
commit 6b56a65123720325ad5084b3e888bf6bfe89c148
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date: Sat May 24 12:01:13 2014 +0300
test-path-util: add tests for path_make_relative()
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index 0aa0bf1..9f8ae4d 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -170,10 +170,32 @@ static void test_fsck_exists(void) {
assert_se(fsck_exists("AbCdE") == -ENOENT);
}
+static void test_make_relative(void) {
+ char *result;
+
+ assert_se(path_make_relative("some/relative/path", "/some/path", &result) < 0);
+ assert_se(path_make_relative("/some/path", "some/relative/path", &result) < 0);
+
+#define test(from_dir, to_path, expected) { \
+ path_make_relative(from_dir, to_path, &result); \
+ assert_se(streq(result, expected)); \
+ free(result); \
+ }
+
+ test("/", "/", ".");
+ test("/", "/some/path", "some/path");
+ test("/some/path", "/some/path", ".");
+ test("/some/path", "/some/path/in/subdir", "in/subdir");
+ test("/some/path", "/", "../..");
+ test("/some/path", "/some/other/path", "../other/path");
+ test("//extra/////slashes///won't////fool///anybody//", "////extra///slashes////are/just///fine///", "../../../are/just/fine");
+}
+
int main(int argc, char **argv) {
test_path();
test_find_binary(argv[0]);
test_prefixes();
test_fsck_exists();
+ test_make_relative();
return 0;
}
commit 5216f599ff677a18016bfa1995d1f1e6a50e4a0b
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date: Sat May 24 12:01:12 2014 +0300
path-util: fix missing terminating zero
There was this code:
if (to_path_len > 0)
memcpy(p, to_path, to_path_len);
That didn't add the terminating zero, so the resulting string was
corrupt if this code path was taken.
Using strcpy() instead of memcpy() solves this issue, and also
simplifies the code.
Previously there was special handling for shortening "../../" to
"../..", but that has now been replaced by a path_kill_slashes() call,
which also makes the result prettier in case the input contains
redundant slashes that would otherwise be copied to the result.
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 2f38c10..5863429 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -135,7 +135,6 @@ char *path_make_absolute_cwd(const char *p) {
int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
char *r, *p;
unsigned n_parents;
- size_t to_path_len;
assert(from_dir);
assert(to_path);
@@ -168,6 +167,8 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
if (!r)
return -ENOMEM;
+ path_kill_slashes(r);
+
*_r = r;
return 0;
}
@@ -202,21 +203,15 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
n_parents++;
}
- to_path_len = strlen(to_path);
-
- r = malloc(n_parents * 3 + to_path_len);
+ r = malloc(n_parents * 3 + strlen(to_path) + 1);
if (!r)
return -ENOMEM;
for (p = r; n_parents > 0; n_parents--, p += 3)
memcpy(p, "../", 3);
- if (to_path_len > 0)
- memcpy(p, to_path, to_path_len);
- else
- /* "to_path" is a parent directory of "from_dir". Let's remove
- * the redundant slash from the end of the result. */
- *(p - 1) = 0;
+ strcpy(p, to_path);
+ path_kill_slashes(r);
*_r = r;
return 0;
More information about the systemd-commits
mailing list