Module: Imap::Backup::Utils

Defined in:
lib/imap/backup/utils.rb

Class Method Summary collapse

Class Method Details

.check_permissions(filename, limit) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/imap/backup/utils.rb', line 5

def self.check_permissions(filename, limit)
  actual = mode(filename)
  return nil if actual.nil?

  mask = ~limit & 0o777
  return if (actual & mask).zero?

  message = format(
    "Permissions on '%<filename>s' " \
    "should be 0%<limit>o, not 0%<actual>o",
    filename: filename, limit: limit, actual: actual
  )
  raise message
end

.make_folder(base_path, path, permissions) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/imap/backup/utils.rb', line 27

def self.make_folder(base_path, path, permissions)
  parts = path.split("/")
  return if parts.empty?

  full_path = File.join(base_path, path)
  FileUtils.mkdir_p full_path
  path = base_path
  parts.each do |part|
    path = File.join(path, part)
    FileUtils.chmod permissions, path
  end
end

.mode(filename) ⇒ Object



20
21
22
23
24
25
# File 'lib/imap/backup/utils.rb', line 20

def self.mode(filename)
  return nil if !File.exist?(filename)

  stat = File.stat(filename)
  stat.mode & 0o777
end