1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

Userland: Support moving files between different mounted filesystems

In case we cannot use rename() because of cross-device error, copy file to the
destination then unlink the old source file.
This commit is contained in:
Bui Quang Minh 2021-02-18 21:34:43 +07:00 committed by Andreas Kling
parent 14058b6858
commit ff67340d81
3 changed files with 202 additions and 160 deletions

View file

@ -24,9 +24,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cp.h"
#include <AK/LexicalPath.h>
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
@ -71,6 +73,9 @@ int main(int argc, char** argv)
return 1;
}
auto my_umask = umask(0);
umask(my_umask);
for (auto& old_path : paths) {
String combined_new_path;
const char* new_path = original_new_path;
@ -82,11 +87,19 @@ int main(int argc, char** argv)
rc = rename(old_path, new_path);
if (rc < 0) {
perror("rename");
return 1;
if (errno == EXDEV) {
bool recursion_allowed = true;
bool link = false;
bool ok = copy_file_or_directory(old_path, new_path, recursion_allowed, link);
if (!ok)
return 1;
rc = unlink(old_path);
if (rc < 0)
fprintf(stderr, "mv: unlink '%s': %s\n", old_path, strerror(errno));
}
}
if (verbose)
if (verbose && rc == 0)
printf("renamed '%s' -> '%s'\n", old_path, new_path);
}