{"thread":{"id":"63665","subject":"[PATCH] diff-no-index: do not reference .d_type member of struct dirent","startedAt":"2025-06-18T20:04:15Z","lastAt":"2025-06-18T20:37:44Z","messageCount":2,"participants":["Junio C Hamano","Collin Funk"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"520365","messageId":"xmqqh60ces03.fsf@gitster.g","threadId":"63665","inReplyTo":null,"subject":"[PATCH] diff-no-index: do not reference .d_type member of struct dirent","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-06-18T20:04:12Z","receivedAt":"2025-06-18T20:04:15Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Some platforms like AIX lack .d_type member in \"struct dirent\"; use\nthe DTYPE(e) macro instead of a direct reference to e->d_type and\nwhen it yields DT_UNKNOWN, find the real type with get_dtype().\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n * get_dtype() was designed for a typical\n\n    - prepare the path in a strbuf B\n    - opendir(B)\n    - loop over readdir(B)\n      - do things to path (B + e->d_name)\n\n   code structure, but because this code path does not use a strbuf\n   (instead the path given to opendir is a \"const char *\"), the\n   entire thing becomes messier than necessary.  get_dtype() has a\n   short-cut to avoid having to concatenate path+e->d_name and run\n   (l)stat() when e->d_type exists and known, but we need to open\n   code it here.\n\n diff-no-index.c | 14 +++++++++++++-\n 1 file changed, 13 insertions(+), 1 deletion(-)\n\ndiff --git a/diff-no-index.c b/diff-no-index.c\nindex 4aeeb98cfa..88ae4cee56 100644\n--- a/diff-no-index.c\n+++ b/diff-no-index.c\n@@ -41,12 +41,24 @@ static int read_directory_contents(const char *path, struct string_list *list,\n \n \twhile ((e = readdir_skip_dot_and_dotdot(dir))) {\n \t\tif (pathspec) {\n+\t\t\tint is_dir = 0;\n+\n \t\t\tstrbuf_setlen(&match, len);\n \t\t\tstrbuf_addstr(&match, e->d_name);\n+\t\t\tif (NOT_CONSTANT(DTYPE(e)) != DT_UNKNOWN) {\n+\t\t\t\tis_dir = (DTYPE(e) == DT_DIR);\n+\t\t\t} else {\n+\t\t\t\tstruct strbuf pathbuf = STRBUF_INIT;\n+\n+\t\t\t\tstrbuf_addstr(&pathbuf, path);\n+\t\t\t\tstrbuf_complete(&pathbuf, '/');\n+\t\t\t\tis_dir = get_dtype(e, &pathbuf, 0) == DT_DIR;\n+\t\t\t\tstrbuf_release(&pathbuf);\n+\t\t\t}\n \n \t\t\tif (!match_leading_pathspec(NULL, pathspec,\n \t\t\t\t\t\t    match.buf, match.len,\n-\t\t\t\t\t\t    0, NULL, e->d_type == DT_DIR ? 1 : 0))\n+\t\t\t\t\t\t    0, NULL, is_dir))\n \t\t\t\tcontinue;\n \t\t}\n \n-- \n2.50.0-228-g6e205fdad9\n\n\n"},{"id":"520366","messageId":"874iwcss4p.fsf@gmail.com","threadId":"63665","inReplyTo":"xmqqh60ces03.fsf@gitster.g","subject":"Re: [PATCH] diff-no-index: do not reference .d_type member of struct dirent","fromName":"Collin Funk","fromEmail":"collin.funk1@gmail.com","sentAt":"2025-06-18T20:37:42Z","receivedAt":"2025-06-18T20:37:44Z","isPatch":true,"sender":{"key":"collin.funk1@gmail.com","avatar":"https://avatars.githubusercontent.com/u/65689063?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Some platforms like AIX lack .d_type member in \"struct dirent\"; use\n> the DTYPE(e) macro instead of a direct reference to e->d_type and\n> when it yields DT_UNKNOWN, find the real type with get_dtype().\n>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> ---\n>  * get_dtype() was designed for a typical\n>\n>     - prepare the path in a strbuf B\n>     - opendir(B)\n>     - loop over readdir(B)\n>       - do things to path (B + e->d_name)\n>\n>    code structure, but because this code path does not use a strbuf\n>    (instead the path given to opendir is a \"const char *\"), the\n>    entire thing becomes messier than necessary.  get_dtype() has a\n>    short-cut to avoid having to concatenate path+e->d_name and run\n>    (l)stat() when e->d_type exists and known, but we need to open\n>    code it here.\n>\n>  diff-no-index.c | 14 +++++++++++++-\n>  1 file changed, 13 insertions(+), 1 deletion(-)\n\nTested on AIX 7.3 like the previous patch just to be safe and it works\nas expected.\n\nReviewed-by: Collin Funk <collin.funk1@gmail.com>\nTested-by: Collin Funk <collin.funk1@gmail.com>\n\nThanks,\nCollin\n"}]}