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 15 #include "BlockingQueue.h" 16 17 class BFile; 18 class BMessage; 19 class BMessenger; 20 21 class CopyEngine { 22 public: 23 CopyEngine(const BMessenger& messenger, 24 BMessage* message); 25 virtual ~CopyEngine(); 26 27 void ResetTargets(); 28 status_t CollectTargets(const char* source, 29 sem_id cancelSemaphore = -1); 30 31 status_t CopyFolder(const char* source, 32 const char* destination, 33 sem_id cancelSemaphore = -1); 34 35 status_t CopyFile(const BEntry& entry, 36 const BEntry& destination, 37 sem_id cancelSemaphore = -1); 38 39 private: 40 status_t _CollectCopyInfo(const char* source, 41 int32& level, sem_id cancelSemaphore); 42 status_t _CopyFolder(const char* source, 43 const char* destination, 44 int32& level, sem_id cancelSemaphore); 45 46 bool _ShouldCopyEntry(const char* name, 47 const struct stat& statInfo, 48 int32 level) const; 49 50 bool _ShouldClobberFolder(const char* name, 51 const struct stat& statInfo, 52 int32 level) const; 53 54 status_t _RemoveFolder(BEntry& entry); 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 { 67 Buffer(BFile* file) 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 BlockingQueue<Buffer> fBufferQueue; 89 90 thread_id fWriterThread; 91 volatile bool fQuitting; 92 93 off_t fBytesRead; 94 uint64 fItemsCopied; 95 bigtime_t fTimeRead; 96 97 off_t fBytesWritten; 98 bigtime_t fTimeWritten; 99 100 off_t fBytesToCopy; 101 uint64 fItemsToCopy; 102 103 const char* fCurrentTargetFolder; 104 const char* fCurrentItem; 105 106 BMessenger fMessenger; 107 BMessage* fMessage; 108 }; 109 110 111 #endif // COPY_ENGINE_2_H 112