Differences From Artifact [689b4e3ed7]:
- File src/OFFile.m — part of check-in [d43eb177bc] at 2011-09-19 12:34:21 on branch trunk — BOOL naming conventions. (user: js, size: 16862) [annotate] [blame] [check-ins using]
To Artifact [c6a9166c97]:
- File
src/OFFile.m
— part of check-in
[e1e7ffa903]
at
2011-09-22 23:25:42
on branch trunk
— Exceptions are now autoreleased.
This is safe as an "exception loop" can't happen, since if allocating
an exception fails, it throws an OFAllocFailedException which is
preallocated and can always be thrown.So, the worst case would be that an autorelease of an exception fails,
triggering an OFOutOfMemoryException for which there is no memory,
resulting in an OFAllocFailedException to be thrown. (user: js, size: 17041) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
228 229 230 231 232 233 234 | { #ifndef _WIN32 if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], DIR_MODE)) #else if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #endif | | > | | | | 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | { #ifndef _WIN32 if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], DIR_MODE)) #else if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #endif @throw [OFCreateDirectoryFailedException exceptionWithClass: self path: path]; } + (OFArray*)filesInDirectoryAtPath: (OFString*)path { OFAutoreleasePool *pool; OFMutableArray *files = [OFMutableArray array]; #ifndef _WIN32 DIR *dir; struct dirent *dirent; if ((dir = opendir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) == NULL) @throw [OFOpenFileFailedException exceptionWithClass: self path: path mode: @"r"]; @try { pool = [[OFAutoreleasePool alloc] init]; while ((dirent = readdir(dir)) != NULL) { OFString *file; |
︙ | ︙ | |||
278 279 280 281 282 283 284 | WIN32_FIND_DATA fd; pool = [[OFAutoreleasePool alloc] init]; path = [path stringByAppendingString: @"\\*"]; if ((handle = FindFirstFile([path cString], &fd)) == INVALID_HANDLE_VALUE) | | | | | 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | WIN32_FIND_DATA fd; pool = [[OFAutoreleasePool alloc] init]; path = [path stringByAppendingString: @"\\*"]; if ((handle = FindFirstFile([path cString], &fd)) == INVALID_HANDLE_VALUE) @throw [OFOpenFileFailedException exceptionWithClass: self path: path mode: @"r"]; @try { OFAutoreleasePool *pool2 = [[OFAutoreleasePool alloc] init]; do { OFString *file; |
︙ | ︙ | |||
314 315 316 317 318 319 320 | return files; } + (void)changeToDirectory: (OFString*)path { if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) | | > | | > | | | > | | | > | | | | | | | | | | | | | | | | > | | | | 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | return files; } + (void)changeToDirectory: (OFString*)path { if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) @throw [OFChangeDirectoryFailedException exceptionWithClass: self path: path]; } #ifndef _PSP + (void)changeModeOfFile: (OFString*)path toMode: (mode_t)mode { # ifndef _WIN32 if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], mode)) @throw [OFChangeFileModeFailedException exceptionWithClass: self path: path mode: mode]; # else DWORD attributes = GetFileAttributes([path cString]); if (attributes == INVALID_FILE_ATTRIBUTES) @throw [OFChangeFileModeFailedException exceptionWithClass: self path: path mode: mode]; if ((mode / 100) & 2) attributes &= ~FILE_ATTRIBUTE_READONLY; else attributes |= FILE_ATTRIBUTE_READONLY; if (!SetFileAttributes([path cString], attributes)) @throw [OFChangeFileModeFailedException exceptionWithClass: self path: path mode: mode]; # endif } #endif + (OFDate*)modificationDateOfFile: (OFString*)path { struct stat s; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == -1) /* FIXME: Maybe use another exception? */ @throw [OFOpenFileFailedException exceptionWithClass: self path: path mode: @"r"]; /* FIXME: We could be more precise on some OSes */ return [OFDate dateWithTimeIntervalSince1970: s.st_mtime]; } #if !defined(_WIN32) && !defined(_PSP) + (void)changeOwnerOfFile: (OFString*)path toOwner: (OFString*)owner group: (OFString*)group { uid_t uid = -1; gid_t gid = -1; if (owner == nil && group == nil) @throw [OFInvalidArgumentException exceptionWithClass: self selector: _cmd]; # ifdef OF_THREADS [mutex lock]; @try { # endif if (owner != nil) { struct passwd *passwd; if ((passwd = getpwnam([owner cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) == NULL) @throw [OFChangeFileOwnerFailedException exceptionWithClass: self path: path owner: owner group: group]; uid = passwd->pw_uid; } if (group != nil) { struct group *group_; if ((group_ = getgrnam([group cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) == NULL) @throw [OFChangeFileOwnerFailedException exceptionWithClass: self path: path owner: owner group: group]; gid = group_->gr_gid; } # ifdef OF_THREADS } @finally { [mutex unlock]; } # endif if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], uid, gid)) @throw [OFChangeFileOwnerFailedException exceptionWithClass: self path: path owner: owner group: group]; } #endif + (void)copyFileAtPath: (OFString*)source toPath: (OFString*)destination { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; |
︙ | ︙ | |||
440 441 442 443 444 445 446 | destination = [OFString stringWithPath: destination, filename, nil]; } override = [self fileExistsAtPath: destination]; if ((buffer = malloc(of_pagesize)) == NULL) | | | | 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | destination = [OFString stringWithPath: destination, filename, nil]; } override = [self fileExistsAtPath: destination]; if ((buffer = malloc(of_pagesize)) == NULL) @throw [OFOutOfMemoryException exceptionWithClass: self requestedSize: of_pagesize]; @try { sourceFile = [OFFile fileWithPath: source mode: @"rb"]; destinationFile = [OFFile fileWithPath: destination mode: @"wb"]; |
︙ | ︙ | |||
492 493 494 495 496 497 498 | #ifndef _WIN32 if (rename([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #else if (!MoveFile([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #endif | | > | | | | | > | | | | | > | | | | | > | | > | | | 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | #ifndef _WIN32 if (rename([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #else if (!MoveFile([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #endif @throw [OFRenameFileFailedException exceptionWithClass: self sourcePath: source destinationPath: destination]; [pool release]; } + (void)deleteFileAtPath: (OFString*)path { #ifndef _WIN32 if (unlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #else if (!DeleteFile([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #endif @throw [OFDeleteFileFailedException exceptionWithClass: self path: path]; } + (void)deleteDirectoryAtPath: (OFString*)path { if (rmdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) @throw [OFDeleteDirectoryFailedException exceptionWithClass: self path: path]; } #ifndef _WIN32 + (void)linkFileAtPath: (OFString*)source toPath: (OFString*)destination { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; } if (link([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0) @throw [OFLinkFailedException exceptionWithClass: self sourcePath: source destinationPath: destination]; [pool release]; } #endif #if !defined(_WIN32) && !defined(_PSP) + (void)symlinkFileAtPath: (OFString*)source toPath: (OFString*)destination { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; } if (symlink([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0) @throw [OFSymlinkFailedException exceptionWithClass: self sourcePath: source destinationPath: destination]; [pool release]; } #endif - init { Class c = isa; [self release]; @throw [OFNotImplementedException exceptionWithClass: c selector: _cmd]; } - initWithPath: (OFString*)path mode: (OFString*)mode { self = [super init]; @try { int flags; if ((flags = parse_mode([mode cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) == -1) @throw [OFInvalidArgumentException exceptionWithClass: isa selector: _cmd]; if ((fileDescriptor = open([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1) @throw [OFOpenFileFailedException exceptionWithClass: isa path: path mode: mode]; closable = YES; } @catch (id e) { [self release]; @throw e; } |
︙ | ︙ | |||
620 621 622 623 624 625 626 | - (size_t)_readNBytes: (size_t)length intoBuffer: (void*)buffer { size_t ret; if (fileDescriptor == -1 || atEndOfStream) | | | | | | | | | | | | | | | | | | | | 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | - (size_t)_readNBytes: (size_t)length intoBuffer: (void*)buffer { size_t ret; if (fileDescriptor == -1 || atEndOfStream) @throw [OFReadFailedException exceptionWithClass: isa stream: self requestedLength: length]; if ((ret = read(fileDescriptor, buffer, length)) == 0) atEndOfStream = YES; return ret; } - (void)_writeNBytes: (size_t)length fromBuffer: (const void*)buffer { if (fileDescriptor == -1 || atEndOfStream || write(fileDescriptor, buffer, length) < length) @throw [OFWriteFailedException exceptionWithClass: isa stream: self requestedLength: length]; } - (void)_seekToOffset: (off_t)offset { if (lseek(fileDescriptor, offset, SEEK_SET) == -1) @throw [OFSeekFailedException exceptionWithClass: isa stream: self offset: offset whence: SEEK_SET]; } - (off_t)_seekForwardWithOffset: (off_t)offset { off_t ret; if ((ret = lseek(fileDescriptor, offset, SEEK_CUR)) == -1) @throw [OFSeekFailedException exceptionWithClass: isa stream: self offset: offset whence: SEEK_CUR]; return ret; } - (off_t)_seekToOffsetRelativeToEnd: (off_t)offset { off_t ret; if ((ret = lseek(fileDescriptor, offset, SEEK_END)) == -1) @throw [OFSeekFailedException exceptionWithClass: isa stream: self offset: offset whence: SEEK_END]; return ret; } - (int)fileDescriptor { return fileDescriptor; |
︙ | ︙ | |||
703 704 705 706 707 708 709 | @implementation OFFileSingleton - initWithPath: (OFString*)path mode: (OFString*)mode { Class c = isa; [self release]; | | | | 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | @implementation OFFileSingleton - initWithPath: (OFString*)path mode: (OFString*)mode { Class c = isa; [self release]; @throw [OFNotImplementedException exceptionWithClass: c selector: _cmd]; } - autorelease { return self; } |
︙ | ︙ | |||
728 729 730 731 732 733 734 | - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } - (void)dealloc { | | | | | | | | | | 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } - (void)dealloc { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; [super dealloc]; /* Get rid of stupid warning */ } - (void)_seekToOffset: (off_t)offset { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (off_t)_seekForwardWithOffset: (off_t)offset { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } - (off_t)_seekToOffsetRelativeToEnd: (off_t)offset { @throw [OFNotImplementedException exceptionWithClass: isa selector: _cmd]; } @end |