Repository navigation
Expand file tree
/
Copy pathVFS_CopyMove.cpp
More file actions
88 lines (77 loc) · 2.82 KB
/
Copy pathVFS_CopyMove.cpp
File metadata and controls
88 lines (77 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <filesystem>
#include <string>
#include "Concurrency/WorkContractGroup.h"
#include "Concurrency/WorkService.h"
#include "EntropyCore.h"
#include "VirtualFileSystem/FileHandle.h"
#include "VirtualFileSystem/VirtualFileSystem.h"
using namespace EntropyEngine::Core;
using namespace EntropyEngine::Core::Concurrency;
using namespace EntropyEngine::Core::IO;
static std::string tempPath(const std::string& name) {
return (std::filesystem::temp_directory_path() / name).string();
}
int main() {
WorkService svc({});
WorkContractGroup group(128, "VFS_CopyMove");
svc.start();
svc.addWorkContractGroup(&group);
VirtualFileSystem vfs(&group);
auto srcHandle = vfs.createFileHandle(tempPath("vfs_copy_src.txt"));
auto dstHandle = vfs.createFileHandle(tempPath("vfs_copy_dst.txt"));
auto dst2Handle = vfs.createFileHandle(tempPath("vfs_move_dst.txt"));
// Seed source file via FileHandle
const std::string payload = std::string(4096, 'A');
WriteOptions wo;
wo.truncate = true;
wo.createIfMissing = true;
auto w = srcHandle.writeAll(payload, wo);
w.wait();
if (w.status() != FileOpStatus::Complete) {
ENTROPY_LOG_ERROR(std::string("Seed write failed: ") + w.errorInfo().message);
svc.stop();
return 1;
}
// Copy using FileHandle read/write (simple demo)
auto r = srcHandle.readAll();
r.wait();
if (r.status() != FileOpStatus::Complete) {
ENTROPY_LOG_ERROR(std::string("Copy read failed: ") + r.errorInfo().message);
svc.stop();
return 1;
}
WriteOptions cwo;
cwo.truncate = true;
cwo.createIfMissing = true;
auto cw = dstHandle.writeAll(r.contentsBytes(), cwo);
cw.wait();
if (cw.status() != FileOpStatus::Complete) {
ENTROPY_LOG_ERROR(std::string("Copy write failed: ") + cw.errorInfo().message);
svc.stop();
return 1;
}
ENTROPY_LOG_INFO(std::string("Copy complete, bytes: ") + std::to_string(cw.bytesWritten()));
// Move: write to destination (overwrite) then remove source
auto r2 = dstHandle.readAll();
r2.wait();
if (r2.status() == FileOpStatus::Complete) {
WriteOptions mwo;
mwo.truncate = true;
mwo.createIfMissing = true;
auto mw = dst2Handle.writeAll(r2.contentsBytes(), mwo);
mw.wait();
if (mw.status() == FileOpStatus::Complete) {
dstHandle.remove().wait();
ENTROPY_LOG_INFO(std::string("Move complete: ") + dst2Handle.metadata().path);
} else {
ENTROPY_LOG_ERROR(std::string("Move write failed: ") + mw.errorInfo().message);
}
} else {
ENTROPY_LOG_ERROR(std::string("Move read failed: ") + r2.errorInfo().message);
}
// Cleanup via handles
srcHandle.remove().wait();
dst2Handle.remove().wait();
svc.stop();
return 0;
}