PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenEmu/OEFileManager.m

http://github.com/OpenEmu/OpenEmu
Objective C | 124 lines | 84 code | 14 blank | 26 comment | 19 complexity | 97a78250a04b5bdbbd7130d959fa205e MD5 | raw file
Possible License(s): Unlicense, LGPL-2.1
  1. /*
  2. Copyright (c) 2014, OpenEmu Team
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. * Neither the name of the OpenEmu Team nor the
  11. names of its contributors may be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY OpenEmu Team ''AS IS'' AND ANY
  14. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL OpenEmu Team BE LIABLE FOR ANY
  17. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #import "OEFileManager.h"
  25. #include <copyfile.h>
  26. @interface OEFileManager ()
  27. @end
  28. @implementation OEFileManager
  29. int copyfile_callback(int what, int stage, copyfile_state_t state, const char * src, const char * dst, void * ctx_ptr);
  30. - (NSUInteger)sizeOfItemAtURL:(NSURL*)url
  31. {
  32. NSUInteger fileSize = 0;
  33. id value = nil;
  34. NSError *error = nil;
  35. if([url getResourceValue:&value forKey:NSURLIsDirectoryKey error:&error])
  36. {
  37. if([value boolValue])
  38. {
  39. NSArray *keys = @[NSURLIsDirectoryKey, NSURLFileSizeKey];
  40. NSDirectoryEnumerator *directoryEnumerator = [self enumeratorAtURL:url includingPropertiesForKeys:keys options:0 errorHandler:nil];
  41. while((url = [directoryEnumerator nextObject]))
  42. {
  43. if([url getResourceValue:&value forKey:NSURLIsDirectoryKey error:nil] && ![value boolValue] && [url getResourceValue:&value forKey:NSURLFileSizeKey error:nil])
  44. fileSize += [value unsignedIntegerValue];
  45. }
  46. }
  47. else
  48. {
  49. if([url getResourceValue:&value forKey:NSURLFileSizeKey error:&error])
  50. {
  51. fileSize = [value unsignedIntegerValue];
  52. }
  53. }
  54. }
  55. return fileSize;
  56. }
  57. int copyfile_callback(int what, int stage, copyfile_state_t state, const char * src, const char * dst, void * ctx_ptr)
  58. {
  59. return ((__bridge int (^)(copyfile_state_t, int, int, const char *, const char *))ctx_ptr)(state, what, stage, src, dst);
  60. }
  61. - (BOOL)copyItemAtURL:(NSURL*)source toURL:(NSURL *)destination error:(NSError *__autoreleasing *)error
  62. {
  63. const char *src = source.path.fileSystemRepresentation;
  64. const char *dst = destination.path.fileSystemRepresentation;
  65. __block off_t total = [self sizeOfItemAtURL:source];
  66. __block off_t done = 0;
  67. int (^callback)(copyfile_state_t, int, int, const char *, const char *) = ^ int (copyfile_state_t state, int what, int stage, const char *csrc, const char *cdst){
  68. off_t current;
  69. copyfile_state_get(state, COPYFILE_STATE_COPIED, &current);
  70. off_t tmp_done = done + current;
  71. double progress = (double)tmp_done/total;
  72. switch (stage) {
  73. case COPYFILE_PROGRESS:
  74. if(self->_progressHandler)
  75. return self->_progressHandler(progress) ? COPYFILE_CONTINUE : COPYFILE_QUIT;
  76. break ;
  77. case COPYFILE_FINISH:
  78. done += current;
  79. if((what == COPYFILE_RECURSE_DIR_CLEANUP || what == COPYFILE_RECURSE_FILE) && self->_itemDoneHandler!=nil)
  80. {
  81. NSURL *srcURL = [NSURL fileURLWithFileSystemRepresentation:csrc isDirectory:what!=COPYFILE_RECURSE_FILE relativeToURL:nil];
  82. NSURL *dstURL = [NSURL fileURLWithFileSystemRepresentation:cdst isDirectory:what!=COPYFILE_RECURSE_FILE relativeToURL:nil];
  83. return self->_itemDoneHandler(srcURL, dstURL, nil) ? COPYFILE_CONTINUE : COPYFILE_QUIT;
  84. }
  85. break;
  86. case COPYFILE_ERR:
  87. if(self->_errorHandler)
  88. {
  89. // TODO: figure out what error occured
  90. NSURL *srcURL = [NSURL fileURLWithFileSystemRepresentation:csrc isDirectory:stage!=COPYFILE_RECURSE_FILE relativeToURL:nil];
  91. NSURL *dstURL = [NSURL fileURLWithFileSystemRepresentation:cdst isDirectory:stage!=COPYFILE_RECURSE_FILE relativeToURL:nil];
  92. NSError *error = [NSError errorWithDomain:@"OEFileManagerDomain" code:errno userInfo:nil];
  93. return self->_errorHandler(srcURL, dstURL, error) ? COPYFILE_CONTINUE : COPYFILE_QUIT;
  94. }
  95. return COPYFILE_QUIT;
  96. }
  97. return COPYFILE_CONTINUE;
  98. };
  99. copyfile_state_t state = copyfile_state_alloc();
  100. copyfile_state_set(state, COPYFILE_STATE_STATUS_CB, copyfile_callback);
  101. copyfile_state_set(state, COPYFILE_STATE_STATUS_CTX, (__bridge const void *)(callback));
  102. int res = copyfile(src, dst, state, COPYFILE_ALL|COPYFILE_RECURSIVE|COPYFILE_EXCL);
  103. copyfile_state_free(state);
  104. if(res != NO && error != NULL)
  105. *error = [NSError errorWithDomain:@"OEFileManagerDomain" code:errno userInfo:nil];
  106. return res == 0;
  107. }
  108. @end