Changeset 7956257 in mainline


Ignore:
Timestamp:
2026-03-17T12:36:53Z (33 hours ago)
Author:
Vít Skalický <skalicky@…>
Children:
4a998bf9
Parents:
731bdd4
git-author:
Vít Skalický <skalicky@…> (2026-03-06 14:33:21)
git-committer:
Vít Skalický <skalicky@…> (2026-03-17 12:36:53)
Message:

Add logging to cmpdirs

File:
1 edited

Legend:

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

    r731bdd4 r7956257  
    5353#define NAME_BUFFER_SIZE 256
    5454
     55// Macros to make handling error less clumbersome
     56#define _check_ok(rc, action, ...) if ((rc) != EOK) {fprintf(stderr, __VA_ARGS__); log_msg(LOG_DEFAULT, LVL_ERROR, __VA_ARGS__ ); action; }
     57#define check_ok(rc, label, ...) _check_ok(rc, goto label, __VA_ARGS__)
     58#define check_ok_break(rc, ...) _check_ok(rc, break, __VA_ARGS__)
     59
    5560static void print_usage(void);
    5661
     
    7883                default:
    7984                        fprintf(stderr,
    80                             "Unknown error while parsing command line options");
     85                            "Unknown error while parsing command line options\n");
    8186                        errflg++;
    8287                        break;
     
    107112        int handle1;
    108113        int handle2;
    109         rc = vfs_lookup(argv[0], WALK_DIRECTORY | WALK_REGULAR | WALK_MOUNT_POINT, &handle1);
    110         if (rc != EOK) goto close_end;
    111         rc = vfs_lookup(argv[1], WALK_DIRECTORY | WALK_REGULAR | WALK_MOUNT_POINT, &handle2);
    112         if (rc != EOK) goto close1;
     114        rc = vfs_lookup(argv[0], 0, &handle1);
     115        check_ok(rc, close_end, "Failed to lookup \"%s\"\n", argv[0]);
     116        rc = vfs_lookup(argv[1], 0, &handle2);
     117        check_ok(rc, close1, "Failed to lookup \"%s\"\n", argv[1]);
    113118
    114119        bool equal;
     
    225230 *  - both handles represent a file, not dir
    226231 *  - both files have the same size
    227  *  - both buffers are the size CHUNK_SIZE */
     232 *  - both buffers are the size CHUNK_SIZE
     233 *  - both handle are open for reading */
    228234static errno_t content_equal(int handle1, vfs_stat_t stat1, char* buffer1, int handle2, vfs_stat_t stat2, char* buffer2, bool *equal_out) {
    229235        errno_t rc = EOK;
     
    231237        aoff64_t pos = 0;
    232238
    233         //todo open the files?
    234 
    235239        while (pos < stat1.size && equal) {
    236240                ssize_t read1, read2;
    237241                rc = vfs_read_short(handle1, pos, buffer1, CHUNK_SIZE, &read1);
    238                 if (rc != EOK) break;
     242                check_ok_break(rc, "Failed to read handle %d\n", handle1);
    239243                rc = vfs_read_short(handle2, pos, buffer2, CHUNK_SIZE, &read2);
    240                 if (rc != EOK) break;
    241                 size_t read = min(read1, read2);
     244                check_ok_break(rc, "Failed to read handle %d\n", handle2);
     245                const size_t read = min(read1, read2);
    242246                equal = (0 == memcmp(buffer1, buffer2, read));
    243247                pos += read;
     
    247251                }
    248252                if (read == 0) {
    249                         // something bad has happened - one has read 0 while other not and we are not at the end yet. That is not possible
     253                        // something bad has happened - one has read 0 while other not and we are not at the end yet. That is not supposed to be possible
    250254                        log_msg(LOG_DEFAULT, LVL_ERROR, "Reading of one file ended sooner than the other.");
    251255                        equal = false;
     
    254258        }
    255259        *equal_out = equal;
    256         //todo close the files. do it here?
    257260        return rc;
    258261}
     
    264267        errno_t rc = EOK;
    265268        bool equal = true;
     269
     270        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Root handles are: %d, %d\n", root_handle1, root_handle2);
     271
    266272        // stack of handles to compare
    267273        // All handles on the stack are NOT open yet, but must be put() in the end
    268274        struct stack s;
    269275        rc = stack_new(&s);
    270         if (rc != EOK) goto close_end;
     276        check_ok(rc, close_end, "Failed to create stack\n");
    271277
    272278        char *file_buffer1 = malloc(CHUNK_SIZE);
    273         if (file_buffer1 == NULL) {
    274                 rc = ENOMEM;
    275                 goto close1;
    276         }
     279        if (file_buffer1 == NULL) rc = ENOMEM;
     280        check_ok(rc, close1, "No memory for file buffer #1 (%d bytes needed)\n", CHUNK_SIZE);
     281
    277282        char *file_buffer2 = malloc(CHUNK_SIZE);
    278         if (file_buffer2 == NULL) {
    279                 rc = ENOMEM;
    280                 goto close2;
    281         }
     283        if (file_buffer2 == NULL) rc = ENOMEM;
     284        check_ok(rc, close2, "No memory for file buffer #2 (%d bytes needed)\n", CHUNK_SIZE);
     285
    282286        char name_buffer[NAME_BUFFER_SIZE];
    283287
     
    295299                        goto close3;
    296300                }
    297                 //TODO open the handles here
    298301
    299302                // 1. compare metadata
     
    303306                vfs_stat_t stat1;
    304307                rc = vfs_stat(item.handle1, &stat1);
    305                 if (rc != EOK) goto close_inner;
     308                check_ok(rc, close_inner, "Cannot stat handle %d\n", item.handle1);
    306309                vfs_stat_t stat2;
    307310                rc = vfs_stat(item.handle2, &stat2);
    308                 if (rc != EOK) goto close_inner;
     311                check_ok(rc, close_inner, "Cannot stat handle %d\n", item.handle2);
    309312
    310313                rc = vfs_open(item.handle1, MODE_READ);
    311                 if (rc != EOK) goto close_inner;
     314                check_ok(rc, close_inner, "Cannot open handle %d\n", item.handle1);
    312315                rc = vfs_open(item.handle2, MODE_READ);
    313                 if (rc != EOK) goto close_inner;
     316                check_ok(rc, close_inner, "Cannot open handle %d\n", item.handle2);
    314317
    315318                equal = equal && stat_equal(stat1, stat2);
     
    319322                if ((stat1.is_file && stat1.is_directory) || (!stat1.is_file && !stat1.is_directory)) { // sanity check
    320323                        // WTF
    321                         log_msg(LOG_DEFAULT, LVL_FATAL, "Invalid entry encountered: It either claims to be both a directory and file at the same time or it claims to be neither.");
    322324                        rc = EINVAL;
    323                         goto close_inner;
     325                        check_ok(rc, close_inner, "Invalid entry encountered: It either claims to be both a directory and file at the same time or it claims to be neither.");
    324326                }
    325327                if (stat1.is_file) {
     
    348350                                rc = list_dir(item.handle1, pos, NAME_BUFFER_SIZE, name_buffer, &read);
    349351                                log_msg(LOG_DEFAULT, LVL_DEBUG2, "Read directory entry name: result %s, read size: %"PRIdn".", str_error_name(rc), read);
    350                                 if (rc != EOK) break;
     352                                check_ok_break(rc, "Failed to list handle %d\n", item.handle1);
    351353                                // check that the string is null-terminated before the end of the buffer
    352354                                if (str_nsize(name_buffer, NAME_BUFFER_SIZE) >= NAME_BUFFER_SIZE) {
    353355                                        rc = ENAMETOOLONG;
    354                                         break;
    355356                                }
     357                                check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE);
    356358                                // 2.
    357359                                int entry_handle1; // don't forget to put
    358                                 rc = vfs_walk(item.handle1, name_buffer, WALK_DIRECTORY | WALK_REGULAR | WALK_MOUNT_POINT, &entry_handle1);
    359                                 if (rc != EOK) break;
     360                                rc = vfs_walk(item.handle1, name_buffer, 0, &entry_handle1);
     361                                check_ok_break(rc, "(0) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle1);
    360362                                // 3.
    361363                                int entry_handle2; // must be put
    362                                 rc= vfs_walk(item.handle2, name_buffer, WALK_DIRECTORY | WALK_REGULAR | WALK_MOUNT_POINT, &entry_handle2);
     364                                rc= vfs_walk(item.handle2, name_buffer, 0, &entry_handle2);
    363365                                if (rc != EOK) {
     366                                        errno_t rc2 = vfs_put(entry_handle1);
     367                                        (void) rc2; // if put failed, I guess there is nothing I can do about it
    364368                                        if (rc == ENOENT) { // todo is ENOENT the correct one?
    365369                                                rc = EOK;
    366370                                                equal = false;
     371                                                break;
    367372                                        }
    368                                         errno_t rc2 = vfs_put(entry_handle1);
    369                                         (void) rc2; // if put failed, I guess there is nothing I can do about it
    370                                         break;
     373                                        check_ok_break(rc, "(1) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle2);
    371374                                }
    372375
     
    379382                                        (void) rc2; // if put failed, I guess there is nothing I can do about it
    380383                                        (void) rc3;
    381                                         break;
    382384                                }
     385                                check_ok_break(rc, "Failed to append to stack\n");
    383386                        }
    384387                        if (rc != EOK) goto close_inner;
     
    392395                                rc = list_dir(item.handle2, pos, NAME_BUFFER_SIZE, name_buffer, &read);
    393396                                log_msg(LOG_DEFAULT, LVL_DEBUG2, "Read directory entry name: result %s, read size: %"PRIdn".", str_error_name(rc), read);
    394                                 if (rc != EOK) break;
     397                                check_ok_break(rc, "Failed to list handle %d\n", item.handle2);
    395398                                // check that the string is null-terminated before the end of the buffer
    396399                                if (str_nsize(name_buffer, NAME_BUFFER_SIZE) >= NAME_BUFFER_SIZE) {
    397400                                        rc = ENAMETOOLONG;
    398                                         break;
    399401                                }
     402                                check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE);
    400403                                // 6.
    401404                                int entry_handle; // don't forget to put
    402                                 rc = vfs_walk(item.handle2, name_buffer, WALK_DIRECTORY | WALK_REGULAR | WALK_MOUNT_POINT, &entry_handle);
     405                                rc = vfs_walk(item.handle1, name_buffer, 0, &entry_handle);
    403406                                errno_t rc2 = vfs_put(entry_handle);
    404407                                (void) rc2; // if put failed, I guess there is nothing I can do about it
     
    407410                                                rc = EOK;
    408411                                                equal = false;
     412                                                break;
    409413                                        }
    410                                         break;
     414                                        check_ok_break(rc, "(2) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle1);
    411415                                }
    412416                        }
Note: See TracChangeset for help on using the changeset viewer.