Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented chown and chmod #118

Merged
merged 5 commits into from Apr 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*.sw?
pkg
Gemfile.lock
.rbenv-version
18 changes: 11 additions & 7 deletions 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
Expand Down
5 changes: 4 additions & 1 deletion 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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 31 additions & 9 deletions lib/fakefs/file.rb
Expand Up @@ -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)
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down
38 changes: 36 additions & 2 deletions lib/fakefs/fileutils.rb
Expand Up @@ -118,15 +118,49 @@ 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
list
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={})
Expand Down
120 changes: 112 additions & 8 deletions test/fakefs_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down