Skip to content

Instantly share code, notes, and snippets.

@cyndis
Created November 18, 2009 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyndis/97f1bc618087e7dc3860 to your computer and use it in GitHub Desktop.
Save cyndis/97f1bc618087e7dc3860 to your computer and use it in GitHub Desktop.
From c6bc46083634fb7d2accbcbdbf728de8f8b0e089 Mon Sep 17 00:00:00 2001
From: Mikko Perttunen <cyndis@lyseo.edu.ouka.fi>
Date: Mon, 16 Nov 2009 20:57:15 +0200
Subject: [PATCH 1/2] Add File::Stat#sticky?
---
kernel/common/file.rb | 7 +++++++
rakelib/platform.rake | 1 +
2 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/kernel/common/file.rb b/kernel/common/file.rb
index b002955..a6a00e3 100644
--- a/kernel/common/file.rb
+++ b/kernel/common/file.rb
@@ -751,6 +751,8 @@ class File < IO
# Returns true if the named file has the sticky bit set.
def self.sticky?(file_name)
Stat.new(file_name).sticky?
+ rescue Errno::ENOENT
+ return false
end
##
@@ -860,6 +862,7 @@ class File::Stat
S_IFWHT = Rubinius::Config['rbx.platform.file.S_IFWHT']
S_ISUID = Rubinius::Config['rbx.platform.file.S_ISUID']
S_ISGID = Rubinius::Config['rbx.platform.file.S_ISGID']
+ S_ISVTX = Rubinius::Config['rbx.platform.file.S_ISVTX']
POSIX = FFI::Platform::POSIX
@@ -1088,6 +1091,10 @@ class File::Stat
def setuid?
@stat[:st_mode] & S_ISUID != 0
end
+
+ def sticky?
+ @stat[:st_mode] & S_ISVTX != 0
+ end
def size
@stat[:st_size]
diff --git a/rakelib/platform.rake b/rakelib/platform.rake
index c6f219a..64a7292 100644
--- a/rakelib/platform.rake
+++ b/rakelib/platform.rake
@@ -139,6 +139,7 @@ file 'runtime/platform.conf' => deps do |task|
S_IFWHT
S_ISUID
S_ISGID
+ S_ISVTX
]
file_constants.each { |c| cg.const c }
--
1.6.2
From a59af00cdd83fff8b1743e7be90479aae0ffe286 Mon Sep 17 00:00:00 2001
From: Mikko Perttunen <cyndis@lyseo.edu.ouka.fi>
Date: Tue, 17 Nov 2009 13:36:19 +0200
Subject: [PATCH 2/2] Updated specs for File.sticky? and FileTest.sticky?
---
spec/frozen/core/file/sticky_spec.rb | 1 +
spec/frozen/core/filetest/sticky_spec.rb | 1 +
spec/frozen/shared/file/sticky.rb | 26 ++++++++++++++++++++++++++
3 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/spec/frozen/core/file/sticky_spec.rb b/spec/frozen/core/file/sticky_spec.rb
index 7a76fff..9513719 100644
--- a/spec/frozen/core/file/sticky_spec.rb
+++ b/spec/frozen/core/file/sticky_spec.rb
@@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../../shared/file/sticky'
describe "File.sticky?" do
it_behaves_like :file_sticky, :sticky?, File
+ it_behaves_like :file_sticky_missing, :sticky?, File
end
describe "File.sticky?" do
diff --git a/spec/frozen/core/filetest/sticky_spec.rb b/spec/frozen/core/filetest/sticky_spec.rb
index 1027fda..42e31d1 100644
--- a/spec/frozen/core/filetest/sticky_spec.rb
+++ b/spec/frozen/core/filetest/sticky_spec.rb
@@ -3,4 +3,5 @@ require File.dirname(__FILE__) + '/../../shared/file/sticky'
describe "FileTest.sticky?" do
it_behaves_like :file_sticky, :sticky?, FileTest
+ it_behaves_like :file_sticky_missing, :sticky?, FileTest
end
diff --git a/spec/frozen/shared/file/sticky.rb b/spec/frozen/shared/file/sticky.rb
index 474ba18..f7a5577 100644
--- a/spec/frozen/shared/file/sticky.rb
+++ b/spec/frozen/shared/file/sticky.rb
@@ -1,6 +1,32 @@
describe :file_sticky, :shared => true do
+ before :each do
+ @dir = tmp('sticky_dir')
+ Dir.rmdir(@dir) if File.exists?(@dir)
+ end
+
+ after :each do
+ Dir.rmdir(@dir) if File.exists?(@dir)
+ end
+
+ platform_is_not :windows do
+ it "returns true if the named file has the sticky bit, otherwise false" do
+ Dir.mkdir @dir, 1755
+
+ @object.send(@method, @dir).should == true
+ @object.send(@method, '/').should == false
+ end
+ end
+
ruby_version_is "1.9" do
# please add a 1.9 test that accepts a mock_to_path("/path") argument
# it "accepts an object that has a #to_path method"
end
end
+
+describe :file_sticky_missing, :shared => true do
+ platform_is_not :windows do
+ it "returns false if the file dies not exist" do
+ @object.send(@method, 'fake_file').should == false
+ end
+ end
+end
--
1.6.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment