MCPcopy Create free account
hub / github.com/computationalpathologygroup/ASAP / copyFile

Function copyFile

core/filetools.cpp:128–177  ·  view source on GitHub ↗

---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

126
127//---------------------------------------------------------------------------
128 bool copyFile(const std::string &source, const std::string &target, bool overwrite, bool copyAttributes)
129 {
130 //copy_file throws: if !exists(source_file) || is_directory(source_file)
131 //|| exists(target_file) || target_file.empty() || !exists(to_file_path/"..")).
132 //we check these first and return false if needed
133
134 //Check source
135 if (!fileExists(source)) return false;
136
137 //Check taget
138 //Check taget
139 if (!dirExists(extractFilePath(target)))
140 {
141 if (!createDirectory(extractFilePath(target)))
142 {
143 return false;
144 }
145 }
146 else if (fileExists(target))
147 {
148 if (overwrite)
149 {
150 if (!deleteFile(target))
151 {
152 return false;
153 }
154 }
155 else
156 {
157 return false;
158 }
159 }
160
161 if (!copyAttributes)
162 {
163 //now we can safely copy
164 //create_file( path(target));
165 //this trick is needed to prevent attributes from being copied
166 std::ifstream ifs(source.c_str());
167 std::ofstream ofs(target.c_str());
168 ofs << ifs.rdbuf();
169 ofs.close();
170 ifs.close();
171 }
172 else
173 {
174 fs::copy_file(fs::path(source), fs::path(target));
175 }
176 return fileExists(target);
177 }
178
179
180//---------------------------------------------------------------------------

Callers 1

copyDirectoryFunction · 0.85

Calls 6

fileExistsFunction · 0.85
dirExistsFunction · 0.85
extractFilePathFunction · 0.85
createDirectoryFunction · 0.85
deleteFileFunction · 0.85
closeMethod · 0.45

Tested by

no test coverage detected