Update Source To removefile-49.120.1

This commit is contained in:
Thomas A 2023-02-28 22:26:55 -08:00
parent d74822f79b
commit eef9e4adcc
6 changed files with 90 additions and 15 deletions

View File

@ -24,11 +24,11 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd Feb 26, 2015
.Dd April 1, 2020
.Dt REMOVEFILE 3
.Os
.Sh NAME
.Nm removefile , removefile_state_alloc ,
.Nm removefile , removefileat , removefile_state_alloc ,
.Nm removefile_state_free , removefile_state_get ,
.Nm removefile_state_set
.Nd remove files or directories
@ -36,6 +36,8 @@
.In removefile.h
.Ft int
.Fn removefile "const char *path" "removefile_state_t state" "removefile_flags_t flags"
.Ft int
.Fn removefileat "int fd" "const char *path" "removefile_state_t state" "removefile_flags_t flags"
.Ft removefile_state_t
.Fn removefile_state_alloc "void"
.Ft int
@ -47,8 +49,8 @@
.Ft int
.Fn removefile_cancel "removefile_state_t state"
.Sh DESCRIPTION
.Pp
These functions are used to remove a file or directory. Various levels
These functions are used to remove a file or directory.
Various levels
of overwriting may be specified to prevent other people from recovering any
information about the file.
.Pp
@ -68,10 +70,26 @@ The
function is used to deallocate the object and its contents.
.Pp
The
.Fn removefileat
function is equivalent to the
.Fn removefile
function except in the case where the
.Va path
specifies a relative path.
In that case the file to be removed is determined relative to the directory associated with the file descriptor
.Va fd
instead of the current working directory.
If the special value AT_FDCWD is passed in the
.Va fd
argument, the current working directory is used and the behavior is identical to a call to
.Fn removefile.
.Pp
The
.Fn removefile
function removes files and directories located at the named
.Va path
filesystem location. The named
filesystem location.
The named
.Va path
location can be specified as either an absolute path or relative to the working directory
of the calling process.
@ -97,12 +115,14 @@ location is a directory, then recursively delete the entire directory.
.It Dv REMOVEFILE_KEEP_PARENT
The file or directory at the
.Va path
location is not deleted. If specified in conjunction with REMOVEFILE_RECURSIVE,
location is not deleted.
If specified in conjunction with REMOVEFILE_RECURSIVE,
then all of the contents of the directory at
.Va path
location will be deleted, but not the directory itself.
.It Dv REMOVEFILE_CROSS_MOUNT
By default, recursive traversals do not cross mount points. This option allows
By default, recursive traversals do not cross mount points.
This option allows
.Fn removefile
to descend into directories that have a different device number than the file from which
the descent began.
@ -236,7 +256,6 @@ parameter was passed into
In addition, all functions may return an error from an underlying library or
system call.
.Sh NOTES
.Pp
Write protected files owned by another user cannot be removed by
.Fn removefile ,
regardless of the permissions on the directory containing the file.
@ -252,7 +271,7 @@ that may add risk when run with privileges.
.Pp
.Fn removefile
operates on symbolic links, rather than the target of the link.
.Sh EXAMPLES
.Sh EXAMPLE
.Bd -literal -offset indent
/* Initialize a state variable */
removefile_state_t s;

View File

@ -142,3 +142,39 @@ removefile_cancel(removefile_state_t state) {
return 0;
}
}
int
removefileat(int fd, const char* path, removefile_state_t state_param, removefile_flags_t flags) {
int error = 0;
if (path == NULL) {
errno = EINVAL;
return -1;
}
// check if the path is absolute, or the AT_FDCWD argument was given
char c = *(path);
if (c == '/' || fd == AT_FDCWD)
return removefile(path, state_param, flags);
// get the fd path
char* base_path = malloc(PATH_MAX);
if (base_path == NULL) {
errno = ENOMEM;
return -1;
}
error = fcntl(fd, F_GETPATH, base_path);
// generate the base_path to call in removefile
if (error == 0) {
// add a '/' between base_path and the relative path.
if (snprintf(base_path, PATH_MAX, "%s/%s", base_path, path) < PATH_MAX) {
error = removefile(base_path, state_param, flags);
} else {
error = -1;
errno = ENAMETOOLONG;
}
}
free(base_path);
return error;
}

View File

@ -61,6 +61,7 @@ enum {
int removefile(const char* path, removefile_state_t state, removefile_flags_t flags);
int removefileat(int fd, const char* path, removefile_state_t state, removefile_flags_t flags);
int removefile_cancel(removefile_state_t state);

View File

@ -229,6 +229,7 @@
"-lsystem_malloc",
"-lsystem_c",
);
OTHER_TAPI_FLAGS = "-umbrella System";
"SIM_SUFFIX[sdk=iphonesimulator*]" = _sim;
VERSION_INFO_PREFIX = "__attribute__((visibility(\"hidden\")))";
WARNING_CFLAGS = "-Wall";

View File

@ -101,14 +101,14 @@ int main(int argc, char *argv[]) {
assert((state = removefile_state_alloc()) != NULL);
assert(pthread_create(&thread, NULL, threadproc, state) == 0);
start_timer("removefile(state) with cancel");
assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CALLBACK, removefile_error_callback) == 0);
assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CONTEXT, (void*)4567) == 0);
assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CALLBACK, removefile_error_callback) == 0);
assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CONTEXT, (void*)4567) == 0);
assert(removefile("/tmp/removefile-test", state, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == -1 && errno == ECANCELED);
stop_timer();
start_timer("removefile(NULL)");
assert(removefile("/tmp/removefile-test", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
stop_timer();
start_timer("removefile(NULL)");
assert(removefile("/tmp/removefile-test", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
stop_timer();
mkdirs();
assert(removefile_state_set(state, 1234567, (void*)1234567) == -1 && errno == EINVAL);
@ -132,5 +132,22 @@ int main(int argc, char *argv[]) {
assert(removefile("/tmp/removefile-test", state, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
stop_timer();
int fd;
mkdirs();
assert((fd = open("/tmp/removefile-test", O_RDONLY)) != -1);
start_timer("removefileat(NULL)");
assert(removefileat(fd, "/tmp/removefile-test/foo/baz/woot", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
assert(removefileat(fd, "../removefile-test/foo/baz", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
assert(removefileat(fd, "foo/bar", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
assert(removefileat(fd, "./foo", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
char path[1024];
memset_pattern4(path, "././", 1000);
path[1000] = NULL;
assert(removefileat(fd, path, NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == -1 && errno == ENAMETOOLONG);
assert(removefileat(AT_FDCWD, "/tmp/removefile-test", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
stop_timer();
close(fd);
return 0;
}

View File

@ -36,7 +36,8 @@ LinkManPages removefile.3 \
removefile_state_alloc.3 \
removefile_state_free.3 \
removefile_state_get.3 \
removefile_state_set.3
removefile_state_set.3 \
removefileat.3
InstallManPages checkint.3
LinkManPages checkint.3 \