Changeset 7956257 in mainline
- Timestamp:
- 2026-03-17T12:36:53Z (33 hours ago)
- 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)
- File:
-
- 1 edited
-
uspace/app/cmpdirs/cmpdirs.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/cmpdirs/cmpdirs.c
r731bdd4 r7956257 53 53 #define NAME_BUFFER_SIZE 256 54 54 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 55 60 static void print_usage(void); 56 61 … … 78 83 default: 79 84 fprintf(stderr, 80 "Unknown error while parsing command line options ");85 "Unknown error while parsing command line options\n"); 81 86 errflg++; 82 87 break; … … 107 112 int handle1; 108 113 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]); 113 118 114 119 bool equal; … … 225 230 * - both handles represent a file, not dir 226 231 * - 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 */ 228 234 static errno_t content_equal(int handle1, vfs_stat_t stat1, char* buffer1, int handle2, vfs_stat_t stat2, char* buffer2, bool *equal_out) { 229 235 errno_t rc = EOK; … … 231 237 aoff64_t pos = 0; 232 238 233 //todo open the files?234 235 239 while (pos < stat1.size && equal) { 236 240 ssize_t read1, read2; 237 241 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); 239 243 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); 242 246 equal = (0 == memcmp(buffer1, buffer2, read)); 243 247 pos += read; … … 247 251 } 248 252 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 possible253 // 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 250 254 log_msg(LOG_DEFAULT, LVL_ERROR, "Reading of one file ended sooner than the other."); 251 255 equal = false; … … 254 258 } 255 259 *equal_out = equal; 256 //todo close the files. do it here?257 260 return rc; 258 261 } … … 264 267 errno_t rc = EOK; 265 268 bool equal = true; 269 270 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Root handles are: %d, %d\n", root_handle1, root_handle2); 271 266 272 // stack of handles to compare 267 273 // All handles on the stack are NOT open yet, but must be put() in the end 268 274 struct stack s; 269 275 rc = stack_new(&s); 270 if (rc != EOK) goto close_end;276 check_ok(rc, close_end, "Failed to create stack\n"); 271 277 272 278 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 277 282 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 282 286 char name_buffer[NAME_BUFFER_SIZE]; 283 287 … … 295 299 goto close3; 296 300 } 297 //TODO open the handles here298 301 299 302 // 1. compare metadata … … 303 306 vfs_stat_t stat1; 304 307 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); 306 309 vfs_stat_t stat2; 307 310 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); 309 312 310 313 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); 312 315 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); 314 317 315 318 equal = equal && stat_equal(stat1, stat2); … … 319 322 if ((stat1.is_file && stat1.is_directory) || (!stat1.is_file && !stat1.is_directory)) { // sanity check 320 323 // 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.");322 324 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."); 324 326 } 325 327 if (stat1.is_file) { … … 348 350 rc = list_dir(item.handle1, pos, NAME_BUFFER_SIZE, name_buffer, &read); 349 351 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); 351 353 // check that the string is null-terminated before the end of the buffer 352 354 if (str_nsize(name_buffer, NAME_BUFFER_SIZE) >= NAME_BUFFER_SIZE) { 353 355 rc = ENAMETOOLONG; 354 break;355 356 } 357 check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE); 356 358 // 2. 357 359 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); 360 362 // 3. 361 363 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); 363 365 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 364 368 if (rc == ENOENT) { // todo is ENOENT the correct one? 365 369 rc = EOK; 366 370 equal = false; 371 break; 367 372 } 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); 371 374 } 372 375 … … 379 382 (void) rc2; // if put failed, I guess there is nothing I can do about it 380 383 (void) rc3; 381 break;382 384 } 385 check_ok_break(rc, "Failed to append to stack\n"); 383 386 } 384 387 if (rc != EOK) goto close_inner; … … 392 395 rc = list_dir(item.handle2, pos, NAME_BUFFER_SIZE, name_buffer, &read); 393 396 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); 395 398 // check that the string is null-terminated before the end of the buffer 396 399 if (str_nsize(name_buffer, NAME_BUFFER_SIZE) >= NAME_BUFFER_SIZE) { 397 400 rc = ENAMETOOLONG; 398 break;399 401 } 402 check_ok_break(rc, "Name too long (limit is %d excluding null-terminator)", NAME_BUFFER_SIZE); 400 403 // 6. 401 404 int entry_handle; // don't forget to put 402 rc = vfs_walk(item.handle 2, name_buffer, WALK_DIRECTORY | WALK_REGULAR | WALK_MOUNT_POINT, &entry_handle);405 rc = vfs_walk(item.handle1, name_buffer, 0, &entry_handle); 403 406 errno_t rc2 = vfs_put(entry_handle); 404 407 (void) rc2; // if put failed, I guess there is nothing I can do about it … … 407 410 rc = EOK; 408 411 equal = false; 412 break; 409 413 } 410 break;414 check_ok_break(rc, "(2) Failed to find entry \"%s\" in directory of handle %d\n", name_buffer, item.handle1); 411 415 } 412 416 }
Note:
See TracChangeset
for help on using the changeset viewer.
