Module: Kanrisuru::Core::File

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/file.rb,
lib/kanrisuru/core/file/types.rb,
lib/kanrisuru/core/file/commands.rb,
lib/kanrisuru/core/file/parsers/wc.rb,
lib/kanrisuru/core/file/commands/rm.rb,
lib/kanrisuru/core/file/commands/wc.rb,
lib/kanrisuru/core/file/commands/copy.rb,
lib/kanrisuru/core/file/commands/link.rb,
lib/kanrisuru/core/file/commands/move.rb,
lib/kanrisuru/core/file/commands/chmod.rb,
lib/kanrisuru/core/file/commands/chown.rb,
lib/kanrisuru/core/file/commands/mkdir.rb,
lib/kanrisuru/core/file/commands/touch.rb,
lib/kanrisuru/core/file/commands/unlink.rb,
lib/kanrisuru/core/file/commands/symlink.rb

Defined Under Namespace

Modules: Parser Classes: FileCount

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#chmod(path, mode, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kanrisuru/core/file/commands/chmod.rb', line 6

def chmod(path, mode, opts = {})
  recursive = opts[:recursive]

  command =
    if mode.instance_of?(String) && (mode.include?(',') || /[=+-]/.match(mode)) && mode.length < 10
      Kanrisuru::Command.new("chmod #{mode} #{path}")
    elsif mode.instance_of?(Kanrisuru::Mode)
      Kanrisuru::Command.new("chmod #{mode.numeric} #{path}")
    else
      mode = Kanrisuru::Mode.new(mode)
      Kanrisuru::Command.new("chmod #{mode.numeric} #{path}")
    end

  command.append_flag('-R', recursive)

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    stat(path).data
  end
end

#chown(path, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kanrisuru/core/file/commands/chown.rb', line 6

def chown(path, opts = {})
  owner = opts[:owner]
  group = opts[:group]

  uid = get_uid(owner).to_i if Kanrisuru::Util.present?(owner)
  gid = get_gid(group).to_i if Kanrisuru::Util.present?(group)

  ## Don't chown a blank owner and group.
  return false if Kanrisuru::Util.blank?(uid) && Kanrisuru::Util.blank?(gid)

  command = Kanrisuru::Command.new('chown')

  arg = ''
  arg = uid.to_s if Kanrisuru::Util.present?(uid)
  arg += ":#{gid}" if Kanrisuru::Util.present?(gid)

  command.append_value(arg)
  command.append_flag('-R', opts[:recursive])

  command << path

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    stat(path).data
  end
end

#copy(source, dest, opts = {}) ⇒ Object



6
7
8
# File 'lib/kanrisuru/core/file/commands/copy.rb', line 6

def copy(source, dest, opts = {})
  cp(source, dest, opts)
end

#cp(source, dest, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kanrisuru/core/file/commands/copy.rb', line 10

def cp(source, dest, opts = {})
  backup = opts[:backup]
  command = Kanrisuru::Command.new('cp')

  command.append_flag('-R', opts[:recursive])
  command.append_flag('-x', opts[:one_file_system])
  command.append_flag('-u', opts[:update])
  command.append_flag('-L', opts[:follow])
  command.append_flag('-n', opts[:no_clobber])
  command.append_flag('--strip-trailing-slashes', opts[:strip_trailing_slashes])

  if backup.instance_of?(TrueClass)
    command.append_flag('-b')
  elsif Kanrisuru::Util.present?(backup)
    raise ArgumentError, 'invalid backup control value' unless backup_control_valid?(backup)

    command << "--backup=#{backup}"
  end

  if opts[:preserve].instance_of?(TrueClass)
    command.append_flag('-p')
  elsif Kanrisuru::Util.present?(opts[:preserve])
    preserve = Kanrisuru::Util.array_join_string(opts[:preserve], ',')
    command << "--preserve=#{preserve}"
  end

  if opts[:no_target_directory]
    command.append_flag('-T')
    command << source
    command << dest
  elsif opts[:target_directory]
    command.append_arg('-t', dest)
    command.append_array(source)
  else
    command.append_array(source)
    command << dest
  end

  execute_shell(command)

  Kanrisuru::Result.new(command)
end


6
7
8
# File 'lib/kanrisuru/core/file/commands/link.rb', line 6

def link(src, dest, opts = {})
  ln(src, dest, opts)
end

#ln(src, dest, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kanrisuru/core/file/commands/link.rb', line 10

def ln(src, dest, opts = {})
  ## Can't hardlink dirs
  return false if dir?(src)

  command = Kanrisuru::Command.new("ln #{src} #{dest}")
  command.append_flag('-f', opts[:force])

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    stat(dest).data
  end
end

#ln_s(src, dest, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kanrisuru/core/file/commands/symlink.rb', line 10

def ln_s(src, dest, opts = {})
  return if src == dest

  ## Use absolute path for source
  real_src = realpath(src).path

  ## Not valid if no real path, not an existing inode, or root
  raise ArgumentError, 'Invalid path' if Kanrisuru::Util.blank?(real_src) || !inode?(real_src) || real_src == '/'

  dest_is_dir = dir?(dest)

  ## Don't symlink inside an already existing symlink
  return if symlink?(dest) && dest_is_dir

  real_dest =
    if dest_is_dir
      ## Use real path for destination
      realpath(dest).path
    else
      ## Use standard path
      dest
    end

  return unless real_dest

  command = Kanrisuru::Command.new("ln -s #{real_src} #{real_dest}")
  command.append_flag('-f', opts[:force])

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    stat(real_dest).data
  end
end

#mkdir(path, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kanrisuru/core/file/commands/mkdir.rb', line 6

def mkdir(path, opts = {})
  owner     = opts[:owner]
  group     = opts[:group]
  recursive = opts[:recursive]

  command = Kanrisuru::Command.new('mkdir')

  command.append_array(path)
  command.append_flag('-p', opts[:silent])

  if Kanrisuru::Util.present?(opts[:mode])
    mode = opts[:mode]

    mode = if mode.instance_of?(Kanrisuru::Mode)
             mode.numeric
           elsif mode.instance_of?(String) && (mode.include?(',') || /[=+-]/.match(mode))
             mode
           else
             Kanrisuru::Mode.new(mode).numeric
           end

    command.append_arg('-m', mode)
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    if Kanrisuru::Util.present?(owner) || Kanrisuru::Util.present?(group)
      chown(path, owner: owner, group: group, recursive: recursive)
    end

    stat(path).data
  end
end

#move(source, dest, opts = {}) ⇒ Object



6
7
8
# File 'lib/kanrisuru/core/file/commands/move.rb', line 6

def move(source, dest, opts = {})
  mv(source, dest, opts)
end

#mv(source, dest, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kanrisuru/core/file/commands/move.rb', line 10

def mv(source, dest, opts = {})
  backup = opts[:backup]
  command = Kanrisuru::Command.new('mv')

  command.append_flag('--strip-trailing-slashes', opts[:strip_trailing_slashes])

  if opts[:force]
    command.append_flag('-f')
  elsif opts[:no_clobber]
    command.append_flag('-n')
  end

  if backup.instance_of?(TrueClass)
    command.append_flag('-b')
  elsif Kanrisuru::Util.present?(backup)
    raise ArgumentError, 'invalid backup control value' unless backup_control_valid?(backup)

    command << "--backup=#{backup}"
  end

  if opts[:no_target_directory]
    command.append_flag('-T')
    command << source
    command << dest
  elsif opts[:target_directory]
    command.append_arg('-t', dest)
    command.append_array(source)
  else
    command.append_array(source)
    command << dest
  end

  execute_shell(command)

  Kanrisuru::Result.new(command)
end

#rm(paths, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kanrisuru/core/file/commands/rm.rb', line 6

def rm(paths, opts = {})
  paths = [paths] if paths.instance_of?(String)

  paths.each do |path|
    raise ArgumentError, "Can't delete root path" if path == '/' || realpath(path).path == '/'
  end

  command = Kanrisuru::Command.new('rm')
  command.append_array(paths)

  command.append_flag('--preserve-root')
  command.append_flag('-f', opts[:force])
  command.append_flag('-r', opts[:recursive])

  execute_shell(command)

  Kanrisuru::Result.new(command)
end

#rmdir(paths, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kanrisuru/core/file/commands/rm.rb', line 25

def rmdir(paths, opts = {})
  paths = [paths] if paths.instance_of?(String)
  paths.each do |path|
    raise ArgumentError, "Can't delete root path" if path == '/' || realpath(path).path == '/'
  end

  command = Kanrisuru::Command.new("rmdir #{paths.join(' ')}")
  command.append_flag('--ignore-fail-on-non-empty', opts[:silent])
  command.append_flag('--parents', opts[:parents])

  execute_shell(command)

  Kanrisuru::Result.new(command)
end


6
7
8
# File 'lib/kanrisuru/core/file/commands/symlink.rb', line 6

def symlink(src, dest, opts = {})
  ln_s(src, dest, opts)
end

#touch(paths, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kanrisuru/core/file/commands/touch.rb', line 6

def touch(paths, opts = {})
  date = opts[:date]

  paths = [paths] if paths.instance_of?(String)
  command = Kanrisuru::Command.new('touch')
  command.append_array(paths)

  command.append_flag('-a', opts[:atime])
  command.append_flag('-m', opts[:mtime])
  command.append_flag('-c', opts[:nofiles])

  if Kanrisuru::Util.present?(date)
    date = Date.parse(date) if date.instance_of?(String)
    command.append_arg('-d', date)
  end

  command.append_arg('-r', opts[:reference])

  execute_shell(command)

  Kanrisuru::Result.new(command) do
    paths.map do |path|
      stat(path).data
    end
  end
end


6
7
8
9
10
# File 'lib/kanrisuru/core/file/commands/unlink.rb', line 6

def unlink(path)
  command = Kanrisuru::Command.new("unlink #{path}")
  execute_shell(command)
  Kanrisuru::Result.new(command)
end

#wc(file) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/kanrisuru/core/file/commands/wc.rb', line 6

def wc(file)
  command = Kanrisuru::Command.new("wc #{file}")
  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Wc.parse(cmd)
  end
end