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