Class: Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/types.rb

Overview

Can be used to create directories, to get files within a directory or to see if a directory is writable.

Direct Known Subclasses

SmarbsBackupDir

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, *args) ⇒ Directory

Takes a (path)string as input and creates a directory there, if possible.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/types.rb', line 11

def initialize(directory, *args)
  @dir = Directory.prepare(directory)
  if not @dir[-1, 1].to_s == "/"
    @dir.insert(-1, '/')
  end
  if args.include?(:writable)
    if not Directory.writable?(File.dirname(@dir))
      raise "#{@dir} is not writable!"
    end
  end
  if not File.directory?(@dir)
    if not Directory.writable?(File.dirname(@dir))
      raise "Directory '#{@dir}' is not existing!"
    else
      FileUtils.makedirs(@dir)
      puts "#{@dir} created."
    end
  end
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



33
34
35
# File 'lib/types.rb', line 33

def dir
  @dir
end

Class Method Details

.prepare(input) ⇒ Object

Checks if input is a valid argument and returns normalized string.



36
37
38
39
40
41
42
# File 'lib/types.rb', line 36

def Directory.prepare (input) 
  raise "Internal Error: Argument not a String" if input.class != String
  input.strip!
  raise "No directory specified!" if input.empty?
  input.insert(0, '/') unless input[0, 1].to_s == "/"
  return input
end

.writable?(path) ⇒ Boolean

Checks if the directory (relative to the root directory) is writable, or, if not yet existing, creatable.

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/types.rb', line 46

def Directory.writable?(path)
  Directory.prepare(path)
  if File.exists?(path) and File.directory?(path)
    return File.writable?(path)
  end
  return writable?(File.dirname(path)) 
end

Instance Method Details

#==(other_directory) ⇒ Object

Compares if the two directories are equal.



55
56
57
# File 'lib/types.rb', line 55

def == (other_directory)
  return @dir == other_directory.dir
end

#files(all = true) ⇒ Object

Returns an array of files and folders that the Directory contains, excluding “.” and “..”.

When ‘all’ is ‘false’, then it excludes files with a “~” at the end and all folders.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/types.rb', line 86

def files(all=true)
  if all then
    return Dir.entries(@dir) - ["."] - [".."]
  else
    configfile_names = Array.new
    Dir.foreach(@dir) do |filename|
      path = @dir+"/"+filename
      if File.file?(path) and not path =~ /.*~/ # put every file without ~ at the end into the array
        configfile_names.push(filename)
      end
    end
    return configfile_names
  end
end

#freeObject

Returns how much free space there is (on the whole partition, using “df”) as a Float of bytes



66
67
68
69
# File 'lib/types.rb', line 66

def free
  path = to_s
  `df --block-size=1 #{path}`.split[-3].to_f
end

#space(ownpartition = false) ⇒ Object

Gives back (in bytes) how much space is used for the whole Smarbsbackupdir.

If ownpartition is true, it will use ‘df’ instead of ‘du’ which is much faster.



74
75
76
77
78
79
80
81
# File 'lib/types.rb', line 74

def space(ownpartition = false)
  path = to_s
  if ownpartition
    `df --block-size=1 #{path}`.split[-4].to_f
  else
    `du -bcs #{path}`.split[-2].to_f
  end
end

#to_sObject

Returns the normalized directory path as a string.



60
61
62
# File 'lib/types.rb', line 60

def to_s
  @dir
end