You want to control access to a file by modifying its Unix permissions. For instance, you want to make it so that everyone on your system can read a file, but only you can write to it.
Unless you've got a lot of Unix experience, it's hard to
remember the numeric codes for the nine Unix permission bits. Probably
the first thing you should do is define constants for them. Here's one
constant for every one of the permission bits. If these names are too
concise for you, you can name them
GROUP_WRITE, OTHER_ EXECUTE, and so on.
class File U_R = 0400 U_W = 0200 U_X = 0100 G_R = 0040 G_W = 0020 G_X = 0010 O_R = 0004 O_W = 0002 O_X = 0001 end
You might also want to define these three special constants, which you can use to set the user, group, and world permissions all at once:
class File A_R = 0444 A_W = 0222 A_X = 0111 end
Now you're ready to actually change a file's permissions. Every
Unix file has a permission bitmap, or mode, which
you can change (assuming you have the permissions!) by calling
File.chmod. You can manipulate the
constants defined above to get a new mode, then pass it in along with
the filename to
The following three
calls are equivalent: for the file
my_file, they give readwrite access to to
the user who owns the file, and restrict everyone else to read-only
access. This is equivalent to the permission bitmap 11001001, the
octal number 0644, or the decimal number 420.
open("my_file", "w") ...