vfs: check userland buffers before reading them.
[haiku.git] / src / apps / installer / CopyEngine.h
blob298977225bb9ef440e647800975406ac96d1fb5c
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
9 #include <stdlib.h>
11 #include <Entry.h>
12 #include <File.h>
13 #include <Messenger.h>
14 #include <String.h>
16 #include "BlockingQueue.h"
18 class BFile;
19 class ProgressReporter;
22 class CopyEngine {
23 public:
24 class EntryFilter;
26 public:
27 CopyEngine(ProgressReporter* reporter,
28 EntryFilter* entryFilter);
29 virtual ~CopyEngine();
31 void ResetTargets(const char* source);
32 status_t CollectTargets(const char* source,
33 sem_id cancelSemaphore = -1);
35 status_t CopyFolder(const char* source,
36 const char* destination,
37 sem_id cancelSemaphore = -1);
39 status_t CopyFile(const BEntry& entry,
40 const BEntry& destination,
41 sem_id cancelSemaphore = -1);
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);
50 status_t _RemoveFolder(BEntry& entry);
52 const char* _RelativeEntryPath(
53 const char* absoluteSourcePath) const;
55 void _UpdateProgress();
57 static int32 _WriteThreadEntry(void* cookie);
58 void _WriteThread();
60 private:
61 enum {
62 BUFFER_COUNT = 16,
63 BUFFER_SIZE = 1024 * 1024
65 struct Buffer {
66 Buffer(BFile* file)
68 file(file),
69 buffer(malloc(BUFFER_SIZE)),
70 size(BUFFER_SIZE),
71 validBytes(0),
72 deleteFile(false)
75 ~Buffer()
77 if (deleteFile)
78 delete file;
79 free(buffer);
81 BFile* file;
82 void* buffer;
83 size_t size;
84 size_t validBytes;
85 bool deleteFile;
88 private:
89 BlockingQueue<Buffer> fBufferQueue;
91 thread_id fWriterThread;
92 volatile bool fQuitting;
94 BString fAbsoluteSourcePath;
96 off_t fBytesRead;
97 off_t fLastBytesRead;
98 uint64 fItemsCopied;
99 uint64 fLastItemsCopied;
100 bigtime_t fTimeRead;
102 off_t fBytesWritten;
103 bigtime_t fTimeWritten;
105 off_t fBytesToCopy;
106 uint64 fItemsToCopy;
108 const char* fCurrentTargetFolder;
109 const char* fCurrentItem;
111 ProgressReporter* fProgressReporter;
112 EntryFilter* fEntryFilter;
116 class CopyEngine::EntryFilter {
117 public:
118 virtual ~EntryFilter();
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;
131 #endif // COPY_ENGINE_H