sbase/mv.c
FRIGN 58098575e7 Audit cp() in libutil
1) Rename cp_HLPflag -> cp_follow for consistency.
2) Use function-pointers for stat to clear up the code.
3) BUGFIX: TERMINATE THE RESULT BUFFER OF READLINK !!!
   It's something I noticed earlier and it actually lead to some
   pretty insane behaviour on our side using glibc (musl somehow
   magically solves this).
   Basically, symlinks used to contain the data of the file they
   pointed to. I wondered for weeks where this came from and now
   this has finally been solved.
4) BUGFIX: Do not unconditionally unlink target-files. Even GNU
   coreutils do it wrong.
   The basic idea is this:
   If fflag == 0 --> don't touch target files if they exist.
   If fflag == 1 --> unlink all and don't error out when we try
                     to unlink a file which doesn't exist.
5) Use estrlcpy and estrlcat instead of snprintf for path building.
6) Make it clearer what happens in preserve.
2015-03-19 17:57:12 +01:00

63 lines
1.0 KiB
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include "fs.h"
#include "util.h"
static int mv_status = 0;
static int
mv(const char *s1, const char *s2, int depth)
{
struct recursor r = { .fn = rm, .hist = NULL, .depth = 0, .follow = 'P', .flags = 0};
if (!rename(s1, s2))
return (mv_status = 0);
if (errno == EXDEV) {
cp_aflag = cp_rflag = cp_pflag = 1;
cp_follow = 'P';
rm_rflag = 1;
cp(s1, s2, depth);
rm(s1, NULL, NULL, &r);
return (mv_status = cp_status || rm_status);
}
mv_status = 1;
return -1;
}
static void
usage(void)
{
eprintf("usage: %s [-f] source ... dest\n", argv0);
}
int
main(int argc, char *argv[])
{
struct stat st;
ARGBEGIN {
case 'f':
break;
default:
usage();
} ARGEND;
if (argc < 2)
usage();
if (argc > 2) {
if (stat(argv[argc - 1], &st) < 0)
eprintf("stat %s:", argv[argc - 1]);
if (!S_ISDIR(st.st_mode))
eprintf("%s: not a directory\n", argv[argc - 1]);
}
enmasse(argc, argv, mv);
return mv_status;
}