xref: /haiku/src/apps/installer/CopyEngine.h (revision ddac407426cd3b3d0b4589d7a161b300b3539a2a)
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 BLocker;
19 class BMessage;
20 class BMessenger;
21 
22 class CopyEngine {
23 public:
24 								CopyEngine(const BMessenger& messenger,
25 									BMessage* message);
26 	virtual						~CopyEngine();
27 
28 			void				ResetTargets();
29 			status_t			CollectTargets(const char* source);
30 
31 			status_t			CopyFolder(const char* source,
32 									const char* destination,
33 									BLocker* locker = NULL);
34 
35 			status_t			CopyFile(const BEntry& entry,
36 									const BEntry& destination,
37 									BLocker* locker = NULL);
38 
39 private:
40 			status_t			_CollectCopyInfo(const char* source,
41 									int32& level);
42 			status_t			_CopyFolder(const char* source,
43 									const char* destination,
44 									int32& level,
45 									BLocker* locker = NULL);
46 
47 			bool				_ShouldCopyEntry(const char* name,
48 									const struct stat& statInfo,
49 									int32 level) const;
50 
51 			bool				_ShouldClobberFolder(const char* name,
52 									const struct stat& statInfo,
53 									int32 level) const;
54 
55 			status_t			_RemoveFolder(BEntry& entry);
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 					: file(file)
70 					, buffer(malloc(BUFFER_SIZE))
71 					, size(BUFFER_SIZE)
72 					, validBytes(0)
73 					, deleteFile(false)
74 				{
75 				}
76 				~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 	BlockingQueue<Buffer>		fBufferQueue;
90 
91 			thread_id			fWriterThread;
92 	volatile bool				fQuitting;
93 
94 			off_t				fBytesRead;
95 			uint64				fItemsCopied;
96 			bigtime_t			fTimeRead;
97 
98 			off_t				fBytesWritten;
99 			bigtime_t			fTimeWritten;
100 
101 			off_t				fBytesToCopy;
102 			uint64				fItemsToCopy;
103 
104 			const char*			fCurrentTargetFolder;
105 			const char*			fCurrentItem;
106 
107 			BMessenger			fMessenger;
108 			BMessage*			fMessage;
109 };
110 
111 
112 #endif // COPY_ENGINE_2_H
113