Differences From Artifact [95c6f11fb5]:
- File
src/OFFile.m
— part of check-in
[260391fc1c]
at
2013-12-11 23:53:07
on branch trunk
— Add +[OFFile moveItemAtPath:toPath:].
This replaces +[OFFile renameItemAtPath:toPath:] and supports moving
files to different logical devices. (user: js, size: 21737) [annotate] [blame] [check-ins using]
To Artifact [17aa806995]:
- File src/OFFile.m — part of check-in [ce99d76d79] at 2013-12-15 14:57:39 on branch trunk — OFFile: Clean up the stat mess. (user: js, size: 20953) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
26 27 28 29 30 31 32 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <unistd.h> | < < | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <unistd.h> #include <fcntl.h> #include <dirent.h> #ifdef HAVE_PWD_H # include <pwd.h> #endif #ifdef HAVE_GRP_H |
︙ | ︙ | |||
103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH #define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH #if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS) static of_mutex_t mutex; #endif static int parseMode(const char *mode) { if (!strcmp(mode, "r")) return O_RDONLY; if (!strcmp(mode, "rb")) | > > > > > > > > > > > > > > > > > > > > > > > > > | 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #define DEFAULT_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH #define DIR_MODE DEFAULT_MODE | S_IXUSR | S_IXGRP | S_IXOTH #if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS) static of_mutex_t mutex; #endif int of_stat(OFString *path, of_stat_t *buffer) { #ifdef _WIN32 return _wstat([path UTF16String], buffer); #else return stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], buffer); #endif } int of_lstat(OFString *path, of_stat_t *buffer) { #if defined(_WIN32) return _wstat([path UTF16String], buffer); #elif defined(HAVE_LSTAT) return lstat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], buffer); #else return stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], buffer); #endif } static int parseMode(const char *mode) { if (!strcmp(mode, "r")) return O_RDONLY; if (!strcmp(mode, "rb")) |
︙ | ︙ | |||
192 193 194 195 196 197 198 199 200 201 | } return ret; } + (bool)fileExistsAtPath: (OFString*)path { if (path == nil) @throw [OFInvalidArgumentException exception]; | > > < < < < | < < < < < < > > < < < < | < < < < < < | < | | 215 216 217 218 219 220 221 222 223 224 225 226 227 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 260 261 262 263 264 265 266 267 | } return ret; } + (bool)fileExistsAtPath: (OFString*)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) return false; if (S_ISREG(s.st_mode)) return true; return false; } + (bool)directoryExistsAtPath: (OFString*)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) return false; if (S_ISDIR(s.st_mode)) return true; return false; } #ifdef OF_HAVE_SYMLINK + (bool)symbolicLinkExistsAtPath: (OFString*)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_lstat(path, &s) == -1) return false; if (S_ISLNK(s.st_mode)) return true; return false; } |
︙ | ︙ | |||
406 407 408 409 410 411 412 413 414 415 | #endif @throw [OFChangeCurrentDirectoryPathFailedException exceptionWithPath: path]; } + (off_t)sizeOfFileAtPath: (OFString*)path { if (path == nil) @throw [OFInvalidArgumentException exception]; | > > < < < < | < < < < < | > > < < < < | < < < < < | 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 439 440 441 442 443 444 445 446 447 | #endif @throw [OFChangeCurrentDirectoryPathFailedException exceptionWithPath: path]; } + (off_t)sizeOfFileAtPath: (OFString*)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) /* FIXME: Maybe use another exception? */ @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"r"]; /* FIXME: On Android, off_t is 32 bit, but st_size is long long there */ return (off_t)s.st_size; } + (OFDate*)modificationDateOfFileAtPath: (OFString*)path { of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; if (of_stat(path, &s) == -1) /* FIXME: Maybe use another exception? */ @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"r"]; /* FIXME: We could be more precise on some OSes */ return [OFDate dateWithTimeIntervalSince1970: s.st_mtime]; } |
︙ | ︙ | |||
530 531 532 533 534 535 536 | } #endif + (void)copyItemAtPath: (OFString*)source toPath: (OFString*)destination { void *pool; | < < < | < < | < < < < < < < | < | 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 | } #endif + (void)copyItemAtPath: (OFString*)source toPath: (OFString*)destination { void *pool; of_stat_t s; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); if (of_lstat(destination, &s) == 0) { errno = EEXIST; @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination]; } if (of_lstat(source, &s)) @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination]; if (S_ISDIR(s.st_mode)) { OFArray *contents; OFEnumerator *enumerator; |
︙ | ︙ | |||
665 666 667 668 669 670 671 | objc_autoreleasePoolPop(pool); } + (void)moveItemAtPath: (OFString*)source toPath: (OFString*)destination { void *pool; | < < < | < < | < < < < | 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | objc_autoreleasePoolPop(pool); } + (void)moveItemAtPath: (OFString*)source toPath: (OFString*)destination { void *pool; of_stat_t s; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); if (of_lstat(destination, &s) == 0) { errno = EEXIST; @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination]; } #ifndef _WIN32 |
︙ | ︙ | |||
724 725 726 727 728 729 730 | objc_autoreleasePoolPop(pool); } + (void)removeItemAtPath: (OFString*)path { void *pool; | < < < | < < < < | < | 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | objc_autoreleasePoolPop(pool); } + (void)removeItemAtPath: (OFString*)path { void *pool; of_stat_t s; if (path == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); if (of_lstat(path, &s)) @throw [OFRemoveItemFailedException exceptionWithPath: path]; if (S_ISDIR(s.st_mode)) { OFArray *contents; OFEnumerator *enumerator; OFString *item; |
︙ | ︙ |