1 /* 2 * Copyright 2008-2009, Stephan Aßmus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #ifndef COPY_ENGINE_H 6 #define COPY_ENGINE_H 7 8 9 #include <stdlib.h> 10 11 #include <Entry.h> 12 #include <File.h> 13 #include <Messenger.h> 14 #include <String.h> 15 16 #include "BlockingQueue.h" 17 18 class BFile; 19 class ProgressReporter; 20 21 22 class CopyEngine { 23 public: 24 class EntryFilter; 25 26 public: 27 CopyEngine(ProgressReporter* reporter, 28 EntryFilter* entryFilter); 29 virtual ~CopyEngine(); 30 31 void ResetTargets(const char* source); 32 status_t CollectTargets(const char* source, 33 sem_id cancelSemaphore = -1); 34 35 status_t Copy(const char* source, 36 const char* destination, 37 sem_id cancelSemaphore = -1, 38 bool copyAttributes = true); 39 40 private: 41 status_t _CollectCopyInfo(const char* source, 42 int32& level, sem_id cancelSemaphore, 43 off_t& bytesToCopy, uint64& itemsToCopy); 44 status_t _Copy(BEntry& source, 45 BEntry& destination, 46 int32& level, sem_id cancelSemaphore, 47 bool copyAttributes); 48 status_t _CopyData(const BEntry& entry, 49 const BEntry& destination, 50 sem_id cancelSemaphore = -1); 51 52 status_t _RemoveFolder(BEntry& entry); 53 54 const char* _RelativeEntryPath( 55 const char* absoluteSourcePath) const; 56 57 void _UpdateProgress(); 58 59 static int32 _WriteThreadEntry(void* cookie); 60 void _WriteThread(); 61 62 private: 63 enum { 64 BUFFER_COUNT = 16, 65 BUFFER_SIZE = 1024 * 1024 66 }; 67 struct Buffer { 68 Buffer(BFile* file) 69 : 70 file(file), 71 buffer(malloc(BUFFER_SIZE)), 72 size(BUFFER_SIZE), 73 validBytes(0), 74 deleteFile(false) 75 { 76 } 77 ~Buffer() 78 { 79 if (deleteFile) 80 delete file; 81 free(buffer); 82 } 83 BFile* file; 84 void* buffer; 85 size_t size; 86 size_t validBytes; 87 bool deleteFile; 88 }; 89 90 private: 91 BlockingQueue<Buffer> fBufferQueue; 92 93 thread_id fWriterThread; 94 volatile bool fQuitting; 95 96 BString fAbsoluteSourcePath; 97 98 off_t fBytesRead; 99 off_t fLastBytesRead; 100 uint64 fItemsCopied; 101 uint64 fLastItemsCopied; 102 bigtime_t fTimeRead; 103 104 off_t fBytesWritten; 105 bigtime_t fTimeWritten; 106 107 const char* fCurrentTargetFolder; 108 const char* fCurrentItem; 109 110 ProgressReporter* fProgressReporter; 111 EntryFilter* fEntryFilter; 112 }; 113 114 115 class CopyEngine::EntryFilter { 116 public: 117 virtual ~EntryFilter(); 118 119 virtual bool ShouldCopyEntry(const BEntry& entry, 120 const char* path, 121 const struct stat& statInfo, 122 int32 level) const = 0; 123 virtual bool ShouldClobberFolder(const BEntry& entry, 124 const char* path, 125 const struct stat& statInfo, 126 int32 level) const = 0; 127 }; 128 129 130 #endif // COPY_ENGINE_H 131