Changeset 4a998bf9 in mainline


Ignore:
Timestamp:
2026-03-17T12:36:58Z (22 hours ago)
Author:
Vít Skalický <skalicky@…>
Children:
7c2a3c7
Parents:
7956257
git-author:
Vít Skalický <skalicky@…> (2026-03-12 10:44:21)
git-committer:
Vít Skalický <skalicky@…> (2026-03-17 12:36:58)
Message:

Fix directory listing

The code for listing entries in a directory was wrong.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/cmpdirs/cmpdirs.c

    r7956257 r4a998bf9  
    4646#include <str_error.h>
    4747#include <io/log.h>
     48#include <io/logctl.h>
    4849
    4950#define NAME  "cmpdirs"
     
    108109        rc = log_init(NAME);
    109110        if (rc != EOK) goto close_end;
     111        logctl_set_log_level(NAME, LVL_DEBUG2);
    110112
    111113        // lookup both directories
     
    114116        rc = vfs_lookup(argv[0], 0, &handle1);
    115117        check_ok(rc, close_end, "Failed to lookup \"%s\"\n", argv[0]);
     118        log_msg(LOG_DEFAULT, LVL_DEBUG2, "fd of \"%s\" is %d", argv[0], handle1);
    116119        rc = vfs_lookup(argv[1], 0, &handle2);
    117120        check_ok(rc, close1, "Failed to lookup \"%s\"\n", argv[1]);
     121        log_msg(LOG_DEFAULT, LVL_DEBUG2, "fd of \"%s\" is %d", argv[1], handle2);
    118122
    119123        bool equal;
     
    338342                        // 3. lookup it in handle2
    339343                        // 4. add both new handles to stack
    340                         // 5. iterate again over entries of handel2 -> gives you entry name
    341                         // 6. try to look it up in handle1 jut to check that it exists
    342 
    343                         // 5. and 6. are probably redundant, since the sizes of both directories must be equal.
    344344
    345345                        for (ssize_t pos = 0;
    346346                                ((size_t) pos) < stat1.size;
    347                                 pos++) {
     347                                ) {
    348348                                // 1.
    349349                                ssize_t read;
    350350                                rc = list_dir(item.handle1, pos, NAME_BUFFER_SIZE, name_buffer, &read);
    351351                                log_msg(LOG_DEFAULT, LVL_DEBUG2, "Read directory entry name: result %s, read size: %"PRIdn".", str_error_name(rc), read);
    352                                 check_ok_break(rc, "Failed to list handle %d\n", item.handle1);
     352                                check_ok_break(rc, "Failed (1) to list handle %d, pos %"PRIdn", total size of directory: %"PRIu64"\n", item.handle1, pos, stat1.size);
     353                                // Calculate the real length of the entry name (excluding null-terminator)
     354                                size_t name_size = str_nsize(name_buffer, NAME_BUFFER_SIZE);
    353355                                // check that the string is null-terminated before the end of the buffer
    354                                 if (str_nsize(name_buffer, NAME_BUFFER_SIZE) >= NAME_BUFFER_SIZE) {
     356                                if (name_size >= NAME_BUFFER_SIZE) {
    355357                                        rc = ENAMETOOLONG;
    356358                                }
    357359                                check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE);
     360                                // advance pos to after the name of the entry (add 1 for null-terminator)
     361                                pos += read; // NOLINT(*-narrowing-conversions) because name_size < NAME_BUFFER_SIZE
     362                                log_msg(LOG_DEFAULT, LVL_DEBUG2, "Listed %d: pos %"PRIdn", name: %s\n", item.handle1, pos, name_buffer);
    358363                                // 2.
    359364                                int entry_handle1; // don't forget to put
     
    386391                        }
    387392                        if (rc != EOK) goto close_inner;
    388 
    389                         // 5.
    390                         for (ssize_t pos = 0;
    391                                 ((size_t) pos) < stat2.size; // must be equal to stat1.size anyway
    392                                 pos++) {
    393                                 // 5.
    394                                 ssize_t read;
    395                                 rc = list_dir(item.handle2, pos, NAME_BUFFER_SIZE, name_buffer, &read);
    396                                 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Read directory entry name: result %s, read size: %"PRIdn".", str_error_name(rc), read);
    397                                 check_ok_break(rc, "Failed to list handle %d\n", item.handle2);
    398                                 // check that the string is null-terminated before the end of the buffer
    399                                 if (str_nsize(name_buffer, NAME_BUFFER_SIZE) >= NAME_BUFFER_SIZE) {
    400                                         rc = ENAMETOOLONG;
    401                                 }
    402                                 check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE);
    403                                 // 6.
    404                                 int entry_handle; // don't forget to put
    405                                 rc = vfs_walk(item.handle1, name_buffer, 0, &entry_handle);
    406                                 errno_t rc2 = vfs_put(entry_handle);
    407                                 (void) rc2; // if put failed, I guess there is nothing I can do about it
    408                                 if (rc != EOK) {
    409                                         if (rc == ENOENT) { // todo is ENOENT the correct one?
    410                                                 rc = EOK;
    411                                                 equal = false;
    412                                                 break;
    413                                         }
    414                                         check_ok_break(rc, "(2) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle1);
    415                                 }
    416                         }
    417                         if (rc != EOK) goto close_inner;
    418393                }
    419394
Note: See TracChangeset for help on using the changeset viewer.