⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 02 - changing the permissions on a file.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
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 = 0001end#---class File  A_R = 0444  A_W = 0222  A_X = 0111end#---open("my_file", "w") {}File.chmod(File::U_R | File::U_W | File::G_R | File::O_R, "my_file")File.chmod(File::A_R | File::U_W, "my_file")File.chmod(0644, "my_file")                   # Bitmap: 110001001 File::U_R | File::U_W | File::G_R | File::O_R # => 420File::A_R | File::U_W                         # => 4200644                                          # => 420File.lstat("my_file").mode & 0777             # => 420#---# Take away the world's read access.new_permission = File.lstat("my_file").mode ^ File::O_RFile.chmod(new_permission, "my_file")File.lstat("my_file").mode & 0777              # => 416    # 0640 octal# Give everyone access to everythingnew_permission = File.lstat("my_file").mode | File::A_R | File::A_W | File::A_XFile.chmod(new_permission, "my_file")File.lstat("my_file").mode & 0777              # => 511    # 0777 octal# Take away the world's write and execute accessnew_permission = File.lstat("my_file").mode ^ (File::O_W | File::O_X)File.chmod(new_permission, "my_file")File.lstat("my_file").mode & 0777              # => 508    # 0774 octal#---class File  def File.fancy_chmod(permission_string, file)    mode = File.lstat(file).mode    permission_string.scan(/[ugoa][+-=][rwx]+/) do |setting|      who = setting[0..0]      setting[2..setting.size].each_byte do |perm|        perm = perm.chr.upcase        mask = eval("File::#{who.upcase}_#{perm}")      (setting[1] == ?+) ? mode |= mask : mode ^= mask      end    end    File.chmod(mode, file)  endend# Give the owning user write accessFile.fancy_chmod("u+w", "my_file")File.lstat("my_file").mode & 0777              # => 508    # 0774 octal# Take away the owning group's execute accessFile.fancy_chmod("g-x", "my_file")File.lstat("my_file").mode & 0777              # => 500    # 0764 octal# Give everyone access to everythingFile.fancy_chmod("a+rwx", "my_file")File.lstat("my_file").mode & 0777              # => 511    # 0777 octal# Give the owning user access to everything. Then take away the# execute access for users who aren't the owning user and aren't in# the owning group.File.fancy_chmod("u+rwxo-x", "my_file")File.lstat("my_file").mode & 0777              # => 510    # 0774 octal#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -