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 static status_t RemoveFolder(BEntry& entry); 41 42 private: 43 status_t _CollectCopyInfo(const char* source, 44 sem_id cancelSemaphore, off_t& bytesToCopy, 45 uint64& itemsToCopy); 46 status_t _Copy(BEntry& source, BEntry& destination, 47 sem_id cancelSemaphore, 48 bool copyAttributes); 49 status_t _CopyData(const BEntry& entry, 50 const BEntry& destination, 51 sem_id cancelSemaphore = -1); 52 53 const char* _RelativeEntryPath( 54 const char* absoluteSourcePath) const; 55 56 void _UpdateProgress(); 57 58 static int32 _WriteThreadEntry(void* cookie); 59 void _WriteThread(); 60 61 private: 62 enum { 63 BUFFER_COUNT = 16, 64 BUFFER_SIZE = 1024 * 1024 65 }; 66 struct Buffer { BufferBuffer67 Buffer(BFile* file) 68 : 69 file(file), 70 buffer(malloc(BUFFER_SIZE)), 71 size(BUFFER_SIZE), 72 validBytes(0), 73 deleteFile(false) 74 { 75 } ~BufferBuffer76 ~Buffer() 77 { 78 if (deleteFile) 79 delete file; 80 free(buffer); 81 } 82 BFile* file; 83 void* buffer; 84 size_t size; 85 size_t validBytes; 86 bool deleteFile; 87 }; 88 89 private: 90 BlockingQueue<Buffer> fBufferQueue; 91 92 thread_id fWriterThread; 93 volatile bool fQuitting; 94 95 BString fAbsoluteSourcePath; 96 97 off_t fBytesRead; 98 off_t fLastBytesRead; 99 uint64 fItemsCopied; 100 uint64 fLastItemsCopied; 101 bigtime_t fTimeRead; 102 103 off_t fBytesWritten; 104 bigtime_t fTimeWritten; 105 106 const char* fCurrentTargetFolder; 107 const char* fCurrentItem; 108 109 ProgressReporter* fProgressReporter; 110 EntryFilter* fEntryFilter; 111 }; 112 113 114 class CopyEngine::EntryFilter { 115 public: 116 virtual ~EntryFilter(); 117 118 virtual bool ShouldCopyEntry(const BEntry& entry, 119 const char* path, 120 const struct stat& statInfo) const = 0; 121 }; 122 123 124 #endif // COPY_ENGINE_H 125