Compare commits

...
14 Commits
Author SHA1 Message Date
Joachim Wiberg b8e9b843f0 Update ChangeLog with which() and copyfile() changes
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 14:22:41 +01:00
Joachim Wiberg 7f9f49626a Report read and write errors from copyfile()
copyfile() returns a byte count, with errno cleared, when the copy
fails after the first block.  movefile() across file systems then
removes the source and leaves a truncated copy.

The copy loop adds the result of write() to the total without checking
it, takes a failed read() for end of file, and the result of close() on
the destination is dropped.

Return -1 with errno set on any of them, and retry short writes.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 14:22:40 +01:00
omrzk dc90e93812 Document that which() skips empty PATH components
An empty component in PATH, from a leading, trailing, or doubled colon,
means the current directory to execvp() and the shell.  which() has
always skipped them, but nothing said so, or kept it that way.

Say so in the API description and add tests for it.  A caller that
wants the current directory searched must list it, e.g., as ".".

Closes #27
2026-11-16 14:22:40 +01:00
Joachim Wiberg fac54a5001 Update ChangeLog with fsendfile(), writesf(), and mkpath() fixes
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 14:13:08 +01:00
omrzk eb883301b3 Do not let mkpath() accept an existing non-directory
mkpath(), fmkpath(), and makepath() return success when the path exists
as a regular file, or anything else that is not a directory:

    touch /tmp/foo; mkpath("/tmp/foo", 0755);

Any path that stat() can find is taken to be done.

Check that it is a directory, otherwise fail with ENOTDIR.

Closes #26
2026-11-16 14:13:07 +01:00
omrzk 0013f4fcf4 Report write errors from writesf(), writellf(), and writedf()
The write functions return success when nothing was written, e.g., on
a stream that is not open for writing:

    writesf("data", "r", "/tmp/file");

They return the result of fclose() and drop the one from fprintf().

Return -1 when fprintf() fails, with errno from the failed write.

Closes #25
2026-11-16 14:13:07 +01:00
omrzk 8befd09cf5 Report read errors from fsendfile()
fsendfile() returns the number of bytes copied when reading the source
fails, as if it had reached the end of the file.  It stops when fread()
returns nothing, without checking why.

Check ferror() on the source and return -1, with errno from the failed
read.

Closes #24
2026-11-16 14:13:06 +01:00
Joachim Wiberg 798d70e2a2 Merge branch 'copyfile-symlinks'
Closes #23

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-14 12:49:17 +01:00
Joachim Wiberg 2f6a7f1595 Update ChangeLog with fcopyfile() fix
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-14 12:47:14 +01:00
Joachim Wiberg ef8b17f438 Do not let rsync() write through symlinks in the destination
With LITE_FOPT_RSYNC_LINKS, rsync() leaves symlinks in the destination.
If the source later has a regular file or a directory by the same name,
the next run follows the old link and writes to whatever it points at,
which can be outside the destination tree.

Remove such a link before copying.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-14 12:47:14 +01:00
Joachim Wiberg 4609c53ddd Make rsync() recreate symlinks only on request
Fixing LITE_FOPT_COPYFILE_SYM in copyfile() also changed rsync(), which
has always passed that flag.  Every caller would start getting links in
the destination where they used to get a copy of the file.

Keep following links by default and add LITE_FOPT_RSYNC_LINKS for
callers that want them recreated.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-14 12:46:48 +01:00
omrzk 666a8ef7cd Honor the symbolic link option when copying files
Inspect source links without following them when COPYFILE_SYM is requested, before rejecting directories. This makes the documented option recreate link text instead of copying the referent, including dangling and directory links. Preserve requested link timestamps and report link-creation errors without overwriting the destination. Cover direct calls, destination directories, normal dereferencing and the existing rsync file caller.
2026-11-14 12:41:18 +01:00
Joachim Wiberg dff88a622d Merge pull request #22 from omrzk/fix/copy-binary-file-streams
Preserve binary data when copying file streams
2026-11-14 12:38:36 +01:00
omrzk 2dfc02e67d Preserve binary data when copying file streams
Copy the bytes returned by fread instead of treating each input chunk as a NUL-terminated string. Preserve the public stream positions and report read and write failures rather than returning success after a partial copy. Add binary boundary, current-position, native pipe error and full-device regression coverage.
2026-11-13 09:12:43 -06:00
18 changed files with 673 additions and 56 deletions

No files matched your search

+20
View File
@@ -7,12 +7,32 @@ All notable changes to the project are documented in this file.
[UNRELEASED][]
--------------
### Changes
- Add `LITE_FOPT_RSYNC_LINKS` option to `rsync()`, recreates symlinks to
files in the destination instead of copying what they point to
- Document that `which()` and `whichp()` skip empty components in
`PATH`, the current directory is only searched when listed, e.g., `.`
### Fixes
- Fix #23: `copyfile()` ignored `LITE_FOPT_COPYFILE_SYM` and always
copied what a symlink points to. It now recreates the symlink, also
when it is dangling or points to a directory
- Fix #22: `fcopyfile()` truncated binary data at the first NUL byte in
each chunk, and returned success on read and write errors
- Fix `copyfile()` not preserving the source file mode when the process
umask is more restrictive
- Fix `mkpath()`, and `fmkpath()`, not applying the given mode when the
process umask is more restrictive, e.g., requesting 0755 with umask
0027 silently created a 0750 directory
- Fix `mkpath()`, `fmkpath()`, and `makepath()` returning success when
the path exists but is not a directory, they now fail with `ENOTDIR`
- Fix `fsendfile()` returning the number of bytes copied, instead of
-1, when reading from the source stream fails
- Fix `writesf()`, `writellf()`, and `writedf()` returning success when
the write fails, e.g., on a file opened read-only
- Fix `copyfile()`, and `movefile()` across file systems, reporting
success when reading or writing fails after the first block. A
failed `movefile()` could leave a truncated copy and remove the source
[v2.6.2][] - 2025-11-22
-----------------------
+10
View File
@@ -244,6 +244,16 @@ Generic Functions
- `LITE_FOPT_RSYNC_DELETE (0x01)`
- `LITE_FOPT_KEEP_MTIME (0x02)`
- `LITE_FOPT_RSYNC_LINKS (0x04)`
Symlinks in the source tree are followed by default, the destination
gets a copy of what they point to. With `LITE_FOPT_RSYNC_LINKS` a
symlink to a file, or a dangling symlink, is recreated in the
destination instead. Symlinks to directories are always followed.
With `LITE_FOPT_RSYNC_LINKS` a symlink in the destination is never
followed. If one sits where a file or directory from the source
goes, it is removed first.
In releases prior to v2.0 the argument controlling pruning was called
`delete`, it is now called `opt`. The APIs are 100% compatible if the
+57 -29
View File
@@ -71,33 +71,38 @@ static int adjust_target(const char *src, char **dst)
return isdir;
}
/*
* Actual copy loop, used by both copyfile() and fcopyfile() breaks loop
* on error and EOF
*/
/* Copy up to num bytes, stops at EOF. Returns bytes copied, or -1 */
static ssize_t do_copy(int in, int out, size_t num, char *buffer, size_t len)
{
ssize_t ret = 0, size = -1;
ssize_t size = 0;
do {
while (num > 0) {
size_t count = num > len ? len : num;
ssize_t ret, off = 0;
ret = read(in, buffer, count);
if (ret <= 0) {
if (ret == -1 && EINTR == errno)
if (ret == -1) {
if (errno == EINTR)
continue;
if (ret == 0 && size == -1)
size = 0;
return -1;
}
if (ret == 0)
break;
while (off < ret) {
ssize_t n = write(out, buffer + off, ret - off);
if (n == -1) {
if (errno == EINTR)
continue;
return -1;
}
off += n;
}
if (ret > 0) {
if (size == -1)
size = 0;
size += write(out, buffer, ret);
size += ret;
num -= ret;
}
num -= count;
} while (num > 0);
return size;
}
@@ -137,7 +142,8 @@ static void set_mtime(int in, int out)
*
* @returns The number of bytes copied, or zero, which may be an error
* (check @a errno, see Exceptions below), but it may also indicate that
* @a src was empty. See exceptions, below.
* @a src was empty. If reading or writing fails -1 is returned, with
* @a errno set. See exceptions, below.
*
* @exception EISDIR if @a src is a directory, since copyfile() is not recursive.
*/
@@ -157,7 +163,13 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
if (!buffer)
return 0;
if (fisdir(src)) {
if (sym ? lstat(src, &st) : stat(src, &st)) {
saved_errno = errno;
size = -1;
goto exit;
}
if (S_ISDIR(st.st_mode)) {
saved_errno = EISDIR; /* Error: source is a directory */
goto exit;
}
@@ -165,13 +177,7 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
/* Check if target is a directory, then append src filename. */
isdir = adjust_target(src, &dest);
/* Check if the source file is a symlink ... */
if (stat(src, &st)) {
size = -1;
goto exit;
}
/* ... only try readlink() if symlink and @sym is set. */
/* Only try readlink() if symlink and @sym is set. */
if (sym && (st.st_mode & S_IFMT) == S_IFLNK) {
size = readlink(src, buffer, BUFSIZ);
if (size > 0) {
@@ -181,8 +187,16 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
} else {
buffer[size] = 0;
size = !symlink(buffer, dest);
if (!size)
saved_errno = errno;
else if (keep_mtim) {
struct timespec tv[2] = { st.st_atim, st.st_mtim };
utimensat(AT_FDCWD, dest, tv, AT_SYMLINK_NOFOLLOW);
}
}
} else
saved_errno = errno;
/* Don't fall back to copy, that is a potentially
* dangerous race condition, see CWE-367. */
@@ -222,7 +236,10 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
set_mtime(in, out);
close_exit:
close(out);
if (close(out) && size >= 0) {
saved_errno = errno;
size = -1;
}
close(in);
exit:
@@ -286,6 +303,8 @@ int movefile(const char *src, const char *dst)
int fcopyfile(FILE *src, FILE *dst)
{
char *buf;
size_t len;
int rc, saved_errno;
if (!src || !dst) {
errno = EINVAL;
@@ -296,12 +315,21 @@ int fcopyfile(FILE *src, FILE *dst)
if (!buf)
return -1;
while (fgets(buf, BUFSIZ, src))
fputs(buf, dst);
rc = 0;
while ((len = fread(buf, 1, BUFSIZ, src)) > 0) {
if (fwrite(buf, 1, len, dst) != len) {
rc = -1;
break;
}
}
if (ferror(src))
rc = -1;
saved_errno = errno;
free(buf);
errno = saved_errno;
return 0;
return rc;
}
/**
+7 -1
View File
@@ -53,6 +53,7 @@ ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
char *buf;
size_t tot = 0;
ssize_t blk = BUFSIZ, num = 0;
int saved_errno;
if (!src) {
errno = EINVAL;
@@ -68,8 +69,11 @@ ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
blk = len - tot;
num = fread(buf, 1, blk, src);
if (num == 0)
if (num == 0) {
if (ferror(src))
num = -1;
break;
}
if (dst && (fwrite(buf, num, 1, dst) != 1)) {
num = -1;
@@ -79,7 +83,9 @@ ssize_t fsendfile(FILE *src, FILE *dst, size_t len)
tot += num;
}
saved_errno = errno;
free(buf);
errno = saved_errno;
return (num == -1) ? -1 : (ssize_t)tot;
}
+1
View File
@@ -67,6 +67,7 @@ extern "C"
#define LITE_FOPT_RSYNC_DELETE 0x01 /**< Prune files from destination that are not in source */
#define LITE_FOPT_COPYFILE_SYM 0x01 /**< Recreate symlink or follow to copy target */
#define LITE_FOPT_KEEP_MTIME 0x02 /**< Preserve modification time */
#define LITE_FOPT_RSYNC_LINKS 0x04 /**< Recreate symlinks to files instead of copying their target */
typedef struct lfile lfile_t; /**< Opqaue context struct for lfile APIs */
typedef struct sdbuf sdbuf_t; /**< Opqaue context struct for telnet APIs */
+8 -2
View File
@@ -36,7 +36,8 @@
* @param mode A &mode_t mode to create @a dir with
*
* Directories are created with exactly @a mode, the process umask is
* not applied like it is for mkdir(2).
* not applied like it is for mkdir(2). If @a dir exists but is not a
* directory, the call fails with @c ENOTDIR.
*
* @returns POSIX OK(0) on success, otherwise -1 with @a errno set.
*/
@@ -49,9 +50,14 @@ int mkpath(const char *dir, mode_t mode)
return -1;
}
if (!stat(dir, &sb))
if (!stat(dir, &sb)) {
if (S_ISDIR(sb.st_mode))
return 0;
errno = ENOTDIR;
return -1;
}
mkpath(dirname(strdupa(dir)), mode);
if (mkdir(dir, mode))
+17 -6
View File
@@ -83,6 +83,20 @@ char *readsnf(char *line, size_t len, const char *fmt, ...)
return ln;
}
/* Close fp, keeping errno from a failed fprintf(), its result in rc */
static int wclose(FILE *fp, int rc)
{
int err = errno;
if (rc >= 0)
return fclose(fp);
fclose(fp);
errno = err;
return -1;
}
/**
* Write a string buffer to a file composed from fmt and optional args.
* @param str Pointer to string buffer, may be multiple lines.
@@ -102,8 +116,7 @@ int writesf(const char *str, const char *mode, const char *fmt, ...)
if (!fp)
return -1;
fprintf(fp, "%s\n", str);
return fclose(fp);
return wclose(fp, fprintf(fp, "%s\n", str));
}
/**
@@ -192,8 +205,7 @@ int writellf(long long value, const char *mode, const char *fmt, ...)
if (!fp)
return -1;
fprintf(fp, "%lld\n", value);
return fclose(fp);
return wclose(fp, fprintf(fp, "%lld\n", value));
}
@@ -216,8 +228,7 @@ int writedf(int value, const char *mode, const char *fmt, ...)
if (!fp)
return -1;
fprintf(fp, "%d\n", value);
return fclose(fp);
return wclose(fp, fprintf(fp, "%d\n", value));
}
+94 -13
View File
@@ -24,6 +24,7 @@
#include <errno.h>
#include <fcntl.h> /* AT_* macros */
#include <libgen.h> /* dirname() */
#include <stdlib.h> /* NULL, free() */
#include <string.h> /* strlen() */
#include <strings.h> /* rindex() */
@@ -35,7 +36,7 @@
#include "lite.h"
static int copy(char *src, char *dst, int opt);
static int mdir(char *buf, size_t buf_len, char *dir, char *name, struct stat *st);
static int mdir(char *buf, size_t buf_len, char *dir, char *name, struct stat *st, int opt);
static int prune(char *dst, char **new_files, int new_num);
static int set_mtime(char *fn, struct stat *st);
@@ -44,7 +45,8 @@ static int set_mtime(char *fn, struct stat *st);
* Synchronize contents and optionally remove non-existing backups
* @param src Source directory
* @param dst Destination directory
* @param opt An option mask of ::LITE_FOPT_RSYNC_DELETE, ::LITE_FOPT_KEEP_MTIME
* @param opt An option mask of ::LITE_FOPT_RSYNC_DELETE, ::LITE_FOPT_KEEP_MTIME,
* ::LITE_FOPT_RSYNC_LINKS
* @param filter Optional filtering function for source directory.
*
* This is a miniature implementation of the famous rsync for local use
@@ -59,6 +61,17 @@ static int set_mtime(char *fn, struct stat *st);
*
* %LITE_FOPT_RSYNC_DELETE: Prune files from @p dst that no longer exist in @p src.
* %LITE_FOPT_KEEP_MTIME: Preserve modification time
* %LITE_FOPT_RSYNC_LINKS: Recreate symlinks to files in @p dst
*
* Symlinks in @p src are followed by default, so @p dst gets a copy of
* what they point to. With ::LITE_FOPT_RSYNC_LINKS a symlink to a file,
* or a dangling symlink, is recreated in @p dst with the same target
* text, replacing any file or symlink already there. Symlinks to
* directories in @p src are always followed.
*
* With ::LITE_FOPT_RSYNC_LINKS a symlink in @p dst is never followed.
* If one sits where a file or directory from @p src goes, it is removed
* first.
*
* The filter callback, @p filter, if provided, is used to determine
* what files to include from the source directory when backing up. If
@@ -86,7 +99,7 @@ int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
if (!fexist(src))
return 1;
if (copy(src, dst, keep_mtim))
if (copy(src, dst, opt))
return 1;
return 0;
@@ -104,7 +117,7 @@ int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
if (stat(src, &st))
return 1;
if (mdir(dest, sizeof(dest), dst, ptr, &st))
if (mdir(dest, sizeof(dest), dst, ptr, &st, opt))
return 1;
dst = dest;
do_mtim = keep_mtim;
@@ -124,7 +137,7 @@ int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
continue;
}
if (mdir(dst2, sizeof(dst2), dst, files[i], &sb)) {
if (mdir(dst2, sizeof(dst2), dst, files[i], &sb, opt)) {
result++;
continue;
}
@@ -136,7 +149,7 @@ int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
continue; /* Next file/dir in @src to copy... */
}
if (copy(source, dst, keep_mtim))
if (copy(source, dst, opt))
result++;
}
@@ -157,23 +170,91 @@ int rsync(char *src, char *dst, int opt, int (*filter)(const char *file))
return result;
}
/* Leaves errno alone, a missing @path is not an error to rsync() */
static int islink(const char *path)
{
int rc, saved_errno = errno;
struct stat st;
rc = !lstat(path, &st) && S_ISLNK(st.st_mode);
errno = saved_errno;
return rc;
}
static int copy(char *src, char *dst, int opt)
{
copyfile(src, dst, 0, opt | LITE_FOPT_COPYFILE_SYM);
if (errno) {
if (errno != EEXIST)
return 1;
char *dest = NULL, *temp = NULL, *file = NULL;
int result = 1, saved_errno, made_temp = 0;
int copt = opt & LITE_FOPT_KEEP_MTIME;
errno = 0;
if (!(opt & LITE_FOPT_RSYNC_LINKS)) {
copyfile(src, dst, 0, copt);
return errno ? 1 : 0;
}
copt |= LITE_FOPT_COPYFILE_SYM;
if (fisdir(dst)) {
char *base = strrchr(src, '/');
if (asprintf(&dest, "%s%s%s", dst, fisslashdir(dst) ? "" : "/", base ? base + 1 : src) < 0)
return 1;
} else {
dest = strdup(dst);
if (!dest)
return 1;
}
return 0;
/*
* A link in the destination, e.g. from a previous run, may point
* anywhere. Remove it so copyfile() cannot write through it.
*/
if (islink(dest) && !islink(src))
unlink(dest);
copyfile(src, dst, 0, copt);
if (errno != EEXIST) {
result = errno ? 1 : 0;
goto exit;
}
/* Replace an existing destination only after the new link is ready. */
if (asprintf(&temp, "%s/.rsync-XXXXXX", dirname(strdupa(dest))) < 0)
goto exit;
if (!mkdtemp(temp))
goto exit;
made_temp = 1;
if (asprintf(&file, "%s/link", temp) < 0)
goto exit;
copyfile(src, file, 0, copt);
if (errno)
goto exit;
result = rename(file, dest) != 0;
exit:
saved_errno = errno;
if (file)
unlink(file);
if (made_temp)
rmdir(temp);
free(file);
free(temp);
free(dest);
errno = saved_errno;
return result;
}
/* Creates dir/name @mode ... skipping / if dir already ends so. */
static int mdir(char *buf, size_t buf_len, char *dir, char *name, struct stat *st)
static int mdir(char *buf, size_t buf_len, char *dir, char *name, struct stat *st, int opt)
{
snprintf(buf, buf_len, "%s%s%s", dir, fisslashdir(dir) ? "" : "/", name);
/* Same as in copy(), do not descend into a stale link. */
if ((opt & LITE_FOPT_RSYNC_LINKS) && islink(buf))
unlink(buf);
if (mkdir(buf, st->st_mode)) {
if (EEXIST != errno)
return 1;
+3
View File
@@ -49,6 +49,9 @@ static char *strip_args(char *path)
* Like which(1), or `command -v foo`
* @param cmd Command to look for in $PATH
*
* Empty components in $PATH are skipped, they do not mean the current
* directory. To search it, list it, e.g., as ".".
*
* @returns A malloc()'ed path to @a cmd on success, or @c NULL.
*/
char *which(const char *cmd)
+3
View File
@@ -4,6 +4,8 @@
.deps/
/chomp
/copyfile
/fcopyfile
/copyfile-symlink
/dir
/fexist
/fisdir
@@ -13,6 +15,7 @@
/makepath
/pidfile
/printhdr
/procval
/progress
/rsync
/runbg
+4
View File
@@ -19,6 +19,8 @@ clean-local:
TESTS =
TESTS += chomp
TESTS += copyfile
TESTS += fcopyfile
TESTS += copyfile-symlink
TESTS += dir
TESTS += fexist
TESTS += fisdir
@@ -28,6 +30,7 @@ TESTS += lfile
TESTS += makepath
TESTS += pidfile
TESTS += printhdr
TESTS += procval
TESTS += progress
TESTS += rsync
TESTS += runbg
@@ -40,3 +43,4 @@ TESTS += which
TESTS += yorn
check_PROGRAMS = $(TESTS)
copyfile_symlink_SOURCES = copyfile-symlink.c
+152
View File
@@ -0,0 +1,152 @@
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "check.h"
static void check_link(const char *path, const char *target)
{
char buf[256];
struct stat st;
ssize_t len;
fail_unless(lstat(path, &st) == 0 && S_ISLNK(st.st_mode));
len = readlink(path, buf, sizeof(buf));
fail_unless(len == (ssize_t)strlen(target));
fail_unless(memcmp(buf, target, len) == 0);
}
/* A link left in the destination by an earlier run must never be written through. */
static void rsync_stale_links(void)
{
struct stat st;
FILE *fp;
fail_unless(mkdir("src", 0700) == 0 && mkdir("dst", 0700) == 0);
fail_unless(touch("victim") == 0);
fail_unless(symlink("../victim", "src/a") == 0);
fail_unless(symlink("../victims", "src/d") == 0);
fail_unless(rsync("src/", "dst/", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
check_link("dst/a", "../victim");
check_link("dst/d", "../victims");
/* The source links become a file and a directory, and dst/d stops dangling. */
fail_unless(unlink("src/a") == 0 && unlink("src/d") == 0);
fp = fopen("src/a", "w");
fail_unless(fp && fputs("data", fp) >= 0 && fclose(fp) == 0);
fail_unless(mkdir("src/d", 0700) == 0 && touch("src/d/f") == 0);
fail_unless(mkdir("victims", 0700) == 0);
fail_unless(rsync("src/", "dst/", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
fail_unless(lstat("dst/a", &st) == 0 && S_ISREG(st.st_mode) && st.st_size == 4);
fail_unless(stat("victim", &st) == 0 && st.st_size == 0);
fail_unless(lstat("dst/d", &st) == 0 && S_ISDIR(st.st_mode));
fail_unless(fexist("dst/d/f") && !fexist("victims/f"));
fail_unless(unlink("src/a") == 0 && unlink("src/d/f") == 0 && rmdir("src/d") == 0);
fail_unless(unlink("dst/a") == 0 && unlink("dst/d/f") == 0 && rmdir("dst/d") == 0);
fail_unless(rmdir("src") == 0 && rmdir("dst") == 0);
fail_unless(unlink("victim") == 0 && rmdir("victims") == 0);
}
int main(void)
{
char tmp[] = "/tmp/libite-copylink-XXXXXX";
const char payload[] = "target contents";
char buf[sizeof(payload)];
const char *targets[] = { "file", "dir", "missing", ".", "link" };
struct timespec times[2] = { { 1234567800, 0 }, { 1234567801, 0 } };
struct stat src_st, dst_st;
char cwd[4096];
size_t i;
FILE *fp;
fail_unless(getcwd(cwd, sizeof(cwd)) && mkdtemp(tmp));
fail_unless(chdir(tmp) == 0);
fail_unless(mkdir("dir", 0700) == 0);
fail_unless(mkdir("out", 0700) == 0);
fp = fopen("file", "w");
fail_unless(fp && fwrite(payload, 1, sizeof(payload), fp) == sizeof(payload));
fail_unless(fclose(fp) == 0);
for (i = 0; i < NELEMS(targets); i++) {
fail_unless(symlink(targets[i], "link") == 0);
fail_unless(copyfile("link", "copied", 0, LITE_FOPT_COPYFILE_SYM) == 1);
check_link("copied", targets[i]);
fail_unless(copyfile("link", "out", 0, LITE_FOPT_COPYFILE_SYM) == 1);
check_link("out/link", targets[i]);
/* Existing targets must survive a failed symlink creation. */
errno = 0;
fail_unless(copyfile("link", "file", 0, LITE_FOPT_COPYFILE_SYM) == 0);
fail_unless(errno == EEXIST);
check_link("copied", targets[i]);
fail_unless(unlink("copied") == 0 && unlink("out/link") == 0);
fail_unless(unlink("link") == 0);
}
fail_unless(symlink("file", "link") == 0);
/* Without SYM, continue to copy target data and preserve regular-file metadata. */
fail_unless(copyfile("link", "copied", 0, LITE_FOPT_KEEP_MTIME) == sizeof(payload));
fail_unless(lstat("copied", &dst_st) == 0 && S_ISREG(dst_st.st_mode));
fail_unless(stat("file", &src_st) == 0 && src_st.st_mtime == dst_st.st_mtime);
fp = fopen("copied", "r");
fail_unless(fp && fread(buf, 1, sizeof(buf), fp) == sizeof(payload));
fail_unless(memcmp(buf, payload, sizeof(payload)) == 0 && fclose(fp) == 0);
fail_unless(unlink("copied") == 0);
fail_unless(utimensat(AT_FDCWD, "link", times, AT_SYMLINK_NOFOLLOW) == 0);
fail_unless(copyfile("link", "copied", 0, LITE_FOPT_COPYFILE_SYM | LITE_FOPT_KEEP_MTIME) == 1);
check_link("copied", "file");
fail_unless(lstat("copied", &dst_st) == 0 && dst_st.st_mtime == times[1].tv_sec);
fail_unless(unlink("copied") == 0);
/* rsync() follows links unless asked to recreate them. */
fail_unless(rsync("link", "copied", 0, NULL) == 0);
fail_unless(lstat("copied", &dst_st) == 0 && S_ISREG(dst_st.st_mode));
fail_unless(dst_st.st_size == sizeof(payload));
fail_unless(unlink("copied") == 0);
fail_unless(rsync("link", "copied", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
check_link("copied", "file");
/* Reuse the destination after changing the source link. */
fail_unless(unlink("link") == 0 && symlink("file", "link") == 0);
fail_unless(rsync("link", "out", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
check_link("out/link", "file");
fail_unless(unlink("link") == 0 && symlink("copied", "link") == 0);
fail_unless(rsync("link", "out", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
check_link("out/link", "copied");
/* A same-path copy must not unlink its source. */
fail_unless(rsync("link", "link", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
check_link("link", "copied");
fail_unless(unlink("out/link") == 0);
/* Replacing a regular destination must also retain the source link. */
fail_unless(touch("out/link") == 0);
fail_unless(rsync("link", "out", LITE_FOPT_RSYNC_LINKS, NULL) == 0);
check_link("out/link", "copied");
fail_unless(unlink("out/link") == 0);
/* A failed replacement must keep both the destination and source. */
fail_unless(mkdir("out/link", 0700) == 0);
fail_unless(touch("out/link/keep") == 0);
fail_unless(rsync("link", "out", LITE_FOPT_RSYNC_LINKS, NULL) != 0);
fail_unless(fexist("out/link/keep"));
check_link("link", "copied");
fail_unless(unlink("out/link/keep") == 0 && rmdir("out/link") == 0);
if (getuid() != 0) {
fail_unless(mkdir("protected", 0700) == 0);
fail_unless(symlink("original", "protected/link") == 0);
fail_unless(chmod("protected", 0500) == 0);
fail_unless(rsync("link", "protected", LITE_FOPT_RSYNC_LINKS, NULL) != 0);
check_link("protected/link", "original");
check_link("link", "copied");
fail_unless(chmod("protected", 0700) == 0);
fail_unless(unlink("protected/link") == 0 && rmdir("protected") == 0);
}
fail_unless(unlink("copied") == 0 && unlink("link") == 0);
rsync_stale_links();
errno = 0;
fail_unless(copyfile("dir", "copied", 0, LITE_FOPT_COPYFILE_SYM) == 0 && errno == EISDIR);
errno = 0;
fail_unless(copyfile("missing", "copied", 0, LITE_FOPT_COPYFILE_SYM) == -1 && errno == ENOENT);
fail_unless(unlink("file") == 0 && rmdir("dir") == 0 && rmdir("out") == 0);
fail_unless(chdir(cwd) == 0 && rmdir(tmp) == 0);
return 0;
}
+30 -1
View File
@@ -1,5 +1,7 @@
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <sys/resource.h>
#include "check.h"
#define ERR(fmt, args...) { printf(fmt ": %s\n", ##args, strerror(errno)); rc = 1; }
@@ -145,10 +147,37 @@ static int _mode(void)
return test(rc, "copyfile() preserves mode 0644 with umask 0077");
}
/* A write error halfway through is an error, not a shorter copy */
static int _werr(void)
{
const char *dst = "/tmp/libite-werr";
struct rlimit old, lim;
ssize_t len;
int rc;
signal(SIGXFSZ, SIG_IGN);
if (getrlimit(RLIMIT_FSIZE, &old))
return test(1, "copyfile() write error, getrlimit()");
lim = old;
lim.rlim_cur = BUFSIZ + BUFSIZ / 2;
if (setrlimit(RLIMIT_FSIZE, &lim))
return test(1, "copyfile() write error, setrlimit()");
len = copyfile("/dev/zero", dst, 4 * BUFSIZ, 0);
rc = len != -1 || errno != EFBIG;
setrlimit(RLIMIT_FSIZE, &old);
erase(dst);
return test(rc, "copyfile() reports write error");
}
int main(void)
{
return _fcopyfile() ||
_copyfile() ||
_sz() ||
_mode();
_mode() ||
_werr();
}
+92
View File
@@ -0,0 +1,92 @@
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "check.h"
static void copy_binary(size_t len)
{
unsigned char data[3 * BUFSIZ + 37], copied[sizeof(data)];
const char prefix[] = "destination prefix";
FILE *src = tmpfile(), *dst = tmpfile();
size_t i;
fail_unless(src && dst);
for (i = 0; i < sizeof(data); i++)
data[i] = i % 253;
/* Begin at the current stream positions, preserving the destination prefix. */
fail_unless(fwrite(prefix, 1, sizeof(prefix), src) == sizeof(prefix));
fail_unless(fwrite(data, 1, len, src) == len);
fail_unless(fseek(src, sizeof(prefix), SEEK_SET) == 0);
fail_unless(fwrite(prefix, 1, sizeof(prefix), dst) == sizeof(prefix));
fail_unless(fcopyfile(src, dst) == 0);
fail_unless(ftell(src) == (long)(sizeof(prefix) + len));
fail_unless(ftell(dst) == (long)(sizeof(prefix) + len));
fail_unless(fseek(dst, 0, SEEK_SET) == 0);
fail_unless(fread(copied, 1, sizeof(prefix), dst) == sizeof(prefix));
fail_unless(memcmp(copied, prefix, sizeof(prefix)) == 0);
fail_unless(fread(copied, 1, sizeof(copied), dst) == len);
fail_unless(memcmp(copied, data, len) == 0);
fail_unless(fgetc(dst) == EOF && feof(dst));
fail_unless(fclose(src) == 0);
fail_unless(fclose(dst) == 0);
}
static void read_error(int partial)
{
const char data[] = "bytes before the read error";
char copied[sizeof(data)];
int fd[2];
FILE *src, *dst = tmpfile();
fail_unless(dst && pipe(fd) == 0);
fail_unless(fcntl(fd[0], F_SETFL, O_NONBLOCK) == 0);
if (partial)
fail_unless(write(fd[1], data, sizeof(data)) == sizeof(data));
src = fdopen(fd[0], "r");
fail_unless(src);
errno = 0;
fail_unless(fcopyfile(src, dst) == -1);
fail_unless(errno == EAGAIN || errno == EWOULDBLOCK);
fail_unless(ferror(src));
fail_unless(ftell(dst) == (long)(partial ? sizeof(data) : 0));
if (partial) {
rewind(dst);
fail_unless(fread(copied, 1, sizeof(copied), dst) == sizeof(data));
fail_unless(memcmp(copied, data, sizeof(data)) == 0);
}
fclose(src);
fclose(dst);
close(fd[1]);
}
int main(void)
{
const size_t lengths[] = { 0, 1, BUFSIZ - 1, BUFSIZ, BUFSIZ + 1, 3 * BUFSIZ + 37 };
size_t i;
for (i = 0; i < NELEMS(lengths); i++)
copy_binary(lengths[i]);
read_error(0);
read_error(1);
#ifdef __linux__
FILE *src = tmpfile();
FILE *dst = fopen("/dev/full", "w");
fail_unless(src && dst);
fail_unless(fputs("data", src) >= 0);
rewind(src);
fail_unless(setvbuf(dst, NULL, _IONBF, 0) == 0);
errno = 0;
fail_unless(fcopyfile(src, dst) == -1);
fail_unless(errno == ENOSPC);
fail_unless(ferror(dst));
fclose(src);
fclose(dst);
#endif
errno = 0;
fail_unless(fcopyfile(NULL, NULL) == -1 && errno == EINVAL);
return 0;
}
+35
View File
@@ -2,6 +2,39 @@
#include <unistd.h>
#include "check.h"
/* A failed read is an error, not EOF */
static void read_error(void)
{
int fd[2];
ssize_t rc;
FILE *src;
if (pipe(fd) || fcntl(fd[0], F_SETFL, O_NONBLOCK))
err(1, "Failed creating nonblocking pipe");
if (write(fd[1], "data", 4) != 4)
err(1, "Failed writing to pipe");
src = fdopen(fd[0], "r");
if (!src)
err(1, "Failed fdopen()");
printf("fsendfile(pipe, NULL, 4)\t");
rc = fsendfile(src, NULL, 4);
if (rc != 4)
errx(1, "Failed, got %zd, expected 4", rc);
printf("OK\n");
printf("fsendfile(empty pipe, NULL, 0)\t");
errno = 0;
rc = fsendfile(src, NULL, 0);
if (rc != -1 || errno != EAGAIN)
errx(1, "Failed, got %zd errno %d, expected -1 and EAGAIN", rc, errno);
printf("OK\n");
fclose(src);
close(fd[1]);
}
int main(void)
{
int i = 0;
@@ -27,5 +60,7 @@ int main(void)
i += 2;
}
read_error();
return 0;
}
+43 -3
View File
@@ -78,6 +78,42 @@ int test_umask(void)
return 0;
}
static int notdir(int rc)
{
return rc == -1 && errno == ENOTDIR;
}
/* An existing non-directory is not a created directory */
int test_nondir(void)
{
const char *bad[] = { BASEDIR "/file", BASEDIR "/file-link", NULL };
const char *dirlink = BASEDIR "/dir-link";
int i;
if (touch(bad[0]) ||
symlink("file", bad[1]) ||
symlink("um", dirlink)) {
perror("Failed setup");
return 1;
}
for (i = 0; bad[i]; i++) {
if (!notdir(mkpath(bad[i], 0755)) ||
!notdir(fmkpath(0755, "%s", bad[i])) ||
!notdir(makepath(bad[i]))) {
fprintf(stderr, "Failed: %s accepted as a directory\n", bad[i]);
return 1;
}
}
if (mkpath(dirlink, 0755)) {
perror("Failed symlink to directory");
return 1;
}
return 0;
}
int main(void)
{
int i, ret = 0;
@@ -94,18 +130,22 @@ int main(void)
mkdir(BASEDIR, 0755);
printf("Pass 1/3: makepath() =========================\n");
printf("Pass 1/4: makepath() =========================\n");
for (i = 0; list[i] && !ret; i++)
ret |= test_makepath(list[i]);
printf("Pass 2/3: fmkpath() ==========================\n");
printf("Pass 2/4: fmkpath() ==========================\n");
for (i = 0; list[i] && !ret; i++)
ret |= test_fmkpath(list[i] + strlen(BASEDIR) + 1);
printf("Pass 3/3: mkpath() with umask ================\n");
printf("Pass 3/4: mkpath() with umask ================\n");
if (!ret)
ret |= test_umask();
printf("Pass 4/4: mkpath() on non-directories ========\n");
if (!ret)
ret |= test_nondir();
printf("Done: Cleaning up ============================\n");
system("rm -rf " BASEDIR);
+61
View File
@@ -0,0 +1,61 @@
#include <errno.h>
#include "check.h"
int main(void)
{
char path[] = "/tmp/libite-procval-XXXXXX";
char buf[32];
long long llval;
int fd, val;
fd = mkstemp(path);
fail_unless(fd >= 0);
close(fd);
fail_unless(writesf("first", "w", "%s", path) == 0);
fail_unless(writesf("second", "a", "%s", path) == 0);
fail_unless(readsnf(buf, sizeof(buf), "%s", path) == buf);
fail_unless(!strcmp(buf, "first"));
fail_unless(writesf("42", "w", "%s", path) == 0);
fail_unless(readdf(&val, "%s", path) == 0);
fail_unless(val == 42);
fail_unless(writedf(-7, "w", "%s", path) == 0);
fail_unless(readdf(&val, "%s", path) == 0);
fail_unless(val == -7);
fail_unless(writellf(1LL << 40, "w", "%s", path) == 0);
fail_unless(readllf(&llval, "%s", path) == 0);
fail_unless(llval == 1LL << 40);
/* Not open for writing, fprintf() fails but fclose() does not */
errno = 0;
fail_unless(writesf("data", "r", "%s", path) == -1);
fail_unless(errno == EBADF);
fail_unless(writedf(1, "r", "%s", path) == -1);
fail_unless(writellf(1, "r", "%s", path) == -1);
if (!access("/dev/full", W_OK)) {
char big[4 * BUFSIZ];
memset(big, 'x', sizeof(big) - 1);
big[sizeof(big) - 1] = 0;
errno = 0;
fail_unless(writesf(big, "w", "/dev/full") == -1);
fail_unless(errno == ENOSPC);
}
fail_unless(unlink(path) == 0);
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* compile-command: "make procval && ./procval"
* End:
*/
+36 -1
View File
@@ -6,6 +6,41 @@ struct tc {
int res;
};
/* Empty PATH components are skipped, they do not mean current directory */
static int empty(void)
{
const char *cmd = "executable";
char *path[] = {
"",
":",
":/nonexistent",
"/nonexistent:",
"/nonexistent::/nonexistent",
NULL
};
int result = 0;
size_t i;
if (chdir("data/which"))
return 1;
for (i = 0; path[i]; i++) {
setenv("PATH", path[i], 1);
if (whichp(cmd)) {
fprintf(stderr, "%20s : found in cwd with PATH='%s'\n", cmd, path[i]);
result++;
}
}
setenv("PATH", "/nonexistent:.", 1);
if (!whichp(cmd)) {
fprintf(stderr, "%20s : not found with . in PATH\n", cmd);
result++;
}
return result;
}
int main(void)
{
int result = 0;
@@ -38,7 +73,7 @@ int main(void)
for (i = 0; test[i].cmd; i++)
result += test[i].exp != test[i].res;
return result;
return result + empty();
}
/**