Our great sponsors
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
-
rmtrash
Put files (and directories) in trash using the `trash-put` command in a way that is, otherwise as `trash-put` itself, compatible to GNUs `rm` and `rmdir`
Suuuuuuuuuuure. https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/issues/123
POSIX compatibility/compliance. That being said, there are various trash programs available, namely trash.
# Make a directory structure of foo/bar/baz, creating children as needed mkdir -p foo/bar/baz # Create 10 empty files named 00 - 09 in each directory for file in {00..09}; do touch {foo,foo/bar,foo/bar/baz}/$file; done # Recursively delete ./foo, but display each system call as it's happening strace rm -r ./foo # NOTE: skipping parts irrelevant to the discussion # Return information about ./foo, such as inode number, owner, etc. # https://linux.die.net/man/2/fstatat newfstatat(AT_FDCWD, "./foo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, AT_SYMLINK_NOFOLLOW) = 0 # Open ./foo, and return its file descriptor (3) # https://linux.die.net/man/2/openat openat(AT_FDCWD, "./foo", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW|O_DIRECTORY) = 3 # Read the file fd3 points to - ./foo and return the number of bytes read # The comment is part of the struct, containing a zero-padding byte and a char # indicating the file's type. 10 files + 2 sub-directories + 1 padding byte == 13. # (in Linux, everything is a file, even directories) # https://linux.die.net/man/2/getdents64 getdents64(3, 0x5598800019a0 /* 13 entries */, 32768) = 312 # getdents is called until it returns 0, indicating the end of the directory getdents64(3, 0x55f7b0f879a0 /* 0 entries */, 32768) = 0 # Close the file # https://linux.die.net/man/2/close close(3) = 0 # Open a file - in this case, 03 newfstatat(4, "03", {st_mode=S_IFREG|0644, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 # Check if the user can access the file pointed to by fd4 (03) in the mode given # W_OK - write access # Interestingly, access, of which faccessat is a superset, notes that this # syscall should be avoided due to a potential security hole that could # be exploited (https://linux.die.net/man/2/access) # https://linux.die.net/man/2/faccessat faccessat(4, "03", W_OK) = 0 # Unlink AKA delete the file # https://linux.die.net/man/2/unlinkat unlinkat(4, "03", 0) = 0 # Repeat for all files*, recursing down the tree's children # (i.e. foo/ then bar/ then baz/) # Oddly, it leaves 3 files in ./foo until the end # I couldn't find why in https://github.com/coreutils/coreutils/blob/master/src/rm.c # or https://github.com/coreutils/coreutils/blob/master/src/remove.c but # but maybe someone better with C can figure it out # Right before the last child directory's contents are checked with getdents64, # this call is made # Return information about the filesystem pointed to by fd3 - which is odd, # because fd3 was closed a few lines earlier, yet fstat doesn't return EBADF, # so clearly fd3 is pointing to _something_ # Also interesting is that the returned f_type is ext2 (this fs is ext4); after # some research, this is because its magic number (0xEF53) is shared by ext2-ext4 # presumably to indicate backwards-compatibility # https://linux.die.net/man/2/fstatfs fstatfs(3, {f_type=EXT2_SUPER_MAGIC, f_bsize=4096, f_blocks=8228079, f_bfree=2037953, f_bavail=1677393, f_files=2097152, f_ffree=1596984, f_fsid={val=[438474233, 1766772608]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_RELATIME}) = 0 # Get information for, then check permission for, then unlink baz, then close newfstatat(5, "baz", {st_mode=S_IFDIR|0755, st_size=4096, ...}, AT_SYMLINK_NOFOLLOW) = 0 faccessat(5, "baz", W_OK) = 0 unlinkat(5, "baz", AT_REMOVEDIR) = 0 close(5)
Related posts
- btrfs-undelete: A simple script for recovering just-deleted files, directories, and wildcards. This script saved my ass just now. (GPLv2)
- trash-d: A near drop-in replacement for rm that uses the trash bin
- I deleted my /usr directory
- Can't believe the community?
- Show HN: A CLI tool that enables you to remove files easily and safely