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>
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
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
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
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
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>
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>
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.
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.
copyfile() passes the source mode to open(2), which applies the umask,
so a 0644 file copied with umask 0027 ends up 0640.
Apply the source mode with fchmod() after creating the destination.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
mkpath(dir, 0755) with umask 0027 silently creates a 0750 directory,
since mkdir(2) applies the umask to the requested mode. Callers pass
an explicit mode and expect exactly that.
Apply the mode with chmod() after each successful mkdir().
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Avoid leaking memory when realloc() fails by using a temporary pointer
and only updating the original on success. Also remove redundant NULL
check before free().
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
For some builds using Buildroot, the system default _PATH_VARRUN did not
include a trailing slas '/', which cause the pidfile() function to fail.
Example:
pidfile("arg") => "/var/runarg.pid"
Since the __pidfile_path can be overridden we add a check if that prefix
path ends with '/', if not we add one in the path composition.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
The runbg() function is a delayed execvp() that has double-forked to the
background (to not create zombies). It is useful for running commands
in the background for APIs that need to return a reply, e.g. a NETCONF
RPC to reboot the system.
Compared to the original in Infix, this version double forks to reparent
itself to PID 1 (init), closes all open files, including stdio.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>