Change gitfo_prettify_dir_path() and gitfo_prettify_file_path() behavior Those functions now return prettified rooted path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
diff --git a/src/fileops.c b/src/fileops.c
index 4ed53bc..4123909 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -443,22 +443,29 @@ int gitfo_dirent(
return GIT_SUCCESS;
}
-#ifdef GIT_WIN32
-static int is_windows_rooted_path(const char *path)
+int retrieve_path_root_offset(const char *path)
{
+ int offset = 0;
+
+#ifdef GIT_WIN32
+
/* Does the root of the path look like a windows drive ? */
if (isalpha(path[0]) && (path[1] == ':'))
- return GIT_SUCCESS;
+ offset += 2;
+
+#endif
+
+ if (*(path + offset) == '/')
+ return offset;
return GIT_ERROR;
}
-#endif
int gitfo_mkdir_recurs(const char *path, int mode)
{
- int error;
+ int error, root_path_offset;
char *pp, *sp;
char *path_copy = git__strdup(path);
@@ -468,12 +475,9 @@ int gitfo_mkdir_recurs(const char *path, int mode)
error = GIT_SUCCESS;
pp = path_copy;
-#ifdef GIT_WIN32
-
- if (!is_windows_rooted_path(pp))
- pp += 2; /* Skip the drive name (eg. C: or D:) */
-
-#endif
+ root_path_offset = retrieve_path_root_offset(pp);
+ if (root_path_offset > 0)
+ pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */
while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != 0) {
if (sp != pp && gitfo_isdir(path_copy) < GIT_SUCCESS) {
@@ -504,6 +508,8 @@ static int retrieve_previous_path_component_start(const char *path)
len = strlen(path);
offset = len - 1;
+ //TODO: Deal with Windows rooted path
+
/* Skip leading slash */
if (path[start] == '/')
start++;
@@ -522,15 +528,25 @@ static int retrieve_previous_path_component_start(const char *path)
return offset;
}
-int gitfo_prettify_dir_path(char *buffer_out, const char *path)
+int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path)
{
- int len = 0, segment_len, only_dots;
+ int len = 0, segment_len, only_dots, root_path_offset, error = GIT_SUCCESS;
char *current;
const char *buffer_out_start, *buffer_end;
- buffer_out_start = buffer_out;
current = (char *)path;
buffer_end = path + strlen(path);
+ buffer_out_start = buffer_out;
+
+ root_path_offset = retrieve_path_root_offset(path);
+ if (root_path_offset < 0) {
+ error = gitfo_getcwd(buffer_out, size);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ len = strlen(buffer_out);
+ buffer_out += len;
+ }
while (current < buffer_end) {
/* Prevent multiple slashes from being added to the output */
@@ -543,7 +559,7 @@ int gitfo_prettify_dir_path(char *buffer_out, const char *path)
segment_len = 0;
/* Copy path segment to the output */
- while (current < buffer_end && *current !='/')
+ while (current < buffer_end && *current != '/')
{
only_dots &= (*current == '.');
*buffer_out++ = *current++;
@@ -568,7 +584,9 @@ int gitfo_prettify_dir_path(char *buffer_out, const char *path)
*buffer_out ='\0';
len = retrieve_previous_path_component_start(buffer_out_start);
- if (len < GIT_SUCCESS)
+
+ /* Are we escaping out of the root dir? */
+ if (len < 0)
return GIT_EINVALIDPATH;
buffer_out = (char *)buffer_out_start + len;
@@ -576,7 +594,7 @@ int gitfo_prettify_dir_path(char *buffer_out, const char *path)
}
/* Guard against potential multiple dot path traversal (cf http://cwe.mitre.org/data/definitions/33.html) */
- if (only_dots &&segment_len > 0)
+ if (only_dots && segment_len > 0)
return GIT_EINVALIDPATH;
*buffer_out++ = '/';
@@ -588,20 +606,24 @@ int gitfo_prettify_dir_path(char *buffer_out, const char *path)
return GIT_SUCCESS;
}
-int gitfo_prettify_file_path(char *buffer_out, const char *path)
+int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path)
{
int error, path_len, i;
const char* pattern = "/..";
path_len = strlen(path);
+ /* Let's make sure the filename isn't empty nor a dot */
+ if (path_len == 0 || (path_len == 1 && *path == '.'))
+ return GIT_EINVALIDPATH;
+
/* Let's make sure the filename doesn't end with "/", "/." or "/.." */
for (i = 1; path_len > i && i < 4; i++) {
if (!strncmp(path + path_len - i, pattern, i))
return GIT_EINVALIDPATH;
}
- error = gitfo_prettify_dir_path(buffer_out, path);
+ error = gitfo_prettify_dir_path(buffer_out, size, path);
if (error < GIT_SUCCESS)
return error;
diff --git a/src/fileops.h b/src/fileops.h
index c2c038b..ce236f6 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -163,12 +163,13 @@ extern int gitfo_getcwd(char *buffer_out, size_t size);
* the file system perspective.
*
* @param buffer_out buffer to populate with the normalized path.
+ * @param size buffer size.
* @param path directory path to clean up.
* @return
* - GIT_SUCCESS on success;
* - GIT_ERROR when the input path is invalid or escapes the current directory.
*/
-int gitfo_prettify_dir_path(char *buffer_out, const char *path);
+int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path);
/**
* Clean up a provided absolute or relative file path.
@@ -185,11 +186,13 @@ int gitfo_prettify_dir_path(char *buffer_out, const char *path);
* the file system perspective.
*
* @param buffer_out buffer to populate with the normalized path.
+ * @param size buffer size.
* @param path file path to clean up.
* @return
* - GIT_SUCCESS on success;
* - GIT_ERROR when the input path is invalid or escapes the current directory.
*/
-int gitfo_prettify_file_path(char *buffer_out, const char *path);
+int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path);
+int retrieve_path_root_offset(const char *path);
#endif /* INCLUDE_fileops_h__ */
diff --git a/src/repository.c b/src/repository.c
index 1329694..91b95a8 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -66,7 +66,7 @@ static int assign_repository_dirs(
if (git_dir == NULL)
return GIT_ENOTFOUND;
- error = gitfo_prettify_dir_path(path_aux, git_dir);
+ error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir);
if (error < GIT_SUCCESS)
return error;
@@ -81,7 +81,7 @@ static int assign_repository_dirs(
if (git_object_directory == NULL)
git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
else {
- error = gitfo_prettify_dir_path(path_aux, git_object_directory);
+ error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory);
if (error < GIT_SUCCESS)
return error;
}
@@ -95,7 +95,7 @@ static int assign_repository_dirs(
if (git_work_tree == NULL)
repo->is_bare = 1;
else {
- error = gitfo_prettify_dir_path(path_aux, git_work_tree);
+ error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree);
if (error < GIT_SUCCESS)
return error;
@@ -108,7 +108,7 @@ static int assign_repository_dirs(
if (git_index_file == NULL)
git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE);
else {
- error = gitfo_prettify_file_path(path_aux, git_index_file);
+ error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file);
if (error < GIT_SUCCESS)
return error;
}
@@ -403,7 +403,7 @@ static int repo_init_find_dir(repo_init *results, const char* path)
char temp_path[GIT_PATH_MAX];
int error = GIT_SUCCESS;
- error = gitfo_prettify_dir_path(temp_path, path);
+ error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path);
if (error < GIT_SUCCESS)
return error;
diff --git a/tests/t00-core.c b/tests/t00-core.c
index 08e42ec..d5fa07e 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -151,187 +151,204 @@ BEGIN_TEST(path2, "get the latest component in a path")
#undef TOPDIR_TEST
END_TEST
-typedef int (normalize_path)(char *, const char *);
+typedef int (normalize_path)(char *, size_t, const char *);
-static int ensure_normalized(const char *input_path, const char *expected_path, normalize_path normalizer)
+/* Assert flags */
+#define CWD_AS_PREFIX 1
+#define PATH_AS_SUFFIX 2
+#define ROOTED_PATH 4
+
+static int ensure_normalized(const char *input_path, const char *expected_path, normalize_path normalizer, int assert_flags)
{
int error = GIT_SUCCESS;
char buffer_out[GIT_PATH_MAX];
+ char current_workdir[GIT_PATH_MAX];
- error = normalizer(buffer_out, input_path);
+ error = gitfo_getcwd(current_workdir, sizeof(current_workdir));
+ if (error < GIT_SUCCESS)
+ return error;
+
+ error = normalizer(buffer_out, sizeof(buffer_out), input_path);
if (error < GIT_SUCCESS)
return error;
if (expected_path == NULL)
return error;
- if (strcmp(buffer_out, expected_path))
- error = GIT_ERROR;
+ if ((assert_flags & PATH_AS_SUFFIX) != 0)
+ if (git__suffixcmp(buffer_out, expected_path))
+ return GIT_ERROR;
+
+ if ((assert_flags & CWD_AS_PREFIX) != 0)
+ if (git__prefixcmp(buffer_out, current_workdir))
+ return GIT_ERROR;
+
+ if ((assert_flags & ROOTED_PATH) != 0) {
+ error = strcmp(expected_path, buffer_out);
+ }
return error;
}
-static int ensure_dir_path_normalized(const char *input_path, const char *expected_path)
+static int ensure_dir_path_normalized(const char *input_path, const char *expected_path, int assert_flags)
{
- return ensure_normalized(input_path, expected_path, gitfo_prettify_dir_path);
+ return ensure_normalized(input_path, expected_path, gitfo_prettify_dir_path, assert_flags);
}
-static int ensure_file_path_normalized(const char *input_path, const char *expected_path)
+static int ensure_file_path_normalized(const char *input_path, const char *expected_path, int assert_flags)
{
- return ensure_normalized(input_path, expected_path, gitfo_prettify_file_path);
+ return ensure_normalized(input_path, expected_path, gitfo_prettify_file_path, assert_flags);
}
BEGIN_TEST(path3, "prettify and validate a path to a file")
- must_pass(ensure_file_path_normalized("a", "a"));
- must_pass(ensure_file_path_normalized("./testrepo.git", "testrepo.git"));
- must_pass(ensure_file_path_normalized("./.git", ".git"));
- must_pass(ensure_file_path_normalized("./git.", "git."));
- must_fail(ensure_file_path_normalized("git./", NULL));
- must_fail(ensure_file_path_normalized("", NULL));
- must_fail(ensure_file_path_normalized(".", NULL));
- must_fail(ensure_file_path_normalized("./", NULL));
- must_fail(ensure_file_path_normalized("./.", NULL));
- must_fail(ensure_file_path_normalized("./..", NULL));
- must_fail(ensure_file_path_normalized("../.", NULL));
- must_fail(ensure_file_path_normalized("./.././/", NULL));
- must_fail(ensure_file_path_normalized("dir/..", NULL));
- must_fail(ensure_file_path_normalized("dir/sub/../..", NULL));
- must_fail(ensure_file_path_normalized("dir/sub/..///..", NULL));
- must_fail(ensure_file_path_normalized("dir/sub///../..", NULL));
- must_fail(ensure_file_path_normalized("dir/sub///..///..", NULL));
- must_fail(ensure_file_path_normalized("dir/sub/../../..", NULL));
- must_pass(ensure_file_path_normalized("dir", "dir"));
- must_fail(ensure_file_path_normalized("dir//", NULL));
- must_pass(ensure_file_path_normalized("./dir", "dir"));
- must_fail(ensure_file_path_normalized("dir/.", NULL));
- must_fail(ensure_file_path_normalized("dir///./", NULL));
- must_fail(ensure_file_path_normalized("dir/sub/..", NULL));
- must_fail(ensure_file_path_normalized("dir//sub/..",NULL));
- must_fail(ensure_file_path_normalized("dir//sub/../", NULL));
- must_fail(ensure_file_path_normalized("dir/sub/../", NULL));
- must_fail(ensure_file_path_normalized("dir/sub/../.", NULL));
- must_fail(ensure_file_path_normalized("dir/s1/../s2/", NULL));
- must_fail(ensure_file_path_normalized("d1/s1///s2/..//../s3/", NULL));
- must_pass(ensure_file_path_normalized("d1/s1//../s2/../../d2", "d2"));
- must_fail(ensure_file_path_normalized("dir/sub/../", NULL));
- must_fail(ensure_file_path_normalized("....", NULL));
- must_fail(ensure_file_path_normalized("...", NULL));
- must_fail(ensure_file_path_normalized("./...", NULL));
- must_fail(ensure_file_path_normalized("d1/...", NULL));
- must_fail(ensure_file_path_normalized("d1/.../", NULL));
- must_fail(ensure_file_path_normalized("d1/.../d2", NULL));
+ must_pass(ensure_file_path_normalized("a", "a", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_file_path_normalized("./testrepo.git", "testrepo.git", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_file_path_normalized("./.git", ".git", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_file_path_normalized("./git.", "git.", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_fail(ensure_file_path_normalized("git./", NULL, 0));
+ must_fail(ensure_file_path_normalized("", NULL, 0));
+ must_fail(ensure_file_path_normalized(".", NULL, 0));
+ must_fail(ensure_file_path_normalized("./", NULL, 0));
+ must_fail(ensure_file_path_normalized("./.", NULL, 0));
+ must_fail(ensure_file_path_normalized("./..", NULL, 0));
+ must_fail(ensure_file_path_normalized("../.", NULL, 0));
+ must_fail(ensure_file_path_normalized("./.././/", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/..", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub/../..", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub/..///..", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub///../..", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub///..///..", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub/../../..", NULL, 0));
+ must_pass(ensure_file_path_normalized("dir", "dir", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_fail(ensure_file_path_normalized("dir//", NULL, 0));
+ must_pass(ensure_file_path_normalized("./dir", "dir", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_fail(ensure_file_path_normalized("dir/.", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir///./", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub/..", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir//sub/..",NULL, 0));
+ must_fail(ensure_file_path_normalized("dir//sub/../", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub/../", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/sub/../.", NULL, 0));
+ must_fail(ensure_file_path_normalized("dir/s1/../s2/", NULL, 0));
+ must_fail(ensure_file_path_normalized("d1/s1///s2/..//../s3/", NULL, 0));
+ must_pass(ensure_file_path_normalized("d1/s1//../s2/../../d2", "d2", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_fail(ensure_file_path_normalized("dir/sub/../", NULL, 0));
+ must_pass(ensure_file_path_normalized("../../a/../../b/c/d/../../e", "b/e", PATH_AS_SUFFIX));
+ must_fail(ensure_file_path_normalized("....", NULL, 0));
+ must_fail(ensure_file_path_normalized("...", NULL, 0));
+ must_fail(ensure_file_path_normalized("./...", NULL, 0));
+ must_fail(ensure_file_path_normalized("d1/...", NULL, 0));
+ must_fail(ensure_file_path_normalized("d1/.../", NULL, 0));
+ must_fail(ensure_file_path_normalized("d1/.../d2", NULL, 0));
- must_pass(ensure_file_path_normalized("/a", "/a"));
- must_pass(ensure_file_path_normalized("/./testrepo.git", "/testrepo.git"));
- must_pass(ensure_file_path_normalized("/./.git", "/.git"));
- must_pass(ensure_file_path_normalized("/./git.", "/git."));
- must_fail(ensure_file_path_normalized("/git./", NULL));
- must_fail(ensure_file_path_normalized("/", NULL));
- must_fail(ensure_file_path_normalized("/.", NULL));
- must_fail(ensure_file_path_normalized("/./", NULL));
- must_fail(ensure_file_path_normalized("/./.", NULL));
- must_fail(ensure_file_path_normalized("/./..", NULL));
- must_fail(ensure_file_path_normalized("/../.", NULL));
- must_fail(ensure_file_path_normalized("/./.././/", NULL));
- must_fail(ensure_file_path_normalized("/dir/..", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub/../..", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub/..///..", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub///../..", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub///..///..", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub/../../..", NULL));
- must_pass(ensure_file_path_normalized("/dir", "/dir"));
- must_fail(ensure_file_path_normalized("/dir//", NULL));
- must_pass(ensure_file_path_normalized("/./dir", "/dir"));
- must_fail(ensure_file_path_normalized("/dir/.", NULL));
- must_fail(ensure_file_path_normalized("/dir///./", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub/..", NULL));
- must_fail(ensure_file_path_normalized("/dir//sub/..",NULL));
- must_fail(ensure_file_path_normalized("/dir//sub/../", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub/../", NULL));
- must_fail(ensure_file_path_normalized("/dir/sub/../.", NULL));
- must_fail(ensure_file_path_normalized("/dir/s1/../s2/", NULL));
- must_fail(ensure_file_path_normalized("/d1/s1///s2/..//../s3/", NULL));
- must_pass(ensure_file_path_normalized("/d1/s1//../s2/../../d2", "/d2"));
- must_fail(ensure_file_path_normalized("/dir/sub/../", NULL));
- must_fail(ensure_file_path_normalized("/....", NULL));
- must_fail(ensure_file_path_normalized("/...", NULL));
- must_fail(ensure_file_path_normalized("/./...", NULL));
- must_fail(ensure_file_path_normalized("/d1/...", NULL));
- must_fail(ensure_file_path_normalized("/d1/.../", NULL));
- must_fail(ensure_file_path_normalized("/d1/.../d2", NULL));
+ must_pass(ensure_file_path_normalized("/a", "/a", ROOTED_PATH));
+ must_pass(ensure_file_path_normalized("/./testrepo.git", "/testrepo.git", ROOTED_PATH));
+ must_pass(ensure_file_path_normalized("/./.git", "/.git", ROOTED_PATH));
+ must_pass(ensure_file_path_normalized("/./git.", "/git.", ROOTED_PATH));
+ must_fail(ensure_file_path_normalized("/git./", NULL, 0));
+ must_fail(ensure_file_path_normalized("/", NULL, 0));
+ must_fail(ensure_file_path_normalized("/.", NULL, 0));
+ must_fail(ensure_file_path_normalized("/./", NULL, 0));
+ must_fail(ensure_file_path_normalized("/./.", NULL, 0));
+ must_fail(ensure_file_path_normalized("/./..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/../.", NULL, 0));
+ must_fail(ensure_file_path_normalized("/./.././/", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/../..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/..///..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub///../..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub///..///..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/../../..", NULL, 0));
+ must_pass(ensure_file_path_normalized("/dir", "/dir", 0));
+ must_fail(ensure_file_path_normalized("/dir//", NULL, 0));
+ must_pass(ensure_file_path_normalized("/./dir", "/dir", 0));
+ must_fail(ensure_file_path_normalized("/dir/.", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir///./", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/..", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir//sub/..",NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir//sub/../", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/../", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/../.", NULL, 0));
+ must_fail(ensure_file_path_normalized("/dir/s1/../s2/", NULL, 0));
+ must_fail(ensure_file_path_normalized("/d1/s1///s2/..//../s3/", NULL, 0));
+ must_pass(ensure_file_path_normalized("/d1/s1//../s2/../../d2", "/d2", 0));
+ must_fail(ensure_file_path_normalized("/dir/sub/../", NULL, 0));
+ must_fail(ensure_file_path_normalized("/....", NULL, 0));
+ must_fail(ensure_file_path_normalized("/...", NULL, 0));
+ must_fail(ensure_file_path_normalized("/./...", NULL, 0));
+ must_fail(ensure_file_path_normalized("/d1/...", NULL, 0));
+ must_fail(ensure_file_path_normalized("/d1/.../", NULL, 0));
+ must_fail(ensure_file_path_normalized("/d1/.../d2", NULL, 0));
END_TEST
BEGIN_TEST(path4, "validate and prettify a path to a folder")
- must_pass(ensure_dir_path_normalized("./testrepo.git", "testrepo.git/"));
- must_pass(ensure_dir_path_normalized("./.git", ".git/"));
- must_pass(ensure_dir_path_normalized("./git.", "git./"));
- must_pass(ensure_dir_path_normalized("git./", "git./"));
- must_pass(ensure_dir_path_normalized("", ""));
- must_pass(ensure_dir_path_normalized(".", ""));
- must_pass(ensure_dir_path_normalized("./", ""));
- must_pass(ensure_dir_path_normalized("./.", ""));
- must_fail(ensure_dir_path_normalized("./..", NULL));
- must_fail(ensure_dir_path_normalized("../.", NULL));
- must_fail(ensure_dir_path_normalized("./.././/", NULL));
- must_pass(ensure_dir_path_normalized("dir/..", ""));
- must_pass(ensure_dir_path_normalized("dir/sub/../..", ""));
- must_pass(ensure_dir_path_normalized("dir/sub/..///..", ""));
- must_pass(ensure_dir_path_normalized("dir/sub///../..", ""));
- must_pass(ensure_dir_path_normalized("dir/sub///..///..", ""));
- must_fail(ensure_dir_path_normalized("dir/sub/../../..", NULL));
- must_pass(ensure_dir_path_normalized("dir", "dir/"));
- must_pass(ensure_dir_path_normalized("dir//", "dir/"));
- must_pass(ensure_dir_path_normalized("./dir", "dir/"));
- must_pass(ensure_dir_path_normalized("dir/.", "dir/"));
- must_pass(ensure_dir_path_normalized("dir///./", "dir/"));
- must_pass(ensure_dir_path_normalized("dir/sub/..", "dir/"));
- must_pass(ensure_dir_path_normalized("dir//sub/..", "dir/"));
- must_pass(ensure_dir_path_normalized("dir//sub/../", "dir/"));
- must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/"));
- must_pass(ensure_dir_path_normalized("dir/sub/../.", "dir/"));
- must_pass(ensure_dir_path_normalized("dir/s1/../s2/", "dir/s2/"));
- must_pass(ensure_dir_path_normalized("d1/s1///s2/..//../s3/", "d1/s3/"));
- must_pass(ensure_dir_path_normalized("d1/s1//../s2/../../d2", "d2/"));
- must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/"));
- must_fail(ensure_dir_path_normalized("....", NULL));
- must_fail(ensure_dir_path_normalized("...", NULL));
- must_fail(ensure_dir_path_normalized("./...", NULL));
- must_fail(ensure_dir_path_normalized("d1/...", NULL));
- must_fail(ensure_dir_path_normalized("d1/.../", NULL));
- must_fail(ensure_dir_path_normalized("d1/.../d2", NULL));
-
- must_pass(ensure_dir_path_normalized("/./testrepo.git", "/testrepo.git/"));
- must_pass(ensure_dir_path_normalized("/./.git", "/.git/"));
- must_pass(ensure_dir_path_normalized("/./git.", "/git./"));
- must_pass(ensure_dir_path_normalized("/git./", "/git./"));
- must_pass(ensure_dir_path_normalized("/", "/"));
- must_pass(ensure_dir_path_normalized("//", "/"));
- must_pass(ensure_dir_path_normalized("///", "/"));
- must_pass(ensure_dir_path_normalized("/.", "/"));
- must_pass(ensure_dir_path_normalized("/./", "/"));
- must_fail(ensure_dir_path_normalized("/./..", NULL));
- must_fail(ensure_dir_path_normalized("/../.", NULL));
- must_fail(ensure_dir_path_normalized("/./.././/", NULL));
- must_pass(ensure_dir_path_normalized("/dir/..", "/"));
- must_pass(ensure_dir_path_normalized("/dir/sub/../..", "/"));
- must_fail(ensure_dir_path_normalized("/dir/sub/../../..", NULL));
- must_pass(ensure_dir_path_normalized("/dir", "/dir/"));
- must_pass(ensure_dir_path_normalized("/dir//", "/dir/"));
- must_pass(ensure_dir_path_normalized("/./dir", "/dir/"));
- must_pass(ensure_dir_path_normalized("/dir/.", "/dir/"));
- must_pass(ensure_dir_path_normalized("/dir///./", "/dir/"));
- must_pass(ensure_dir_path_normalized("/dir//sub/..", "/dir/"));
- must_pass(ensure_dir_path_normalized("/dir/sub/../", "/dir/"));
- must_pass(ensure_dir_path_normalized("//dir/sub/../.", "/dir/"));
- must_pass(ensure_dir_path_normalized("/dir/s1/../s2/", "/dir/s2/"));
- must_pass(ensure_dir_path_normalized("/d1/s1///s2/..//../s3/", "/d1/s3/"));
- must_pass(ensure_dir_path_normalized("/d1/s1//../s2/../../d2", "/d2/"));
- must_fail(ensure_dir_path_normalized("/....", NULL));
- must_fail(ensure_dir_path_normalized("/...", NULL));
- must_fail(ensure_dir_path_normalized("/./...", NULL));
- must_fail(ensure_dir_path_normalized("/d1/...", NULL));
- must_fail(ensure_dir_path_normalized("/d1/.../", NULL));
- must_fail(ensure_dir_path_normalized("/d1/.../d2", NULL));
+ must_pass(ensure_dir_path_normalized("./testrepo.git", "testrepo.git/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("./.git", ".git/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("./git.", "git./", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("git./", "git./", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized(".", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("./", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("./.", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub/../..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub/..///..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub///../..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub///..///..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir//", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("./dir", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/.", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir///./", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub/..", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir//sub/..", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir//sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub/../.", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/s1/../s2/", "dir/s2/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("d1/s1///s2/..//../s3/", "d1/s3/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("d1/s1//../s2/../../d2", "d2/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX));
+ must_pass(ensure_dir_path_normalized("../../a/../../b/c/d/../../e", "b/e/", PATH_AS_SUFFIX));
+ must_fail(ensure_dir_path_normalized("....", NULL, 0));
+ must_fail(ensure_dir_path_normalized("...", NULL, 0));
+ must_fail(ensure_dir_path_normalized("./...", NULL, 0));
+ must_fail(ensure_dir_path_normalized("d1/...", NULL, 0));
+ must_fail(ensure_dir_path_normalized("d1/.../", NULL, 0));
+ must_fail(ensure_dir_path_normalized("d1/.../d2", NULL, 0));
+
+ must_pass(ensure_dir_path_normalized("/./testrepo.git", "/testrepo.git/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/./.git", "/.git/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/./git.", "/git./", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/git./", "/git./", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/", "/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("//", "/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("///", "/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/.", "/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/./", "/", ROOTED_PATH));
+ must_fail(ensure_dir_path_normalized("/./..", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/../.", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/./.././/", NULL, 0));
+ must_pass(ensure_dir_path_normalized("/dir/..", "/", 0));
+ must_pass(ensure_dir_path_normalized("/dir/sub/../..", "/", 0));
+ must_fail(ensure_dir_path_normalized("/dir/sub/../../..", NULL, 0));
+ must_pass(ensure_dir_path_normalized("/dir", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/dir//", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/./dir", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/dir/.", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/dir///./", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/dir//sub/..", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/dir/sub/../", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("//dir/sub/../.", "/dir/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/dir/s1/../s2/", "/dir/s2/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/d1/s1///s2/..//../s3/", "/d1/s3/", ROOTED_PATH));
+ must_pass(ensure_dir_path_normalized("/d1/s1//../s2/../../d2", "/d2/", ROOTED_PATH));
+ must_fail(ensure_dir_path_normalized("/....", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/...", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/./...", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/d1/...", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/d1/.../", NULL, 0));
+ must_fail(ensure_dir_path_normalized("/d1/.../d2", NULL, 0));
END_TEST
static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path)
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index 8b39e39..2c7f131 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -113,22 +113,18 @@ static int ensure_repository_init(
return GIT_ERROR;
if (repo->path_workdir != NULL || expected_working_directory != NULL) {
- if (strcmp(repo->path_workdir, expected_working_directory) != 0)
- //return GIT_ERROR;
+ if (git__suffixcmp(repo->path_workdir, expected_working_directory) != 0)
goto cleanup;
}
- if (strcmp(repo->path_odb, path_odb) != 0)
- //return GIT_ERROR;
+ if (git__suffixcmp(repo->path_odb, path_odb) != 0)
goto cleanup;
- if (strcmp(repo->path_repository, expected_path_repository) != 0)
- //return GIT_ERROR;
+ if (git__suffixcmp(repo->path_repository, expected_path_repository) != 0)
goto cleanup;
if (repo->path_index != NULL || expected_path_index != NULL) {
- if (strcmp(repo->path_index, expected_path_index) != 0)
- //return GIT_ERROR;
+ if (git__suffixcmp(repo->path_index, expected_path_index) != 0)
goto cleanup;
}
@@ -170,18 +166,18 @@ BEGIN_TEST(init2, "Initialize a bare repo with a relative path escaping out of t
must_pass(gitfo_getcwd(current_workdir, sizeof(current_workdir)));
- git__joinpath(path_repository, current_workdir, "a/b/c/");
+ git__joinpath(path_repository, TEMP_REPO_FOLDER, "a/b/c/");
must_pass(gitfo_mkdir_recurs(path_repository, mode));
must_pass(chdir(path_repository));
must_pass(git_repository_init(&repo, "../d/e.git", 1));
+ must_pass(git__suffixcmp(repo->path_repository, "/a/b/d/e.git/"));
+
git_repository_free(repo);
must_pass(chdir(current_workdir));
-
- git__joinpath(path_repository, current_workdir, "a/");
- must_pass(rmdir_recurs(path_repository));
+ rmdir_recurs(TEMP_REPO_FOLDER);
END_TEST
BEGIN_SUITE(repository)