|
1 | 1 | #include "filesystem.h" |
2 | 2 | #include <iostream> |
| 3 | +#include <map> |
3 | 4 |
|
4 | | -void helpers::copy_directory(const fs::path &src, const fs::path &dest) |
| 5 | +/** |
| 6 | + * Try to find matching hardlink in hardlinks map. If src is found in the map, dest is filled with corresponding file. |
| 7 | + * @param hardlinks the hardlinks map (src -> dst) |
| 8 | + * @param src source path being looked up in hardlinks using equvalent func |
| 9 | + * @param dest output arg which is filled in case of success |
| 10 | + * @return true if the hardlink match is found |
| 11 | + */ |
| 12 | +bool find_matching_hadlink(std::map<fs::path, fs::path> &hardlinks, const fs::path &src, fs::path &dest) |
| 13 | +{ |
| 14 | + for (auto& [s, d] : hardlinks) { |
| 15 | + if (fs::equivalent(s, src)) { // both paths point to the same data on the disk (same hardlinks) |
| 16 | + dest = d; |
| 17 | + return true; |
| 18 | + } |
| 19 | + } |
| 20 | + return false; |
| 21 | +} |
| 22 | + |
| 23 | +void copy_diretory_internal(const fs::path &src, const fs::path &dest, bool skip_symlinks, std::map<fs::path, fs::path> &hardlinks) |
5 | 24 | { |
6 | 25 | try { |
| 26 | + // routine checks |
7 | 27 | if (!fs::exists(src)) { |
8 | | - throw filesystem_exception( |
| 28 | + throw helpers::filesystem_exception( |
9 | 29 | "helpers::copy_directory: Source directory does not exist '" + src.string() + "'"); |
10 | | - } else if (!fs::is_directory(fs::symlink_status(src))) { |
11 | | - throw filesystem_exception( |
| 30 | + } |
| 31 | + |
| 32 | + if (skip_symlinks && fs::is_symlink(src)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (!fs::is_directory(fs::symlink_status(src))) { |
| 37 | + throw helpers::filesystem_exception( |
12 | 38 | "helpers::copy_directory: Source directory is not a directory '" + src.string() + "'"); |
13 | | - } else if (!fs::exists(dest) && !fs::create_directories(dest)) { |
14 | | - throw filesystem_exception( |
| 39 | + } |
| 40 | + |
| 41 | + if (!fs::exists(dest) && !fs::create_directories(dest)) { |
| 42 | + throw helpers::filesystem_exception( |
15 | 43 | "helpers::copy_directory: Destination directory cannot be created '" + dest.string() + "'"); |
16 | 44 | } |
17 | 45 |
|
| 46 | + // proceed with copying |
18 | 47 | fs::directory_iterator endit; |
19 | 48 | for (fs::directory_iterator it(src); it != endit; ++it) { |
| 49 | + auto srcPath = it->path(); |
| 50 | + auto destPath = dest / it->path().filename(); |
| 51 | + |
| 52 | + if (skip_symlinks && fs::is_symlink(srcPath)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
20 | 56 | if (fs::is_directory(it->symlink_status())) { |
21 | | - helpers::copy_directory(it->path(), dest / it->path().filename()); |
| 57 | + copy_diretory_internal(srcPath, destPath, skip_symlinks, hardlinks); |
22 | 58 | } else { |
23 | | - fs::copy(it->path(), dest / it->path().filename()); |
| 59 | + // a file may be either copied or hardlinked |
| 60 | + if (!fs::is_symlink(srcPath) && fs::hard_link_count(srcPath) > 1) { |
| 61 | + fs::path destPathHardlink; |
| 62 | + if (find_matching_hadlink(hardlinks, it->path(), destPathHardlink)) { |
| 63 | + // another file refering to the same data already exists in dest directory |
| 64 | + fs::create_hard_link(destPathHardlink, destPath); |
| 65 | + continue; // move to next file, hardlink replaced copying |
| 66 | + } else { |
| 67 | + // this is the first time we encoutered this data, lets register them in hardlinks map |
| 68 | + hardlinks.emplace(std::make_pair(it->path(), destPath)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // no hardlink created, lets proceed with copying |
| 73 | + fs::copy(it->path(), destPath); |
24 | 74 | } |
25 | 75 | } |
26 | 76 | } catch (fs::filesystem_error &e) { |
27 | | - throw filesystem_exception("helpers::copy_directory: Error in copying directories: " + std::string(e.what())); |
| 77 | + throw helpers::filesystem_exception("helpers::copy_directory: Error in copying directories: " + std::string(e.what())); |
28 | 78 | } |
29 | 79 |
|
30 | 80 | return; |
31 | 81 | } |
32 | 82 |
|
| 83 | +void helpers::copy_directory(const fs::path &src, const fs::path &dest, bool skip_symlinks) |
| 84 | +{ |
| 85 | + /* |
| 86 | + * Hardlinks map provide mapping between files in src and dest which have been harlinked. |
| 87 | + * Everytime a file with > 1 hadlink count is copied from src to dest, it is registered here. |
| 88 | + * When files with > 1 hardlinks are encountered, this map is searched and if match is found |
| 89 | + * the new file is hadlinked inside dest instead of coping it from src. |
| 90 | + */ |
| 91 | + std::map<fs::path, fs::path> hardlinks; |
| 92 | + ::copy_diretory_internal(src, dest, skip_symlinks, hardlinks); |
| 93 | +} |
| 94 | + |
33 | 95 | fs::path helpers::normalize_path(const fs::path &path) |
34 | 96 | { |
35 | 97 | // prepare root and path chunks |
|
0 commit comments