cosmopolitan/tool/build/cp.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

264 lines
6.8 KiB
C
Raw Normal View History

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
2023-06-18 12:39:31 +00:00
#include "libc/fmt/libgen.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/limits.h"
#include "libc/mem/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/ftw.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/ex.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "libc/sysv/consts/s.h"
#include "libc/x/x.h"
#include "third_party/getopt/getopt.internal.h"
#define USAGE \
" SRC... DST\n\
\n\
SYNOPSIS\n\
\n\
Copies Files\n\
\n\
FLAGS\n\
\n\
-?\n\
-h help\n\
-f force\n\
-r recursive\n\
-n no clobber\n\
-a preserve all\n\
-p preserve owner and timestamps\n\
\n"
int flags;
bool force;
int striplen;
bool recursive;
const char *prog;
char mkbuf[PATH_MAX];
char srcdir[PATH_MAX];
char dstdir[PATH_MAX];
char srcfile[PATH_MAX];
char dstfile[PATH_MAX];
char linkbuf[PATH_MAX];
void Cp(char *, char *);
bool IsDirectory(const char *path) {
int e;
bool res;
struct stat st;
e = errno;
res = stat(path, &st) != -1 && S_ISDIR(st.st_mode);
errno = e;
return res;
}
bool IsSymlink(const char *path) {
int e;
bool res;
struct stat st;
e = errno;
res = fstatat(AT_FDCWD, path, &st, AT_SYMLINK_NOFOLLOW) != -1 &&
S_ISLNK(st.st_mode);
errno = e;
return res;
}
2023-07-03 09:47:05 +00:00
wontreturn void PrintUsage(int rc, int fd) {
tinyprint(fd, "USAGE\n\n ", prog, USAGE, NULL);
exit(rc);
}
void GetOpts(int argc, char *argv[]) {
int opt;
2023-07-03 09:47:05 +00:00
while ((opt = getopt(argc, argv, "hfnaprR")) != -1) {
switch (opt) {
case 'f':
force = true;
break;
case 'r':
case 'R':
recursive = true;
break;
case 'n':
Make improvements - We now serialize the file descriptor table when spawning / executing processes on Windows. This means you can now inherit more stuff than just standard i/o. It's needed by bash, which duplicates the console to file descriptor #255. We also now do a better job serializing the environment variables, so you're less likely to encounter E2BIG when using your bash shell. We also no longer coerce environ to uppercase - execve() on Windows now remotely controls its parent process to make them spawn a replacement for itself. Then it'll be able to terminate immediately once the spawn succeeds, without having to linger around for the lifetime as a shell process for proxying the exit code. When process worker thread running in the parent sees the child die, it's given a handle to the new child, to replace it in the process table. - execve() and posix_spawn() on Windows will now provide CreateProcess an explicit handle list. This allows us to remove handle locks which enables better fork/spawn concurrency, with seriously correct thread safety. Other codebases like Go use the same technique. On the other hand fork() still favors the conventional WIN32 inheritence approach which can be a little bit messy, but is *controlled* by guaranteeing perfectly clean slates at both the spawning and execution boundaries - sigset_t is now 64 bits. Having it be 128 bits was a mistake because there's no reason to use that and it's only supported by FreeBSD. By using the system word size, signal mask manipulation on Windows goes very fast. Furthermore @asyncsignalsafe funcs have been rewritten on Windows to take advantage of signal masking, now that it's much more pleasant to use. - All the overlapped i/o code on Windows has been rewritten for pretty good signal and cancelation safety. We're now able to ensure overlap data structures are cleaned up so long as you don't longjmp() out of out of a signal handler that interrupted an i/o operation. Latencies are also improved thanks to the removal of lots of "busy wait" code. Waits should be optimal for everything except poll(), which shall be the last and final demon we slay in the win32 i/o horror show. - getrusage() on Windows is now able to report RUSAGE_CHILDREN as well as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
// TODO(jart): restore functionality
// flags |= COPYFILE_NOCLOBBER;
break;
case 'a':
case 'p':
Make improvements - We now serialize the file descriptor table when spawning / executing processes on Windows. This means you can now inherit more stuff than just standard i/o. It's needed by bash, which duplicates the console to file descriptor #255. We also now do a better job serializing the environment variables, so you're less likely to encounter E2BIG when using your bash shell. We also no longer coerce environ to uppercase - execve() on Windows now remotely controls its parent process to make them spawn a replacement for itself. Then it'll be able to terminate immediately once the spawn succeeds, without having to linger around for the lifetime as a shell process for proxying the exit code. When process worker thread running in the parent sees the child die, it's given a handle to the new child, to replace it in the process table. - execve() and posix_spawn() on Windows will now provide CreateProcess an explicit handle list. This allows us to remove handle locks which enables better fork/spawn concurrency, with seriously correct thread safety. Other codebases like Go use the same technique. On the other hand fork() still favors the conventional WIN32 inheritence approach which can be a little bit messy, but is *controlled* by guaranteeing perfectly clean slates at both the spawning and execution boundaries - sigset_t is now 64 bits. Having it be 128 bits was a mistake because there's no reason to use that and it's only supported by FreeBSD. By using the system word size, signal mask manipulation on Windows goes very fast. Furthermore @asyncsignalsafe funcs have been rewritten on Windows to take advantage of signal masking, now that it's much more pleasant to use. - All the overlapped i/o code on Windows has been rewritten for pretty good signal and cancelation safety. We're now able to ensure overlap data structures are cleaned up so long as you don't longjmp() out of out of a signal handler that interrupted an i/o operation. Latencies are also improved thanks to the removal of lots of "busy wait" code. Waits should be optimal for everything except poll(), which shall be the last and final demon we slay in the win32 i/o horror show. - getrusage() on Windows is now able to report RUSAGE_CHILDREN as well as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
// TODO(jart): restore functionality
// flags |= COPYFILE_PRESERVE_OWNER;
// flags |= COPYFILE_PRESERVE_TIMESTAMPS;
break;
case 'h':
2023-07-03 09:47:05 +00:00
PrintUsage(0, 1);
default:
2023-07-03 09:47:05 +00:00
PrintUsage(1, 2);
}
}
}
int Visit(const char *fpath, const struct stat *sb, int tflag,
struct FTW *ftwbuf) {
char *src;
strcpy(srcfile, fpath);
src = srcfile + striplen;
strcpy(dstfile, dstdir);
if (!endswith(dstfile, "/")) {
strcat(dstfile, "/");
}
strcat(dstfile, src);
strcpy(srcfile, fpath);
switch (tflag) {
case FTW_D:
return 0;
case FTW_F:
case FTW_SL:
case FTW_SLN:
Cp(srcfile, dstfile);
return 0;
default:
2023-07-03 09:47:05 +00:00
tinyprint(2, fpath, ": bad file type\n", NULL);
exit(1);
}
}
char *Join(const char *a, const char *b) {
size_t n, m;
n = strlen(a);
m = strlen(b);
if (n + 1 + m + 1 > sizeof(dstfile)) {
2023-07-03 09:47:05 +00:00
tinyprint(2, prog, ": path too long\n", NULL);
exit(1);
}
stpcpy(stpcpy(stpcpy(dstfile, a), "/"), b);
return dstfile;
}
bool MovePreservingDestinationInode(const char *from, const char *to) {
bool res;
struct stat st;
int fdin, fdout;
if ((fdin = open(from, O_RDONLY)) == -1) {
return false;
}
fstat(fdin, &st);
if ((fdout = creat(to, st.st_mode)) == -1) {
close(fdin);
return false;
}
res = copyfd(fdin, fdout, -1) != -1;
close(fdin);
close(fdout);
return res;
}
void Cp(char *src, char *dst) {
ssize_t rc;
const char *s;
if (strlen(src) + 1 > PATH_MAX)
_Exit(2);
if (strlen(dst) + 1 > PATH_MAX)
_Exit(2);
basename(src);
basename(dst);
if (IsDirectory(src)) {
if (!recursive) {
2023-07-03 09:47:05 +00:00
tinyprint(2, prog, ": won't copy directory without -r flag\n", NULL);
exit(1);
}
strcpy(dstdir, dst);
if (IsDirectory(dst)) {
strcpy(srcdir, src);
basename(srcdir);
striplen = 0;
strcpy(srcdir, basename(src));
} else {
strcpy(srcdir, src);
basename(srcdir);
striplen = strlen(srcdir);
strcpy(srcdir, "");
}
if (nftw(src, Visit, 20, 0) == -1) {
2023-07-03 09:47:05 +00:00
perror(src);
exit(1);
}
return;
}
if (IsDirectory(dst)) {
dst = Join(dst, basename(src));
}
2023-07-03 09:47:05 +00:00
if (!force && access(dst, W_OK) == -1 && errno != ENOENT) {
perror(dst);
exit(1);
}
strcpy(mkbuf, dst);
2023-07-03 09:47:05 +00:00
if (makedirs((s = dirname(mkbuf)), 0755) == -1) {
perror(s);
exit(1);
}
if (IsSymlink(src)) {
2023-07-03 09:47:05 +00:00
if ((rc = readlink(src, linkbuf, sizeof(linkbuf) - 1)) == -1) {
perror(src);
exit(1);
}
linkbuf[rc] = 0;
2023-07-03 09:47:05 +00:00
if (symlink(linkbuf, dst) == -1) {
perror(dst);
exit(1);
}
} else {
2023-07-03 09:47:05 +00:00
if (!MovePreservingDestinationInode(src, dst)) {
perror(src);
exit(1);
}
}
}
int main(int argc, char *argv[]) {
int i;
2023-07-03 09:47:05 +00:00
prog = argv[0];
if (!prog)
prog = "cp";
GetOpts(argc, argv);
2023-07-03 09:47:05 +00:00
if (argc - optind < 2) {
tinyprint(2, prog, ": missing operand\n", NULL);
exit(1);
}
for (i = optind; i < argc - 1; ++i) {
Cp(argv[i], argv[argc - 1]);
}
2023-07-03 09:47:05 +00:00
return 0;
}