— Steve Jobs in WIRED magazine, February 1996
I'm Mustafa Paksoy and I write code for a living. I post assorted items of interest here. You can contact me via email.
I also take pictures, dabble in Twitter and occasionally put projects on GitHub.
Unix file permissions fall into three basic categories, read, write and execute. Which one do you think controls file deletion? It’s probably write, right? Well, let’s test it:
$ touch foo; chmod -w foo
$ ls -l foo
-r-------- 1 user group 0 2011-01-24 12:16 foo
$ rm foo
$ ls foo
ls: cannot access foo: No such file or directory
That’s weird, it looks like I successfully deleted that file. Maybe it’s governed by user/group ownership.
$ touch foo; sudo chown root:root foo
$ ls -l foo
-rw------- 1 root root 0 2011-01-24 12:18 foo
$ rm foo
$ ls foo
ls: cannot access foo: No such file or directory
I just deleted a file owned by root. WTF?
It turns out, Unix file deletion (and creation) is controlled by write access to the parent directory. Let’s demonstrate quickly:
$ mkdir test; touch test/foo
$ chmod -w test
$ rm test/foo
rm: test/foo: Permission denied
You see, each Unix file has a dedicated datastructure that stores metadata called an inode. While inodes store permissions, access/modification timestamps and other useful info, they do not track file location or name. Instead, directories map file names to inodes. The name of an inode is derived from its location. When we delete a file with the rm command, we are merely removing a directory entry. Hence, we only need write permissions to the directory in question.
You can even link to an inode from multiple directory locations with the ln command. These references are called hard links and are counted by the inode. The inode is reclaimed and the disk storage freed only when the reference count reaches zero. So not all rm operations will result in the underlying file being destroyed, which is yet another reason for file deletion being controlled by the parent directory.
Apparently the iPhone also has a universal orientation lock like the iPad. Here’s how to use it:
- Doube-tap the home button to view the recent apps selector.
- Swipe right on the selector to reveal the iPod controls and, lo and behold, the orientation lock.
- Lie in bed all morning browsing the web.
(Step 3 is optional.)
The following Bash snippet will repeat a command with the given delay until it succeeds.
while [ "$?" -gt "0" ] ; do
sleep 120 # delay in seconds between retries
# insert command to retry here
done
Unix processes return integers when they exit. These values can be read via $? in Bash. As a matter of convention, a process that completes without errors will return 0, so by checking for exit codes greater than zero we can detect errors and retry.
I discovered this gem in the rm man page on OS X the other day:
-P Overwrite regular files before deleting them. Files are
overwritten three times, first with the byte pattern 0xff,
then 0x00, and then 0xff again, before they are deleted.
So there you go rm -P will securely delete a file from your disk with minimum hassle (i.e. without having to through the Thrash folder). This might be one of those goodies OS X inherited from BSD. On Linux you have to use something like shred to achieve the same thing.
Addendum: santry on the HN Thread points out the same thing can done via the srm command. Several people also noted that secure delete interacts unpredictably with modern file systems. jrockway points out that full disk encryption should be used in place of secure delete for this reason.
— Douglas Adams, presumably talking about how distributed systems are more reliable since fault tolerance is baked into the architecture
-
-
-
Using Dropbox as a Git repository
So last month I wrote a bit about setting up your own personal Git repositories on a Linux box, and how to use...
-
Me: “Alright, I want you to go ahead and drag that file to your desktop.”
Client: “Pff. I don’t have a desktop, I have a laptop!” -
“If a worker labors under the threat of force or of need, or a student produces on demand, we may admire what they do, but despise what they are.”
-
How To Tell if Different Kinds Of Fruit Are Ripe
It seems like everyone these days has some kind of quick tip for gauging the ripeness of fruit.
Still, billions of shoppers every day find themselves in the produce aisle, ears pressed to cassava melons, grinning like idiots.
-
Client: “I need your agency to develop a robust morale-boosting program for the top 100 ‘leaders’ in our region during our annual team building...
-
EZ-Laminator450 Users Manual: Frequently Asked Questions
FAQ
Is this the same type of laminator they have at high schools?
Yes! Your I.D. card...