Skip to content

Commit

Permalink
Adding Dir.chroot (ref #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonc committed Jun 19, 2014
1 parent ebb20c0 commit 1dab496
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/memfs/dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ def self.chdir(path, &block)
return 0
end

def self.chroot(path)
unless Process.uid.zero?
raise Errno::EPERM, path
end

dir = fs.find_directory!(path)
dir.name = '/'
fs.root = dir
0
end

def self.entries(dirname, opts = {})
fs.entries(dirname)
end
Expand Down
38 changes: 38 additions & 0 deletions spec/memfs/dir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ module MemFs
end
end

describe '.chroot' do
before { Process.stub(uid: 0) }

it "changes the process's idea of the file system root" do

subject.mkdir('/test/subdir')
subject.chroot('/test')

expect(File.exist?('/subdir')).to be true
end

it 'returns zero' do
expect(subject.chroot('/test')).to eq 0
end

context "when the given path is a file" do
before { fs.touch('/test/test-file') }

it 'raises an exception' do
expect{ subject.chroot('/test/test-file') }.to raise_error(Errno::ENOTDIR)
end
end

context "when the given path doesn't exist" do
it 'raises an exception' do
expect{ subject.chroot('/no-dir') }.to raise_error(Errno::ENOENT)
end
end

context 'when the user is not root' do
before { Process.stub(uid: 42) }

it 'raises an exception' do
expect{ subject.chroot('/no-dir') }.to raise_error(Errno::EPERM)
end
end
end

describe ".delete" do
it_behaves_like 'aliased method', :delete, :rmdir
end
Expand Down

0 comments on commit 1dab496

Please sign in to comment.