xref: /haiku/src/apps/installer/CopyEngine.h (revision 323b65468e5836bb27a5e373b14027d902349437)
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 ProgressReporter;
19 
20 
21 class CopyEngine {
22 public:
23 								CopyEngine(ProgressReporter* reporter);
24 	virtual						~CopyEngine();
25 
26 			void				ResetTargets(const char* source);
27 			status_t			CollectTargets(const char* source,
28 									sem_id cancelSemaphore = -1);
29 
30 			status_t			CopyFolder(const char* source,
31 									const char* destination,
32 									sem_id cancelSemaphore = -1);
33 
34 			status_t			CopyFile(const BEntry& entry,
35 									const BEntry& destination,
36 									sem_id cancelSemaphore = -1);
37 
38 private:
39 			status_t			_CollectCopyInfo(const char* source,
40 									int32& level, sem_id cancelSemaphore);
41 			status_t			_CopyFolder(const char* source,
42 									const char* destination,
43 									int32& level, sem_id cancelSemaphore);
44 
45 			bool				_ShouldCopyEntry(const BEntry& entry,
46 									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 					:
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 			off_t				fLastBytesRead;
96 			uint64				fItemsCopied;
97 			uint64				fLastItemsCopied;
98 			bigtime_t			fTimeRead;
99 
100 			off_t				fBytesWritten;
101 			bigtime_t			fTimeWritten;
102 
103 			off_t				fBytesToCopy;
104 			uint64				fItemsToCopy;
105 
106 			const char*			fCurrentTargetFolder;
107 			const char*			fCurrentItem;
108 
109 			ProgressReporter*	fProgressReporter;
110 
111 	// TODO: Should be made into a list of BEntris to be ignored, perhaps.
112 	// settable by method...
113 			BEntry				fSwapFileEntry;
114 };
115 
116 
117 #endif // COPY_ENGINE_H
118