Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

implemented chown and chmod #118

Merged
merged 5 commits into from

2 participants

@mmriis

Implemented these.

Note: Dir should have empty "" content since File.new("directory") is a valid construct.

@jfirebaugh jfirebaugh referenced this pull request
Merged

Omnibus pull request #124

@rue rue merged commit 30afd1e into defunkt:master
@jfirebaugh jfirebaugh referenced this pull request
Closed

File#chmod & File#chown #117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 25, 2012
  1. @mmriis

    implemented chown and chmod

    mmriis authored
Commits on Jan 30, 2012
  1. @mmriis
  2. @mmriis

    fix for one file only

    mmriis authored
  3. @mmriis
  4. @mmriis

    chown fixed

    mmriis authored
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -1,3 +1,4 @@
*.sw?
pkg
Gemfile.lock
+.rbenv-version
View
18 lib/fakefs/fake/dir.rb
@@ -1,14 +1,18 @@
module FakeFS
class FakeDir < Hash
- attr_accessor :name, :parent
- attr_reader :ctime, :mtime, :atime
+ attr_accessor :name, :parent, :mode, :uid, :gid
+ attr_reader :ctime, :mtime, :atime, :content
def initialize(name = nil, parent = nil)
- @name = name
- @parent = parent
- @ctime = Time.now
- @mtime = @ctime
- @atime = @ctime
+ @name = name
+ @parent = parent
+ @ctime = Time.now
+ @mtime = @ctime
+ @atime = @ctime
+ @mode = 0100000 + (0777 - File.umask)
+ @uid = Process.uid
+ @gid = Process.gid
+ @content = ""
end
def entry
View
5 lib/fakefs/fake/file.rb
@@ -1,6 +1,6 @@
module FakeFS
class FakeFile
- attr_accessor :name, :parent, :content, :mtime, :atime
+ attr_accessor :name, :parent, :content, :mtime, :atime, :mode, :uid, :gid
attr_reader :ctime
class Inode
@@ -35,6 +35,9 @@ def initialize(name = nil, parent = nil)
@ctime = Time.now
@mtime = @ctime
@atime = @ctime
+ @mode = 0100000 + (0666 - File.umask)
+ @uid = Process.uid
+ @gid = Process.gid
end
attr_accessor :inode
View
40 lib/fakefs/file.rb
@@ -233,9 +233,25 @@ def self.lstat(file)
def self.split(path)
return RealFile.split(path)
end
+
+ def self.chmod(mode_int, filename)
+ FileSystem.find(filename).mode = 0100000 + mode_int
+ end
+ def self.chown(owner_int, group_int, filename)
+ file = FileSystem.find(filename)
+ owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ file.uid = owner_int
+ file.gid = group_int
+ end
+
+ def self.umask
+ RealFile.umask
+ end
+
class Stat
- attr_reader :ctime, :mtime, :atime
+ attr_reader :ctime, :mtime, :atime, :mode, :uid, :gid
def initialize(file, __lstat = false)
if !File.exists?(file)
@@ -248,6 +264,9 @@ def initialize(file, __lstat = false)
@ctime = @fake_file.ctime
@mtime = @fake_file.mtime
@atime = @fake_file.atime
+ @mode = @fake_file.mode
+ @uid = @fake_file.uid
+ @gid = @fake_file.gid
end
def symlink?
@@ -340,14 +359,6 @@ def atime
self.class.atime(@path)
end
- def chmod(mode_int)
- raise NotImplementedError
- end
-
- def chown(owner_int, group_int)
- raise NotImplementedError
- end
-
def ctime
self.class.ctime(@path)
end
@@ -359,6 +370,17 @@ def flock(locking_constant)
def mtime
self.class.mtime(@path)
end
+
+ def chmod(mode_int)
+ @file.mode = 0100000 + mode_int
+ end
+
+ def chown(owner_int, group_int)
+ owner_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ group_int.is_a?(Fixnum) or raise TypeError, "can't convert String into Integer"
+ @file.uid = owner_int
+ @file.gid = group_int
+ end
if RUBY_VERSION >= "1.9"
def binmode?
View
38 lib/fakefs/fileutils.rb
@@ -118,7 +118,11 @@ def mv(src, dest, options={})
def chown(user, group, list, options={})
list = Array(list)
list.each do |f|
- unless File.exists?(f)
+ if File.exists?(f)
+ uid = (user.to_s.match(/[0-9]+/) ? user.to_i : Etc.getpwnam(user).uid)
+ gid = (group.to_s.match(/[0-9]+/) ? group.to_i : Etc.getgrnam(group).gid)
+ File.chown(uid, gid, f)
+ else
raise Errno::ENOENT, f
end
end
@@ -126,7 +130,37 @@ def chown(user, group, list, options={})
end
def chown_R(user, group, list, options={})
- chown(user, group, list, options={})
+ list = Array(list)
+ list.each do |file|
+ chown(user, group, file)
+ [FileSystem.find("#{file}/**/**")].flatten.each do |f|
+ chown(user, group, f.to_s)
+ end
+ end
+ list
+ end
+
+ def chmod(mode, list, options={})
+ list = Array(list)
+ list.each do |f|
+ if File.exists?(f)
+ File.chmod(mode, f)
+ else
+ raise Errno::ENOENT, f
+ end
+ end
+ list
+ end
+
+ def chmod_R(mode, list, options={})
+ list = Array(list)
+ list.each do |file|
+ chmod(mode, file)
+ [FileSystem.find("#{file}/**/**")].flatten.each do |f|
+ chmod(mode, f.to_s)
+ end
+ end
+ list
end
def touch(list, options={})
View
120 test/fakefs_test.rb
@@ -690,30 +690,83 @@ def test_can_chown_files
good = 'file.txt'
bad = 'nofile.txt'
File.open(good,'w') { |f| f.write "foo" }
+ username = Etc.getpwuid(Process.uid).name
+ groupname = Etc.getgrgid(Process.gid).name
- out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
+ out = FileUtils.chown(1337, 1338, good, :verbose => true)
assert_equal [good], out
+ assert_equal File.stat(good).uid, 1337
+ assert_equal File.stat(good).gid, 1338
assert_raises(Errno::ENOENT) do
- FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
+ FileUtils.chown(username, groupname, bad, :verbose => true)
end
- assert_equal [good], FileUtils.chown('noone', 'nogroup', good)
+ assert_equal [good], FileUtils.chown(username, groupname, good)
+ assert_equal File.stat(good).uid, Process.uid
+ assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
- FileUtils.chown('noone', 'nogroup', bad)
+ FileUtils.chown(username, groupname, bad)
end
- assert_equal [good], FileUtils.chown('noone', 'nogroup', [good])
+ assert_equal [good], FileUtils.chown(username, groupname, [good])
+ assert_equal File.stat(good).uid, Process.uid
+ assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
- FileUtils.chown('noone', 'nogroup', [good, bad])
+ FileUtils.chown(username, groupname, [good, bad])
end
end
def test_can_chown_R_files
+ username = Etc.getpwuid(Process.uid).name
+ groupname = Etc.getgrgid(Process.gid).name
FileUtils.mkdir_p '/path/'
File.open('/path/foo', 'w') { |f| f.write 'foo' }
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
- resp = FileUtils.chown_R('no', 'no', '/path')
- assert_equal ['/path'], resp
+ assert_equal ['/path'], FileUtils.chown_R(username, groupname, '/path')
+ %w(/path /path/foo /path/foobar).each do |f|
+ assert_equal File.stat(f).uid, Process.uid
+ assert_equal File.stat(f).gid, Process.gid
+ end
+ end
+
+ def test_can_chmod_files
+ good = "file.txt"
+ bad = "nofile.txt"
+ FileUtils.touch(good)
+
+ assert_equal [good], FileUtils.chmod(0600, good, :verbose => true)
+ assert_equal File.stat(good).mode, 0100600
+ assert_raises(Errno::ENOENT) do
+ FileUtils.chmod(0600, bad)
+ end
+
+ assert_equal [good], FileUtils.chmod(0666, good)
+ assert_equal File.stat(good).mode, 0100666
+ assert_raises(Errno::ENOENT) do
+ FileUtils.chmod(0666, bad)
+ end
+
+ assert_equal [good], FileUtils.chmod(0644, [good])
+ assert_equal File.stat(good).mode, 0100644
+ assert_raises(Errno::ENOENT) do
+ FileUtils.chmod(0644, bad)
+ end
+ end
+
+ def test_can_chmod_R_files
+ FileUtils.mkdir_p "/path/sub"
+ FileUtils.touch "/path/file1"
+ FileUtils.touch "/path/sub/file2"
+
+ assert_equal ["/path"], FileUtils.chmod_R(0600, "/path")
+ assert_equal File.stat("/path").mode, 0100600
+ assert_equal File.stat("/path/file1").mode, 0100600
+ assert_equal File.stat("/path/sub").mode, 0100600
+ assert_equal File.stat("/path/sub/file2").mode, 0100600
+
+ FileUtils.mkdir_p "/path2"
+ FileUtils.touch "/path2/hej"
+ assert_equal ["/path2"], FileUtils.chmod_R(0600, "/path2")
end
def test_dir_globs_paths
@@ -1824,6 +1877,57 @@ def test_split
assert_equal path, "/this/is/what/we"
assert_equal filename, "expect.txt"
end
+
+ #########################
+ def test_file_default_mode
+ FileUtils.touch "foo"
+ assert_equal File.stat("foo").mode, (0100000 + 0666 - File.umask)
+ end
+
+ def test_dir_default_mode
+ Dir.mkdir "bar"
+ assert_equal File.stat("bar").mode, (0100000 + 0777 - File.umask)
+ end
+
+ def test_file_default_uid_and_gid
+ FileUtils.touch "foo"
+ assert_equal File.stat("foo").uid, Process.uid
+ assert_equal File.stat("foo").gid, Process.gid
+ end
+
+ def test_file_chmod_of_file
+ FileUtils.touch "foo"
+ File.chmod 0600, "foo"
+ assert_equal File.stat("foo").mode, 0100600
+ File.new("foo").chmod 0644
+ assert_equal File.stat("foo").mode, 0100644
+ end
+
+ def test_file_chmod_of_dir
+ Dir.mkdir "bar"
+ File.chmod 0777, "bar"
+ assert_equal File.stat("bar").mode, 0100777
+ File.new("bar").chmod 01700
+ assert_equal File.stat("bar").mode, 0101700
+ end
+
+ def test_file_chown_of_file
+ FileUtils.touch "foo"
+ File.chown 1337, 1338, "foo"
+ assert_equal File.stat("foo").uid, 1337
+ assert_equal File.stat("foo").gid, 1338
+ end
+
+ def test_file_chown_of_dir
+ Dir.mkdir "bar"
+ File.chown 1337, 1338, "bar"
+ assert_equal File.stat("bar").uid, 1337
+ assert_equal File.stat("bar").gid, 1338
+ end
+
+ def test_file_umask
+ assert_equal File.umask, RealFile.umask
+ end
def here(fname)
Something went wrong with that request. Please try again.