xref: /haiku/src/apps/installer/CopyEngine.h (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
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();
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 char* name,
46 									const struct stat& statInfo,
47 									int32 level) const;
48 
49 			bool				_ShouldClobberFolder(const char* name,
50 									const struct stat& statInfo,
51 									int32 level) const;
52 
53 			status_t			_RemoveFolder(BEntry& entry);
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 	BlockingQueue<Buffer>		fBufferQueue;
89 
90 			thread_id			fWriterThread;
91 	volatile bool				fQuitting;
92 
93 			off_t				fBytesRead;
94 			off_t				fLastBytesRead;
95 			uint64				fItemsCopied;
96 			uint64				fLastItemsCopied;
97 			bigtime_t			fTimeRead;
98 
99 			off_t				fBytesWritten;
100 			bigtime_t			fTimeWritten;
101 
102 			off_t				fBytesToCopy;
103 			uint64				fItemsToCopy;
104 
105 			const char*			fCurrentTargetFolder;
106 			const char*			fCurrentItem;
107 
108 			ProgressReporter*	fProgressReporter;
109 };
110 
111 
112 #endif // COPY_ENGINE_H
113