REMOVEFILE(3)
REMOVEFILE(3) β Library Functions Manual β¨
okay bestie, welcome to REMOVEFILE(3) from the Library Functions Manual. This is the whole squad for removing files or directories, and you can even pick overwrite levels so nobody recovers your deleted info. π
π NAME π
removefile, removefileat, removefile_state_alloc, removefile_state_free, removefile_state_get, removefile_state_set β remove files or directories.
β¨ SYNOPSIS β¨
#include <removefile.h>
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);
removefile_state_t
removefile_state_alloc(void);
int
removefile_state_free(removefile_state_t state);
int
removefile_state_get(removefile_state_t state, uint32_t key,
void * dst);
int
removefile_state_set(removefile_state_t state, uint32_t key,
const void * value);
int
removefile_cancel(removefile_state_t state);
π DESCRIPTION π
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. π₯
removefile_state_alloc() initializes a removefile_state_t object (which is an opaque data type). You can pass this object to removefile(). removefile_state_get() and removefile_state_set() can be used to manipulate the state (see below). removefile_state_free() is used to deallocate the object and its contents. π«Ά
removefileat() is equivalent to the removefile() function except in the case where the path specifies a relative path. In that case the file to be removed is determined relative to the directory associated with the file descriptor fd instead of the current working directory. If the special value AT_FDCWD is passed in the fd argument, the current working directory is used and the behavior is identical to a call to removefile(). πΈ
removefile() removes files and directories located at the named path filesystem location. The named path location can be specified as either an absolute path or relative to the working directory of the calling process. If the state parameter is the return value from removefile_state_alloc(), then removefile() will use the information from the state object; if it is NULL, then removefile() will work normally, but less control will be available to the caller. The flags parameter controls deletion options:
REMOVEFILE_RECURSIVEβ If thepathlocation is a directory, then recursively delete the entire directory.REMOVEFILE_KEEP_PARENTβ The file or directory at thepathlocation is not deleted. If specified in conjunction withREMOVEFILE_RECURSIVE, then all of the contents of the directory atpathlocation will be deleted, but not the directory itself.REMOVEFILE_CROSS_MOUNTβ By default, recursive traversals do not cross mount points. This option allowsremovefile()to descend into directories that have a different device number than the file from which the descent began.REMOVEFILE_SECURE_7_PASSβ Overwrite the file with 7 US DoD compliant passes (0xF6, 0x00, 0xFF, random, 0x00, 0xFF, random).REMOVEFILE_SECURE_35_PASSβ Overwrite the file using 35-pass Gutmann algorithm.REMOVEFILE_SECURE_3_PASSβ Overwrite the file twice with random bytes, and then with0xAA.REMOVEFILE_SECURE_1_PASSβ Overwrite with a single pass of random data.REMOVEFILE_SECURE_1_PASS_ZEROβ Overwrite with a single pass of zeroes.REMOVEFILE_ALLOW_LONG_PATHSβ Allow paths traversed internally to exceed thePATH_MAXconstant. This requires changing the working directory of the process that has called intoremovefile()temporarily. (This does not remove the requirement that no component of thepathlocation exceedsNAME_MAXcharacters, nor does it allow thepathargument itself to exceedPATH_MAX.)
The removefile_state_get() and removefile_state_set() functions can be used to manipulate the removefile_state_t object returned by removefile_state_alloc(). In both functions, the dst or the value parameter's type depends on the key parameter that is passed in. π
REMOVEFILE_STATE_CONFIRM_CALLBACKβ Get or set the callback function of typeremovefile_callback_tto be called prior to file deletion.REMOVEFILE_STATE_CONFIRM_CONTEXTβ Get or set any parameters of typevoid *that are needed for the confirm callback function.REMOVEFILE_STATE_ERROR_CALLBACKβ Get or set the callback function of typeremovefile_callback_tto be called when an error is detected.REMOVEFILE_STATE_ERROR_CONTEXTβ Get or set any parameters of typevoid *that are needed for the error callback function.REMOVEFILE_STATE_ERRNOβ Get or set the currenterrnoof typeint.REMOVEFILE_STATE_STATUS_CALLBACKβ Get or set the callback function of typeremovefile_callback_tto be called subsequent to file deletion.REMOVEFILE_STATE_STATUS_CONTEXTβ Get or set any parameters of typevoid *that are needed for the status callback function.REMOVEFILE_STATE_FTSENTβ Get any available file entry information of typeFTSENT *(setting is not allowed).
The removefile_callback_t function pointer is defined as the following:
int (*removefile_callback_t) (removefile_state_t state, const char
*path, void *context)
The return value of the callback function is given as:
REMOVEFILE_PROCEEDβ File is deleted andremovefile()continues operation as normal.REMOVEFILE_SKIPβ Current file is not deleted andremovefile()continues operation as normal.REMOVEFILE_STOPβ Current file is not deleted andremovefile()exits without continuing further.
The removefile_cancel() function is used to cancel a remove that is in progress. π
π RETURN VALUES π
The family of removefile() functions returns less than 0 on error, and 0 on success.
π ERRORS π
removefile() will fail if:
[EACCES]β Thepathlocation specifies a file or directory for which the calling process does not have proper permissions.[EINVAL]β A callback returned an invalid return value (notREMOVEFILE_PROCEED,REMOVEFILE_SKIP, orREMOVEFILE_STOP).[EMLINK]β Thepathlocation refers to a symbolic link.[ENAMETOOLONG]β A component of thepathlocation exceedsNAME_MAXcharacters, or the entirepathlocation exceedsPATH_MAXcharacters (andREMOVEFILE_ALLOW_LONG_PATHSis not passed).[ENOMEM]β A memory allocation failed.[ENOTEMPTY]β Thepathlocation specifies a directory that contains an immutable file which cannot be deleted.[EPERM]β Thepathlocation specifies an immutable file that cannot be deleted.
removefileat() will fail if:
[ENOTDIR]β Thepathargument is not an absolute path andfdis neitherAT_FDCWDnor a file descriptor associated with a directory.
removefile_cancel() will fail if:
[EINVAL]β ANULLparameter was passed intoremovefile_cancel().
In addition, all functions may return an error from an underlying library or system call. π
π¦ NOTES π¦
- Write protected files owned by another user cannot be removed by
removefile(), regardless of the permissions on the directory containing the file. - If multiple of the
REMOVEFILE_SECURE_1_PASS,REMOVEFILE_SECURE_7_PASS, andREMOVEFILE_SECURE_35_PASSflags are specified,removefile()will proceed using the flag that specifies the highest number of overwriting passes. removefile()is pathname-based; this means that, when descending into a hierarchy, there are potential race conditions that may add risk when run with privileges.removefile()operates on symbolic links, rather than the target of the link.REMOVEFILE_ALLOW_LONG_PATHSmay change the working directory of the process, making it not thread safe.
π EXAMPLE π
/* Initialize a state variable */
removefile_state_t s;
s = removefile_state_alloc();
/* Recursively remove all files and directories while keeping parent tmp directory. */
removefile("/tmp", s, REMOVEFILE_RECURSIVE | REMOVEFILE_KEEP_PARENT);
/* Release the state variable */
removefile_state_free(s);
/* A more complex way to call removefile() -- define a callback function */
int removefile_status_callback(removefile_state_t state, const char * path, void * context) {
fprintf(stderr, "File deleted: %s", path);
return REMOVEFILE_PROCEED;
}
/* Initialize a state variable */
s = removefile_state_alloc();
/* Set callback function properties */
removefile_state_set(s, REMOVEFILE_STATE_CONFIRM_CALLBACK, removefile_confirm_callback);
removefile_state_set(s, REMOVEFILE_STATE_CONFIRM_CONTEXT, NULL);
/* Recursively remove all files and directories while keeping parent tmp directory,
calling a confirm callback prior to each file deletion. */
removefile("/tmp", s, REMOVEFILE_RECURSIVE | REMOVEFILE_KEEP_PARENT);
/* Release the state variable. */
removefile_state_free(s);
πΈ SEE ALSO πΈ
unlink(1), sync(2), sync_volume_np(3)
π HISTORY π
The removefile() API was introduced in Mac OS X 10.5.
π cute sign-off: this page is from Debian, dated August 4, 2023 π
π¦ where this page comes from
- nixpkgs unstable: removefile 85.100.6
- Copyright (c) 2015-23 Apple Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms