{"thread":{"id":"45803","subject":"[PATCH v11 1/5] dir_iterator: add tests for dir_iterator API","startedAt":"2017-04-26T17:03:59Z","lastAt":"2017-04-26T17:04:14Z","messageCount":6,"participants":["Daniel Ferreira"],"isPatch":true,"patchVersion":11,"patchTotal":5},"messages":[{"id":"317937","messageId":"1493226219-33423-2-git-send-email-bnmvco@gmail.com","threadId":"45803","inReplyTo":"1493226219-33423-1-git-send-email-bnmvco@gmail.com","subject":"[PATCH v11 1/5] dir_iterator: add tests for dir_iterator API","fromName":"Daniel Ferreira","fromEmail":"bnmvco@gmail.com","sentAt":"2017-04-26T17:03:35Z","receivedAt":"2017-04-26T17:03:59Z","isPatch":true,"sender":{"key":"bnmvco@gmail.com","avatar":"https://avatars.githubusercontent.com/u/735397?v=4"},"body":"Create t/helper/test-dir-iterator.c, which prints relevant information\nabout a directory tree iterated over with dir_iterator.\n\nCreate t/t0066-dir-iterator.sh, which tests that dir_iterator does\niterate through a whole directory tree.\n\nSigned-off-by: Daniel Ferreira <bnmvco@gmail.com>\n---\n Makefile                     |  1 +\n t/helper/.gitignore          |  1 +\n t/helper/test-dir-iterator.c | 30 ++++++++++++++++++++++++\n t/t0066-dir-iterator.sh      | 55 ++++++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 87 insertions(+)\n create mode 100644 t/helper/test-dir-iterator.c\n create mode 100755 t/t0066-dir-iterator.sh\n\ndiff --git a/Makefile b/Makefile\nindex eb1a1a7..a669e43 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -614,6 +614,7 @@ TEST_PROGRAMS_NEED_X += test-ctype\n TEST_PROGRAMS_NEED_X += test-config\n TEST_PROGRAMS_NEED_X += test-date\n TEST_PROGRAMS_NEED_X += test-delta\n+TEST_PROGRAMS_NEED_X += test-dir-iterator\n TEST_PROGRAMS_NEED_X += test-dump-cache-tree\n TEST_PROGRAMS_NEED_X += test-dump-split-index\n TEST_PROGRAMS_NEED_X += test-dump-untracked-cache\ndiff --git a/t/helper/.gitignore b/t/helper/.gitignore\nindex acd5db1..60adab5 100644\n--- a/t/helper/.gitignore\n+++ b/t/helper/.gitignore\n@@ -3,6 +3,7 @@\n /test-config\n /test-date\n /test-delta\n+/test-dir-iterator\n /test-dump-cache-tree\n /test-dump-split-index\n /test-dump-untracked-cache\ndiff --git a/t/helper/test-dir-iterator.c b/t/helper/test-dir-iterator.c\nnew file mode 100644\nindex 0000000..a7d1470\n--- /dev/null\n+++ b/t/helper/test-dir-iterator.c\n@@ -0,0 +1,30 @@\n+#include \"git-compat-util.h\"\n+#include \"strbuf.h\"\n+#include \"iterator.h\"\n+#include \"dir-iterator.h\"\n+\n+int cmd_main(int argc, const char **argv)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct dir_iterator *diter;\n+\n+\tif (argc < 2)\n+\t\tdie(\"BUG: test-dir-iterator needs one argument\");\n+\n+\tstrbuf_add(&path, argv[1], strlen(argv[1]));\n+\n+\tditer = dir_iterator_begin(path.buf);\n+\n+\twhile (dir_iterator_advance(diter) == ITER_OK) {\n+\t\tif (S_ISDIR(diter->st.st_mode))\n+\t\t\tprintf(\"[d] \");\n+\t\telse if (S_ISREG(diter->st.st_mode))\n+\t\t\tprintf(\"[f] \");\n+\t\telse\n+\t\t\tprintf(\"[?] \");\n+\n+\t\tprintf(\"(%s) [%s] %s\\n\", diter->relative_path, diter->basename, diter->path.buf);\n+\t}\n+\n+\treturn 0;\n+}\ndiff --git a/t/t0066-dir-iterator.sh b/t/t0066-dir-iterator.sh\nnew file mode 100755\nindex 0000000..46e5ce5\n--- /dev/null\n+++ b/t/t0066-dir-iterator.sh\n@@ -0,0 +1,55 @@\n+#!/bin/sh\n+\n+test_description='Test directory iteration.'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\tmkdir -p dir &&\n+\tmkdir -p dir/a/b/c/ &&\n+\t>dir/b &&\n+\t>dir/c &&\n+\tmkdir -p dir/d/e/d/ &&\n+\t>dir/a/b/c/d &&\n+\t>dir/a/e &&\n+\t>dir/d/e/d/a &&\n+\n+\tmkdir -p dir2/a/b/c/ &&\n+\t>dir2/a/b/c/d\n+'\n+\n+test_expect_success 'dir-iterator should iterate through all files' '\n+\tcat >expect-sorted-output <<-\\EOF &&\n+\t[d] (a) [a] ./dir/a\n+\t[d] (a/b) [b] ./dir/a/b\n+\t[d] (a/b/c) [c] ./dir/a/b/c\n+\t[d] (d) [d] ./dir/d\n+\t[d] (d/e) [e] ./dir/d/e\n+\t[d] (d/e/d) [d] ./dir/d/e/d\n+\t[f] (a/b/c/d) [d] ./dir/a/b/c/d\n+\t[f] (a/e) [e] ./dir/a/e\n+\t[f] (b) [b] ./dir/b\n+\t[f] (c) [c] ./dir/c\n+\t[f] (d/e/d/a) [a] ./dir/d/e/d/a\n+\tEOF\n+\n+\ttest-dir-iterator ./dir >out &&\n+\tsort <out >./actual-pre-order-sorted-output &&\n+\n+\ttest_cmp expect-sorted-output actual-pre-order-sorted-output\n+'\n+\n+test_expect_success 'dir-iterator should list files in the correct order' '\n+\tcat >expect-pre-order-output <<-\\EOF &&\n+\t[d] (a) [a] ./dir2/a\n+\t[d] (a/b) [b] ./dir2/a/b\n+\t[d] (a/b/c) [c] ./dir2/a/b/c\n+\t[f] (a/b/c/d) [d] ./dir2/a/b/c/d\n+\tEOF\n+\n+\ttest-dir-iterator ./dir2 >actual-pre-order-output &&\n+\n+\ttest_cmp expect-pre-order-output actual-pre-order-output\n+'\n+\n+test_done\n-- \n2.7.4 (Apple Git-66)\n\n"},{"id":"317938","messageId":"1493226219-33423-3-git-send-email-bnmvco@gmail.com","threadId":"45803","inReplyTo":"1493226219-33423-1-git-send-email-bnmvco@gmail.com","subject":"[PATCH v11 2/5] remove_subtree(): test removing nested directories","fromName":"Daniel Ferreira","fromEmail":"bnmvco@gmail.com","sentAt":"2017-04-26T17:03:36Z","receivedAt":"2017-04-26T17:04:01Z","isPatch":true,"sender":{"key":"bnmvco@gmail.com","avatar":"https://avatars.githubusercontent.com/u/735397?v=4"},"body":"Test removing a nested directory when an attempt is made to restore the\nindex to a state where it does not exist. A similar test could be found\npreviously in t/t2000-checkout-cache-clash.sh, but it did not check for\nnested directories, which could allow a faulty implementation of\nremove_subtree() pass the tests.\n\nSigned-off-by: Daniel Ferreira <bnmvco@gmail.com>\n---\n t/t2000-checkout-cache-clash.sh | 11 +++++++++++\n 1 file changed, 11 insertions(+)\n\ndiff --git a/t/t2000-checkout-cache-clash.sh b/t/t2000-checkout-cache-clash.sh\nindex de3edb5..ac10ba3 100755\n--- a/t/t2000-checkout-cache-clash.sh\n+++ b/t/t2000-checkout-cache-clash.sh\n@@ -57,4 +57,15 @@ test_expect_success SYMLINKS 'checkout-index -f twice with --prefix' '\n \tgit checkout-index -a -f --prefix=there/\n '\n \n+test_expect_success 'git checkout-index -f should remove nested subtrees' '\n+\techo content >path &&\n+\tgit update-index --add path &&\n+\trm path &&\n+\tmkdir -p path/with/nested/paths &&\n+\techo content >path/file1 &&\n+\techo content >path/with/nested/paths/file2 &&\n+\tgit checkout-index -f -a &&\n+\ttest ! -d path\n+'\n+\n test_done\n-- \n2.7.4 (Apple Git-66)\n\n"},{"id":"317939","messageId":"1493226219-33423-1-git-send-email-bnmvco@gmail.com","threadId":"45803","inReplyTo":null,"subject":"[PATCH v11 0/5] [GSoC] remove_subtree(): reimplement using iterators","fromName":"Daniel Ferreira","fromEmail":"bnmvco@gmail.com","sentAt":"2017-04-26T17:03:34Z","receivedAt":"2017-04-26T17:04:06Z","isPatch":true,"sender":{"key":"bnmvco@gmail.com","avatar":"https://avatars.githubusercontent.com/u/735397?v=4"},"body":"This is the eleventh version of a patch series that implements the GSoC\nmicroproject of converting a recursive call to readdir() to use\ndir_iterator.\n\nv1: https://public-inbox.org/git/CAGZ79kZwT-9mHTiOJ5CEjk2wDFkn6+NcogjX0=vjhsAh16ANYg@mail.gmail.com/T/#t\nv2: https://public-inbox.org/git/CACsJy8Dxh-QPBBLfaFWPAWUsbA9GVXA7x+mXLjEvYKhk1zOpig@mail.gmail.com/T/#t\nv3: https://public-inbox.org/git/CAGZ79kYtpmURSQWPumobA=e3JBFjKhWCdv_LPhKCd71ZRwMovA@mail.gmail.com/T/#t\nv4: https://public-inbox.org/git/1490747533-89143-1-git-send-email-bnmvco@gmail.com/T/#e437a63e0c22c00c69b5d92977c9b438ed2b9fd3a\nv5: https://public-inbox.org/git/1490844730-47634-1-git-send-email-bnmvco@gmail.com/T/#m2323f15e45de699f2e09364f40a62e17047cf453\nv6: https://public-inbox.org/git/1491107726-21504-1-git-send-email-bnmvco@gmail.com/T/#t\nv7: https://public-inbox.org/git/1491163388-41255-1-git-send-email-bnmvco@gmail.com/T/#t\nv8: https://public-inbox.org/git/a60b2ed6-2b99-b134-05af-7c8492a6949c@alum.mit.edu/T/#t\nv9: https://public-inbox.org/git/CAGZ79kaBRS0SFAvrV4mN7-mVk+8QmPKPJMD55zPQ+A14ZzYFYA@mail.gmail.com/T/#me8988b7dd4adbc4ea24946ccb24fc1cf7baf44e3\nv10: https://public-inbox.org/git/xmqqk26fahjn.fsf@gitster.mtv.corp.google.com/T/#m3071006ec67457adf69578b37f55b625d0e7fed7\n\nTravis CI build: https://travis-ci.org/theiostream/git/builds/226079792\n\nOkay, in this version I factored in Junio's request for a test rename\nto t0066, and most of Michael's suggestions from the last review. I'm\nsorry for the delay on this one.\n\nInstead of either removing the iterate root dir feature or return NULL\nas its basename, I chose to get the real_path() out of the dir we are\niterating over and get the basename of that, to avoid the \"/.\" or \"/..\"\nissues. I think this is actually less complex than the NULL solution in\nterms of code that would end up needing to be written, and I think the\nroot dir feature is handy to have on dir-iterator.\n\nAs for the suggestion to put strerror() on the test, I feared for the\nmessage compatibility across platforms (since we actually check which\nerrno code we got). If you could give me a guarantee that this is not a\nproblem and you think it'd be worthy of yet another series, I'm up for\nit.\n\nAs for Junio's concern for a rebase issue on the test script, the\nreason for it was that the same file is used across two tests, so it\nseemed unnecessary to recreate the file within each of them.\n\nDaniel Ferreira (5):\n  dir_iterator: add tests for dir_iterator API\n  remove_subtree(): test removing nested directories\n  dir_iterator: refactor dir_iterator_advance\n  dir_iterator: rewrite state machine model\n  remove_subtree(): reimplement using iterators\n\n Makefile                        |   1 +\n dir-iterator.c                  | 244 ++++++++++++++++++++++++++++------------\n dir-iterator.h                  |  35 ++++--\n entry.c                         |  42 +++----\n refs/files-backend.c            |  15 ++-\n t/helper/.gitignore             |   1 +\n t/helper/test-dir-iterator.c    |  53 +++++++++\n t/t0066-dir-iterator.sh         | 121 ++++++++++++++++++++\n t/t2000-checkout-cache-clash.sh |  11 ++\n 9 files changed, 416 insertions(+), 107 deletions(-)\n create mode 100644 t/helper/test-dir-iterator.c\n create mode 100755 t/t0066-dir-iterator.sh\n\n--\n2.7.4 (Apple Git-66)\n\n"},{"id":"317940","messageId":"1493226219-33423-4-git-send-email-bnmvco@gmail.com","threadId":"45803","inReplyTo":"1493226219-33423-1-git-send-email-bnmvco@gmail.com","subject":"[PATCH v11 3/5] dir_iterator: refactor dir_iterator_advance","fromName":"Daniel Ferreira","fromEmail":"bnmvco@gmail.com","sentAt":"2017-04-26T17:03:37Z","receivedAt":"2017-04-26T17:04:07Z","isPatch":true,"sender":{"key":"bnmvco@gmail.com","avatar":"https://avatars.githubusercontent.com/u/735397?v=4"},"body":"Factor out reusable helpers out of dir_iterator_advance(). Make\ndir_iterator_advance()'s code more legible and allow some behavior to\nbe reusable.\n\nSigned-off-by: Daniel Ferreira <bnmvco@gmail.com>\n---\n dir-iterator.c | 66 ++++++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 43 insertions(+), 23 deletions(-)\n\ndiff --git a/dir-iterator.c b/dir-iterator.c\nindex 34182a9..d168cb2 100644\n--- a/dir-iterator.c\n+++ b/dir-iterator.c\n@@ -50,6 +50,44 @@ struct dir_iterator_int {\n \tstruct dir_iterator_level *levels;\n };\n \n+static void push_dir_level(struct dir_iterator_int *iter, struct dir_iterator_level *level)\n+{\n+\tlevel->dir_state = DIR_STATE_RECURSE;\n+\tALLOC_GROW(iter->levels, iter->levels_nr + 1,\n+\t\t   iter->levels_alloc);\n+\tlevel = &iter->levels[iter->levels_nr++];\n+\tlevel->initialized = 0;\n+}\n+\n+static int pop_dir_level(struct dir_iterator_int *iter)\n+{\n+\treturn --iter->levels_nr;\n+}\n+\n+static int adjust_iterator_data(struct dir_iterator_int *iter,\n+\t\tstruct dir_iterator_level *level)\n+{\n+\tif (lstat(iter->base.path.buf, &iter->base.st) < 0) {\n+\t\tif (errno != ENOENT)\n+\t\t\twarning(\"error reading path '%s': %s\",\n+\t\t\t\titer->base.path.buf,\n+\t\t\t\tstrerror(errno));\n+\t\treturn -1;\n+\t}\n+\n+\t/*\n+\t * We have to set these each time because\n+\t * the path strbuf might have been realloc()ed.\n+\t */\n+\titer->base.relative_path =\n+\t\titer->base.path.buf + iter->levels[0].prefix_len;\n+\titer->base.basename =\n+\t\titer->base.path.buf + level->prefix_len;\n+\tlevel->dir_state = DIR_STATE_ITER;\n+\n+\treturn 0;\n+}\n+\n int dir_iterator_advance(struct dir_iterator *dir_iterator)\n {\n \tstruct dir_iterator_int *iter =\n@@ -84,11 +122,7 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \t\t\t\t * over; now prepare to iterate into\n \t\t\t\t * it.\n \t\t\t\t */\n-\t\t\t\tlevel->dir_state = DIR_STATE_RECURSE;\n-\t\t\t\tALLOC_GROW(iter->levels, iter->levels_nr + 1,\n-\t\t\t\t\t   iter->levels_alloc);\n-\t\t\t\tlevel = &iter->levels[iter->levels_nr++];\n-\t\t\t\tlevel->initialized = 0;\n+\t\t\t\tpush_dir_level(iter, level);\n \t\t\t\tcontinue;\n \t\t\t} else {\n \t\t\t\t/*\n@@ -104,7 +138,7 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \t\t\t * This level is exhausted (or wasn't opened\n \t\t\t * successfully); pop up a level.\n \t\t\t */\n-\t\t\tif (--iter->levels_nr == 0)\n+\t\t\tif (pop_dir_level(iter) == 0)\n \t\t\t\treturn dir_iterator_abort(dir_iterator);\n \n \t\t\tcontinue;\n@@ -129,7 +163,7 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \t\t\t\t\t\titer->base.path.buf, strerror(errno));\n \n \t\t\t\tlevel->dir = NULL;\n-\t\t\t\tif (--iter->levels_nr == 0)\n+\t\t\t\tif (pop_dir_level(iter) == 0)\n \t\t\t\t\treturn dir_iterator_abort(dir_iterator);\n \t\t\t\tbreak;\n \t\t\t}\n@@ -138,23 +172,9 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \t\t\t\tcontinue;\n \n \t\t\tstrbuf_addstr(&iter->base.path, de->d_name);\n-\t\t\tif (lstat(iter->base.path.buf, &iter->base.st) < 0) {\n-\t\t\t\tif (errno != ENOENT)\n-\t\t\t\t\twarning(\"error reading path '%s': %s\",\n-\t\t\t\t\t\titer->base.path.buf,\n-\t\t\t\t\t\tstrerror(errno));\n-\t\t\t\tcontinue;\n-\t\t\t}\n \n-\t\t\t/*\n-\t\t\t * We have to set these each time because\n-\t\t\t * the path strbuf might have been realloc()ed.\n-\t\t\t */\n-\t\t\titer->base.relative_path =\n-\t\t\t\titer->base.path.buf + iter->levels[0].prefix_len;\n-\t\t\titer->base.basename =\n-\t\t\t\titer->base.path.buf + level->prefix_len;\n-\t\t\tlevel->dir_state = DIR_STATE_ITER;\n+\t\t\tif (adjust_iterator_data(iter, level))\n+\t\t\t\tcontinue;\n \n \t\t\treturn ITER_OK;\n \t\t}\n-- \n2.7.4 (Apple Git-66)\n\n"},{"id":"317941","messageId":"1493226219-33423-5-git-send-email-bnmvco@gmail.com","threadId":"45803","inReplyTo":"1493226219-33423-1-git-send-email-bnmvco@gmail.com","subject":"[PATCH v11 4/5] dir_iterator: rewrite state machine model","fromName":"Daniel Ferreira","fromEmail":"bnmvco@gmail.com","sentAt":"2017-04-26T17:03:38Z","receivedAt":"2017-04-26T17:04:12Z","isPatch":true,"sender":{"key":"bnmvco@gmail.com","avatar":"https://avatars.githubusercontent.com/u/735397?v=4"},"body":"Perform a rewrite of dir_iterator_advance(). dir_iterator has\nceased to rely on a combination of level.initialized and level.dir_state\nstate variables and now only tracks the state with level.dir_state,\nwhich simplifies the iterator mechanism, makes the code easier to follow\nand eases additions of new features to the iterator.\n\nMake dir_iterator_begin() attempt to lstat() the path it receives, and\nreturn NULL and an appropriate errno if it fails or if the passed path\nwas not a directory.\n\nCreate an option for the dir_iterator API to iterate over subdirectories\nonly after having iterated through their contents. This feature was\npredicted, although not implemented by 0fe5043 (\"dir_iterator: new API\nfor iterating over a directory tree\", 2016-06-18). This is useful for\nrecursively removing a directory and calling rmdir() on a directory only\nafter all of its contents have been wiped.\n\nAdd an option for dir_iterator to also iterate over the initial\ndirectory (the one passed to dir_iterator_begin()).\n\nAdd the \"flags\" parameter to dir_iterator_create, allowing for the\naforementioned new features to be enabled. The new default behavior\n(i.e. flags set to 0) does not iterate over directories. Flag\nDIR_ITERATOR_PRE_ORDER_TRAVERSAL iterates over a directory before doing\nso over its contents. DIR_ITERATOR_POST_ORDER_TRAVERSAL iterates over a\ndirectory after doing so over its contents. DIR_ITERATOR_LIST_ROOT_DIR\niterates over the initial directory. These flags do not conflict with\neach other and may be used simultaneously.\n\nAmend a call to dir_iterator_begin() in refs/files-backend.c to pass\nthe flags parameter introduced, as well as handle the case in which it\nfails to open the directory.\n\nImprove t/t0066-dir-iterator.sh and t/helper/test-dir-iterator.c to\ntest \"post-order\" and \"iterate-over-root\" modes.\n\nMichael Haggerty contributed with the design of the new\ndir_iterator_advance() implementation, the code for\nt/helper/test-dir-iterator's option parser and numerous reviews that\ngradually shaped this code to its current form.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\nSigned-off-by: Daniel Ferreira <bnmvco@gmail.com>\n---\n dir-iterator.c               | 212 ++++++++++++++++++++++++++++++-------------\n dir-iterator.h               |  35 +++++--\n refs/files-backend.c         |  15 ++-\n t/helper/test-dir-iterator.c |  31 ++++++-\n t/t0066-dir-iterator.sh      | 104 +++++++++++++++++----\n 5 files changed, 299 insertions(+), 98 deletions(-)\n\ndiff --git a/dir-iterator.c b/dir-iterator.c\nindex d168cb2..fba8f49 100644\n--- a/dir-iterator.c\n+++ b/dir-iterator.c\n@@ -4,8 +4,6 @@\n #include \"dir-iterator.h\"\n \n struct dir_iterator_level {\n-\tint initialized;\n-\n \tDIR *dir;\n \n \t/*\n@@ -20,9 +18,15 @@ struct dir_iterator_level {\n \t * iteration and also iterated into):\n \t */\n \tenum {\n-\t\tDIR_STATE_ITER,\n-\t\tDIR_STATE_RECURSE\n+\t\tDIR_STATE_PUSHED,\n+\t\tDIR_STATE_PRE_ITERATION,\n+\t\tDIR_STATE_ITERATING,\n+\t\tDIR_STATE_POST_ITERATION,\n+\t\tDIR_STATE_EXHAUSTED\n \t} dir_state;\n+\n+\t/* The stat structure for the directory this level represents. */\n+\tstruct stat st;\n };\n \n /*\n@@ -48,15 +52,23 @@ struct dir_iterator_int {\n \t * that will be included in this iteration.\n \t */\n \tstruct dir_iterator_level *levels;\n+\n+\t/* Holds the flags passed to dir_iterator_begin(). */\n+\tunsigned flags;\n };\n \n-static void push_dir_level(struct dir_iterator_int *iter, struct dir_iterator_level *level)\n+static void push_dir_level(struct dir_iterator_int *iter, struct stat *st)\n {\n-\tlevel->dir_state = DIR_STATE_RECURSE;\n+\tstruct dir_iterator_level *level;\n+\n \tALLOC_GROW(iter->levels, iter->levels_nr + 1,\n \t\t   iter->levels_alloc);\n+\n+\t/* Push a new level */\n \tlevel = &iter->levels[iter->levels_nr++];\n-\tlevel->initialized = 0;\n+\tlevel->dir = NULL;\n+\tlevel->dir_state = DIR_STATE_PUSHED;\n+\tlevel->st = *st;\n }\n \n static int pop_dir_level(struct dir_iterator_int *iter)\n@@ -67,7 +79,9 @@ static int pop_dir_level(struct dir_iterator_int *iter)\n static int adjust_iterator_data(struct dir_iterator_int *iter,\n \t\tstruct dir_iterator_level *level)\n {\n-\tif (lstat(iter->base.path.buf, &iter->base.st) < 0) {\n+\tif (level->dir_state != DIR_STATE_ITERATING) {\n+\t\titer->base.st = level->st;\n+\t} else if (lstat(iter->base.path.buf, &iter->base.st) < 0) {\n \t\tif (errno != ENOENT)\n \t\t\twarning(\"error reading path '%s': %s\",\n \t\t\t\titer->base.path.buf,\n@@ -76,18 +90,52 @@ static int adjust_iterator_data(struct dir_iterator_int *iter,\n \t}\n \n \t/*\n-\t * We have to set these each time because\n-\t * the path strbuf might have been realloc()ed.\n+\t * Check if we are dealing with the root directory as an\n+\t * item that's being iterated through.\n \t */\n-\titer->base.relative_path =\n-\t\titer->base.path.buf + iter->levels[0].prefix_len;\n-\titer->base.basename =\n-\t\titer->base.path.buf + level->prefix_len;\n-\tlevel->dir_state = DIR_STATE_ITER;\n+\tif (level->dir_state != DIR_STATE_ITERATING &&\n+\t\titer->levels_nr == 1) {\n+\t\tconst char *realpath, *last_path_component;\n+\n+\t\titer->base.relative_path = \".\";\n+\n+\t\t/*\n+\t\t * To get the root directory basename, we get the absolute path\n+\t\t * to our directory. Then, we get whatever is after the last '/'\n+\t\t * as the basename (or the whole directory if that does not exist,\n+\t\t * which should never happen.\n+\t\t */\n+\t\trealpath = real_path(iter->base.path.buf);\n+\t\tlast_path_component = strrchr(realpath, '/');\n+\t\titer->base.basename = last_path_component ?\n+\t\t\tlast_path_component + 1 : realpath;\n+\t} else {\n+\t\titer->base.relative_path =\n+\t\t\titer->base.path.buf + iter->levels[0].prefix_len;\n+\n+\t\tif (S_ISDIR(iter->base.st.st_mode))\n+\t\t\titer->base.basename =\n+\t\t\t\titer->base.path.buf + iter->levels[iter->levels_nr - 2].prefix_len;\n+\t\telse\n+\t\t\titer->base.basename =\n+\t\t\t\titer->base.path.buf + level->prefix_len;\n+\t}\n \n \treturn 0;\n }\n \n+/*\n+ * This function uses a state machine with the following states:\n+ * - DIR_STATE_PUSHED: the directory has been pushed to the\n+ *   iterator traversal tree.\n+ * - DIR_STATE_PRE_ITERATION: the directory is not opened with opendir(). The\n+ *   dirpath has already been returned if pre-order traversal is set.\n+ * - DIR_STATE_ITERATING: the directory is initialized. We are traversing\n+ *   through it.\n+ * - DIR_STATE_POST_ITERATION: the directory has been iterated through, and has\n+ *   been closed.\n+ * - DIR_STATE_EXHAUSTED: the directory is closed and ready to be popped.\n+ */\n int dir_iterator_advance(struct dir_iterator *dir_iterator)\n {\n \tstruct dir_iterator_int *iter =\n@@ -96,9 +144,26 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \twhile (1) {\n \t\tstruct dir_iterator_level *level =\n \t\t\t&iter->levels[iter->levels_nr - 1];\n-\t\tstruct dirent *de;\n \n-\t\tif (!level->initialized) {\n+\t\tif (level->dir_state == DIR_STATE_PUSHED) {\n+\t\t\tlevel->dir_state = DIR_STATE_PRE_ITERATION;\n+\n+\t\t\t/* We may not want the root directory to be iterated over */\n+\t\t\tif ((iter->flags & DIR_ITERATOR_PRE_ORDER_TRAVERSAL) && (\n+\t\t\t\titer->levels_nr != 1 ||\n+\t\t\t\t(iter->flags & DIR_ITERATOR_LIST_ROOT_DIR))) {\n+\t\t\t\t/*\n+\t\t\t\t * This will only error if we fail to lstat() the\n+\t\t\t\t * root directory. In this case, we bail.\n+\t\t\t\t */\n+\t\t\t\tif (adjust_iterator_data(iter, level)) {\n+\t\t\t\t\tlevel->dir_state = DIR_STATE_EXHAUSTED;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\n+\t\t\t\treturn ITER_OK;\n+\t\t\t}\n+\t\t} else if (level->dir_state == DIR_STATE_PRE_ITERATION) {\n \t\t\t/*\n \t\t\t * Note: dir_iterator_begin() ensures that\n \t\t\t * path is not the empty string.\n@@ -108,64 +173,40 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \t\t\tlevel->prefix_len = iter->base.path.len;\n \n \t\t\tlevel->dir = opendir(iter->base.path.buf);\n-\t\t\tif (!level->dir && errno != ENOENT) {\n-\t\t\t\twarning(\"error opening directory %s: %s\",\n-\t\t\t\t\titer->base.path.buf, strerror(errno));\n-\t\t\t\t/* Popping the level is handled below */\n-\t\t\t}\n-\n-\t\t\tlevel->initialized = 1;\n-\t\t} else if (S_ISDIR(iter->base.st.st_mode)) {\n-\t\t\tif (level->dir_state == DIR_STATE_ITER) {\n+\t\t\tif (!level->dir) {\n \t\t\t\t/*\n-\t\t\t\t * The directory was just iterated\n-\t\t\t\t * over; now prepare to iterate into\n-\t\t\t\t * it.\n+\t\t\t\t * This level wasn't opened sucessfully; pretend we\n+\t\t\t\t * iterated through it already.\n \t\t\t\t */\n-\t\t\t\tpush_dir_level(iter, level);\n+\t\t\t\tif (errno != ENOENT)\n+\t\t\t\t\twarning(\"error opening directory %s: %s\",\n+\t\t\t\t\t\titer->base.path.buf, strerror(errno));\n+\n+\t\t\t\tlevel->dir_state = DIR_STATE_POST_ITERATION;\n \t\t\t\tcontinue;\n-\t\t\t} else {\n-\t\t\t\t/*\n-\t\t\t\t * The directory has already been\n-\t\t\t\t * iterated over and iterated into;\n-\t\t\t\t * we're done with it.\n-\t\t\t\t */\n \t\t\t}\n-\t\t}\n-\n-\t\tif (!level->dir) {\n-\t\t\t/*\n-\t\t\t * This level is exhausted (or wasn't opened\n-\t\t\t * successfully); pop up a level.\n-\t\t\t */\n-\t\t\tif (pop_dir_level(iter) == 0)\n-\t\t\t\treturn dir_iterator_abort(dir_iterator);\n \n-\t\t\tcontinue;\n-\t\t}\n+\t\t\tlevel->dir_state = DIR_STATE_ITERATING;\n+\t\t} else if (level->dir_state == DIR_STATE_ITERATING) {\n+\t\t\tstruct dirent *de;\n \n-\t\t/*\n-\t\t * Loop until we find an entry that we can give back\n-\t\t * to the caller:\n-\t\t */\n-\t\twhile (1) {\n \t\t\tstrbuf_setlen(&iter->base.path, level->prefix_len);\n \t\t\terrno = 0;\n \t\t\tde = readdir(level->dir);\n \n \t\t\tif (!de) {\n-\t\t\t\t/* This level is exhausted; pop up a level. */\n+\t\t\t\t/* In case of readdir() error */\n \t\t\t\tif (errno) {\n \t\t\t\t\twarning(\"error reading directory %s: %s\",\n \t\t\t\t\t\titer->base.path.buf, strerror(errno));\n-\t\t\t\t} else if (closedir(level->dir))\n+\t\t\t\t}\n+\n+\t\t\t\tif (closedir(level->dir))\n \t\t\t\t\twarning(\"error closing directory %s: %s\",\n \t\t\t\t\t\titer->base.path.buf, strerror(errno));\n \n-\t\t\t\tlevel->dir = NULL;\n-\t\t\t\tif (pop_dir_level(iter) == 0)\n-\t\t\t\t\treturn dir_iterator_abort(dir_iterator);\n-\t\t\t\tbreak;\n+\t\t\t\tlevel->dir_state = DIR_STATE_POST_ITERATION;\n+\t\t\t\tcontinue;\n \t\t\t}\n \n \t\t\tif (is_dot_or_dotdot(de->d_name))\n@@ -176,7 +217,37 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)\n \t\t\tif (adjust_iterator_data(iter, level))\n \t\t\t\tcontinue;\n \n+\t\t\tif (S_ISDIR(iter->base.st.st_mode)) {\n+\t\t\t\tpush_dir_level(iter, &iter->base.st);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\n \t\t\treturn ITER_OK;\n+\t\t} else if (level->dir_state == DIR_STATE_POST_ITERATION) {\n+\t\t\tlevel->dir_state = DIR_STATE_EXHAUSTED;\n+\n+\t\t\tstrbuf_setlen(&iter->base.path, level->prefix_len);\n+\t\t\t/*\n+\t\t\t * Since we are iterating through the dirpath\n+\t\t\t * after we have gone through it, we still need\n+\t\t\t * to get rid of the trailing slash we appended.\n+\t\t\t */\n+\t\t\tstrbuf_strip_suffix(&iter->base.path, \"/\");\n+\n+\t\t\t/* We may not want the root directory to be iterated over */\n+\t\t\tif ((iter->flags & DIR_ITERATOR_POST_ORDER_TRAVERSAL) && (\n+\t\t\t\titer->levels_nr != 1 ||\n+\t\t\t\t(iter->flags & DIR_ITERATOR_LIST_ROOT_DIR))) {\n+\t\t\t\t/*\n+\t\t\t\t * In this state, adjust_iterator_data() should never return\n+\t\t\t\t * an error.\n+\t\t\t\t */\n+\t\t\t\tadjust_iterator_data(iter, level);\n+\t\t\t\treturn ITER_OK;\n+\t\t\t}\n+\t\t} else if (level->dir_state == DIR_STATE_EXHAUSTED) {\n+\t\t\tif (pop_dir_level(iter) == 0)\n+\t\t\t\treturn dir_iterator_abort(dir_iterator);\n \t\t}\n \t}\n }\n@@ -202,21 +273,36 @@ int dir_iterator_abort(struct dir_iterator *dir_iterator)\n \treturn ITER_DONE;\n }\n \n-struct dir_iterator *dir_iterator_begin(const char *path)\n+struct dir_iterator *dir_iterator_begin(const char *path, unsigned flags)\n {\n-\tstruct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));\n-\tstruct dir_iterator *dir_iterator = &iter->base;\n+\tstruct dir_iterator_int *iter;\n+\tstruct dir_iterator *dir_iterator;\n+\tstruct stat st;\n \n \tif (!path || !*path)\n \t\tdie(\"BUG: empty path passed to dir_iterator_begin()\");\n \n+\tif (lstat(path, &st) < 0)\n+\t\treturn NULL;\n+\n+\tif (!S_ISDIR(st.st_mode)) {\n+\t\terrno = ENOTDIR;\n+\t\treturn NULL;\n+\t}\n+\n+\titer = xcalloc(1, sizeof(*iter));\n+\tdir_iterator = &iter->base;\n+\n+\titer->flags = flags;\n+\tdir_iterator->st = st;\n+\n \tstrbuf_init(&iter->base.path, PATH_MAX);\n \tstrbuf_addstr(&iter->base.path, path);\n \n \tALLOC_GROW(iter->levels, 10, iter->levels_alloc);\n+\titer->levels_nr = 0;\n \n-\titer->levels_nr = 1;\n-\titer->levels[0].initialized = 0;\n+\tpush_dir_level(iter, &dir_iterator->st);\n \n \treturn dir_iterator;\n }\ndiff --git a/dir-iterator.h b/dir-iterator.h\nindex 27739e6..e801748 100644\n--- a/dir-iterator.h\n+++ b/dir-iterator.h\n@@ -5,19 +5,17 @@\n  * Iterate over a directory tree.\n  *\n  * Iterate over a directory tree, recursively, including paths of all\n- * types and hidden paths. Skip \".\" and \"..\" entries and don't follow\n- * symlinks except for the original path.\n+ * types and hidden paths. Skip \".\" and \"..\" entries.\n  *\n  * Every time dir_iterator_advance() is called, update the members of\n  * the dir_iterator structure to reflect the next path in the\n  * iteration. The order that paths are iterated over within a\n- * directory is undefined, but directory paths are always iterated\n- * over before the subdirectory contents.\n+ * directory is undefined.\n  *\n  * A typical iteration looks like this:\n  *\n  *     int ok;\n- *     struct iterator *iter = dir_iterator_begin(path);\n+ *     struct iterator *iter = dir_iterator_begin(path, flags);\n  *\n  *     while ((ok = dir_iterator_advance(iter)) == ITER_OK) {\n  *             if (want_to_stop_iteration()) {\n@@ -38,6 +36,26 @@\n  * dir_iterator_advance() again.\n  */\n \n+/*\n+ * Possible flags for dir_iterator_begin().\n+ *\n+ * - DIR_ITERATOR_PRE_ORDER_TRAVERSAL: the iterator shall return\n+ *   a dirpath it has found before iterating through that directory's\n+ * contents.\n+ * - DIR_ITERATOR_POST_ORDER_TRAVERSAL: the iterator shall return\n+ *   a dirpath it has found after iterating through that directory's\n+ *   contents.\n+ * - DIR_ITERATOR_LIST_ROOT_DIR: the iterator shall return the dirpath\n+ *   of the root directory it is iterating through if either\n+ *   DIR_ITERATOR_PRE_ORDER_TRAVERSAL or DIR_ITERATOR_POST_ORDER_TRAVERSAL\n+ *   is set.\n+ *\n+ * All flags can be used in any combination.\n+ */\n+#define DIR_ITERATOR_PRE_ORDER_TRAVERSAL (1 << 0)\n+#define DIR_ITERATOR_POST_ORDER_TRAVERSAL (1 << 1)\n+#define DIR_ITERATOR_LIST_ROOT_DIR (1 << 2)\n+\n struct dir_iterator {\n \t/* The current path: */\n \tstruct strbuf path;\n@@ -57,15 +75,16 @@ struct dir_iterator {\n };\n \n /*\n- * Start a directory iteration over path. Return a dir_iterator that\n- * holds the internal state of the iteration.\n+ * Start a directory iteration over path, with options specified in\n+ * 'flags'. Return a dir_iterator that holds the internal state of\n+ * the iteration.\n  *\n  * The iteration includes all paths under path, not including path\n  * itself and not including \".\" or \"..\" entries.\n  *\n  * path is the starting directory. An internal copy will be made.\n  */\n-struct dir_iterator *dir_iterator_begin(const char *path);\n+struct dir_iterator *dir_iterator_begin(const char *path, unsigned flags);\n \n /*\n  * Advance the iterator to the first or next item and return ITER_OK.\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex c9d900f..cb492cb 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3482,16 +3482,23 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {\n \n static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)\n {\n+\tstruct dir_iterator *diter;\n \tstruct files_ref_store *refs =\n \t\tfiles_downcast(ref_store, REF_STORE_READ,\n \t\t\t       \"reflog_iterator_begin\");\n-\tstruct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));\n-\tstruct ref_iterator *ref_iterator = &iter->base;\n+\tstruct files_reflog_iterator *iter;\n+\tstruct ref_iterator *ref_iterator;\n+\n \tstruct strbuf sb = STRBUF_INIT;\n+\tfiles_reflog_path(refs, &sb, NULL);\n+\tif (!(diter = dir_iterator_begin(sb.buf, 0)))\n+\t\treturn empty_ref_iterator_begin();\n+\n+\titer = xcalloc(1, sizeof(*iter));\n+\tref_iterator = &iter->base;\n \n \tbase_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);\n-\tfiles_reflog_path(refs, &sb, NULL);\n-\titer->dir_iterator = dir_iterator_begin(sb.buf);\n+\titer->dir_iterator = diter;\n \titer->ref_store = ref_store;\n \tstrbuf_release(&sb);\n \treturn ref_iterator;\ndiff --git a/t/helper/test-dir-iterator.c b/t/helper/test-dir-iterator.c\nindex a7d1470..3b4948b 100644\n--- a/t/helper/test-dir-iterator.c\n+++ b/t/helper/test-dir-iterator.c\n@@ -5,15 +5,38 @@\n \n int cmd_main(int argc, const char **argv)\n {\n+\tconst char **myargv = argv;\n+\tint myargc = argc;\n+\n \tstruct strbuf path = STRBUF_INIT;\n \tstruct dir_iterator *diter;\n \n-\tif (argc < 2)\n-\t\tdie(\"BUG: test-dir-iterator needs one argument\");\n+\tunsigned flag = 0;\n+\n+\twhile (--myargc && starts_with(*++myargv, \"--\")) {\n+\t\tif (!strcmp(*myargv, \"--pre-order\"))\n+\t\t\tflag |= DIR_ITERATOR_PRE_ORDER_TRAVERSAL;\n+\t\telse if (!strcmp(*myargv, \"--post-order\"))\n+\t\t\tflag |= DIR_ITERATOR_POST_ORDER_TRAVERSAL;\n+\t\telse if (!strcmp(*myargv, \"--list-root-dir\"))\n+\t\t\tflag |= DIR_ITERATOR_LIST_ROOT_DIR;\n+\t\telse if (!strcmp(*myargv, \"--\")) {\n+\t\t\tmyargc--;\n+\t\t\tmyargv++;\n+\t\t\tbreak;\n+\t\t} else\n+\t\t\tdie(\"Unrecognized option: %s\", *myargv);\n+\t}\n \n-\tstrbuf_add(&path, argv[1], strlen(argv[1]));\n+\tif (myargc != 1)\n+\t\tdie(\"expected exactly one non-option argument\");\n+\tstrbuf_addstr(&path, *myargv);\n \n-\tditer = dir_iterator_begin(path.buf);\n+\tditer = dir_iterator_begin(path.buf, flag);\n+\tif (diter == NULL) {\n+\t\tprintf(\"begin failed: %d\\n\", errno);\n+\t\treturn 0;\n+\t}\n \n \twhile (dir_iterator_advance(diter) == ITER_OK) {\n \t\tif (S_ISDIR(diter->st.st_mode))\ndiff --git a/t/t0066-dir-iterator.sh b/t/t0066-dir-iterator.sh\nindex 46e5ce5..c2a28e7 100755\n--- a/t/t0066-dir-iterator.sh\n+++ b/t/t0066-dir-iterator.sh\n@@ -15,31 +15,42 @@ test_expect_success 'setup' '\n \t>dir/d/e/d/a &&\n \n \tmkdir -p dir2/a/b/c/ &&\n-\t>dir2/a/b/c/d\n+\t>dir2/a/b/c/d &&\n+\n+\tmkdir dir3 &&\n+\t>file\n '\n \n-test_expect_success 'dir-iterator should iterate through all files' '\n-\tcat >expect-sorted-output <<-\\EOF &&\n-\t[d] (a) [a] ./dir/a\n-\t[d] (a/b) [b] ./dir/a/b\n-\t[d] (a/b/c) [c] ./dir/a/b/c\n-\t[d] (d) [d] ./dir/d\n-\t[d] (d/e) [e] ./dir/d/e\n-\t[d] (d/e/d) [d] ./dir/d/e/d\n-\t[f] (a/b/c/d) [d] ./dir/a/b/c/d\n-\t[f] (a/e) [e] ./dir/a/e\n-\t[f] (b) [b] ./dir/b\n-\t[f] (c) [c] ./dir/c\n-\t[f] (d/e/d/a) [a] ./dir/d/e/d/a\n-\tEOF\n+cat >expect-sorted-output <<-\\EOF &&\n+[d] (a) [a] ./dir/a\n+[d] (a/b) [b] ./dir/a/b\n+[d] (a/b/c) [c] ./dir/a/b/c\n+[d] (d) [d] ./dir/d\n+[d] (d/e) [e] ./dir/d/e\n+[d] (d/e/d) [d] ./dir/d/e/d\n+[f] (a/b/c/d) [d] ./dir/a/b/c/d\n+[f] (a/e) [e] ./dir/a/e\n+[f] (b) [b] ./dir/b\n+[f] (c) [c] ./dir/c\n+[f] (d/e/d/a) [a] ./dir/d/e/d/a\n+EOF\n \n-\ttest-dir-iterator ./dir >out &&\n+test_expect_success 'dir-iterator should iterate through all files' '\n+\ttest-dir-iterator --pre-order ./dir >out &&\n \tsort <out >./actual-pre-order-sorted-output &&\n \n \ttest_cmp expect-sorted-output actual-pre-order-sorted-output\n '\n \n-test_expect_success 'dir-iterator should list files in the correct order' '\n+test_expect_success 'dir-iterator should iterate through all files on post-order mode' '\n+\ttest-dir-iterator --post-order ./dir >out &&\n+\tsort <out >actual-post-order-sorted-output &&\n+\n+\ttest_cmp expect-sorted-output actual-post-order-sorted-output\n+'\n+\n+\n+test_expect_success 'dir-iterator should list files properly on pre-order mode' '\n \tcat >expect-pre-order-output <<-\\EOF &&\n \t[d] (a) [a] ./dir2/a\n \t[d] (a/b) [b] ./dir2/a/b\n@@ -47,9 +58,64 @@ test_expect_success 'dir-iterator should list files in the correct order' '\n \t[f] (a/b/c/d) [d] ./dir2/a/b/c/d\n \tEOF\n \n-\ttest-dir-iterator ./dir2 >actual-pre-order-output &&\n-\n+\ttest-dir-iterator --pre-order ./dir2 >actual-pre-order-output &&\n \ttest_cmp expect-pre-order-output actual-pre-order-output\n '\n \n+test_expect_success 'dir-iterator should list files properly on post-order mode' '\n+\tcat >expect-post-order-output <<-\\EOF &&\n+\t[f] (a/b/c/d) [d] ./dir2/a/b/c/d\n+\t[d] (a/b/c) [c] ./dir2/a/b/c\n+\t[d] (a/b) [b] ./dir2/a/b\n+\t[d] (a) [a] ./dir2/a\n+\tEOF\n+\n+\ttest-dir-iterator --post-order ./dir2 >actual-post-order-output &&\n+\ttest_cmp expect-post-order-output actual-post-order-output\n+'\n+\n+test_expect_success 'dir-iterator should list files properly on pre-order + post-order + root-dir mode' '\n+\tcat >expect-pre-order-post-order-root-dir-output <<-\\EOF &&\n+\t[d] (.) [dir2] ./dir2\n+\t[d] (a) [a] ./dir2/a\n+\t[d] (a/b) [b] ./dir2/a/b\n+\t[d] (a/b/c) [c] ./dir2/a/b/c\n+\t[f] (a/b/c/d) [d] ./dir2/a/b/c/d\n+\t[d] (a/b/c) [c] ./dir2/a/b/c\n+\t[d] (a/b) [b] ./dir2/a/b\n+\t[d] (a) [a] ./dir2/a\n+\t[d] (.) [dir2] ./dir2\n+\tEOF\n+\n+\ttest-dir-iterator --pre-order --post-order --list-root-dir ./dir2 >actual-pre-order-post-order-root-dir-output &&\n+\ttest_cmp expect-pre-order-post-order-root-dir-output actual-pre-order-post-order-root-dir-output\n+'\n+\n+test_expect_success 'dir-iterator should list root dir properly with relative directory' '\n+\tcat >expect-root-dir-output <<-\\EOF &&\n+\t[d] (.) [dir3] ./dir3/.\n+\tEOF\n+\n+\ttest-dir-iterator --pre-order --list-root-dir ./dir3/. >actual-root-dir-output &&\n+\ttest_cmp expect-root-dir-output actual-root-dir-output\n+'\n+\n+test_expect_success 'dir-iterator should return ENOENT upon opening non-existing directory' '\n+\tcat >expect-non-existing-dir-output <<-\\EOF &&\n+\tbegin failed: 2\n+\tEOF\n+\n+\ttest-dir-iterator ./dir666 >actual-non-existing-dir-output &&\n+\ttest_cmp expect-non-existing-dir-output actual-non-existing-dir-output\n+'\n+\n+test_expect_success 'dir-iterator should return ENOTDIR upon opening non-directory path' '\n+\tcat >expect-not-a-directory-output <<-\\EOF &&\n+\tbegin failed: 20\n+\tEOF\n+\n+\ttest-dir-iterator ./file >actual-not-a-directory-output &&\n+\ttest_cmp expect-not-a-directory-output actual-not-a-directory-output\n+'\n+\n test_done\n-- \n2.7.4 (Apple Git-66)\n\n"},{"id":"317942","messageId":"1493226219-33423-6-git-send-email-bnmvco@gmail.com","threadId":"45803","inReplyTo":"1493226219-33423-1-git-send-email-bnmvco@gmail.com","subject":"[PATCH v11 5/5] remove_subtree(): reimplement using iterators","fromName":"Daniel Ferreira","fromEmail":"bnmvco@gmail.com","sentAt":"2017-04-26T17:03:39Z","receivedAt":"2017-04-26T17:04:14Z","isPatch":true,"sender":{"key":"bnmvco@gmail.com","avatar":"https://avatars.githubusercontent.com/u/735397?v=4"},"body":"Use dir_iterator to traverse through remove_subtree()'s directory tree,\navoiding the need for recursive calls to readdir(). Simplify\nremove_subtree()'s code.\n\nA conversion similar in purpose was previously done at 46d092a\n(\"for_each_reflog(): reimplement using iterators\", 2016-05-21).\n\nSigned-off-by: Daniel Ferreira <bnmvco@gmail.com>\n---\n entry.c | 42 ++++++++++++++++--------------------------\n 1 file changed, 16 insertions(+), 26 deletions(-)\n\ndiff --git a/entry.c b/entry.c\nindex d2b512d..a939432 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -3,6 +3,8 @@\n #include \"dir.h\"\n #include \"streaming.h\"\n #include \"submodule.h\"\n+#include \"iterator.h\"\n+#include \"dir-iterator.h\"\n \n static void create_directories(const char *path, int path_len,\n \t\t\t       const struct checkout *state)\n@@ -45,33 +47,21 @@ static void create_directories(const char *path, int path_len,\n \tfree(buf);\n }\n \n-static void remove_subtree(struct strbuf *path)\n+static void remove_subtree(const char *path)\n {\n-\tDIR *dir = opendir(path->buf);\n-\tstruct dirent *de;\n-\tint origlen = path->len;\n-\n-\tif (!dir)\n-\t\tdie_errno(\"cannot opendir '%s'\", path->buf);\n-\twhile ((de = readdir(dir)) != NULL) {\n-\t\tstruct stat st;\n-\n-\t\tif (is_dot_or_dotdot(de->d_name))\n-\t\t\tcontinue;\n-\n-\t\tstrbuf_addch(path, '/');\n-\t\tstrbuf_addstr(path, de->d_name);\n-\t\tif (lstat(path->buf, &st))\n-\t\t\tdie_errno(\"cannot lstat '%s'\", path->buf);\n-\t\tif (S_ISDIR(st.st_mode))\n-\t\t\tremove_subtree(path);\n-\t\telse if (unlink(path->buf))\n-\t\t\tdie_errno(\"cannot unlink '%s'\", path->buf);\n-\t\tstrbuf_setlen(path, origlen);\n+\tstruct dir_iterator *diter = dir_iterator_begin(path,\n+\t\tDIR_ITERATOR_POST_ORDER_TRAVERSAL | DIR_ITERATOR_LIST_ROOT_DIR);\n+\tif (!diter) {\n+\t\tdie_errno(\"cannot remove path '%s'\", path);\n+\t}\n+\n+\twhile (dir_iterator_advance(diter) == ITER_OK) {\n+\t\tif (S_ISDIR(diter->st.st_mode)) {\n+\t\t\tif (rmdir(diter->path.buf))\n+\t\t\t\tdie_errno(\"cannot rmdir '%s'\", diter->path.buf);\n+\t\t} else if (unlink(diter->path.buf))\n+\t\t\tdie_errno(\"cannot unlink '%s'\", diter->path.buf);\n \t}\n-\tclosedir(dir);\n-\tif (rmdir(path->buf))\n-\t\tdie_errno(\"cannot rmdir '%s'\", path->buf);\n }\n \n static int create_file(const char *path, unsigned int mode)\n@@ -312,7 +302,7 @@ int checkout_entry(struct cache_entry *ce,\n \t\t\t\treturn 0;\n \t\t\tif (!state->force)\n \t\t\t\treturn error(\"%s is a directory\", path.buf);\n-\t\t\tremove_subtree(&path);\n+\t\t\tremove_subtree(path.buf);\n \t\t} else if (unlink(path.buf))\n \t\t\treturn error_errno(\"unable to unlink old '%s'\", path.buf);\n \t} else if (state->not_new)\n-- \n2.7.4 (Apple Git-66)\n\n"}]}