From 0dc9f33ba10aa9585184d42a350c56fafcf5a288 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Ragnar=20Dahl=C3=A9n?= Date: Wed, 13 Feb 2008 11:15:31 +0100 Subject: [PATCH] Implement File.truncate, passes specs. * Adds truncate, ftruncate (not used yet) to posix.rb Only tested on Mac OS X 10.5.1. --- kernel/core/file.rb | 8 ++++++++ kernel/platform/posix.rb | 4 ++++ spec/ruby/1.8/core/file/truncate_spec.rb | 2 +- 3 files changed, 13 insertions(+), 1 deletions(-) diff --git a/kernel/core/file.rb b/kernel/core/file.rb index 9393001..10586ab 100644 --- a/kernel/core/file.rb +++ b/kernel/core/file.rb @@ -332,6 +332,14 @@ class File < IO unless self.exist?(path) raise Errno::ENOENT, path end + + unless length.respond_to?(:to_int) + raise TypeError, "can't convert #{length.class} into Integer" + end + + n = POSIX.truncate(path, length) + Errno.handle if n == -1 + n end def self.umask(mask = nil) diff --git a/kernel/platform/posix.rb b/kernel/platform/posix.rb index 5dcd5e3..1093f2a 100644 --- a/kernel/platform/posix.rb +++ b/kernel/platform/posix.rb @@ -71,6 +71,10 @@ module Platform::POSIX # writing attach_function 'fwrite', [:string, :int, :int, :pointer], :int attach_function 'ungetc', [:int, :pointer], :int + + # truncating + attach_function 'truncate', [:string, :off_t], :int + attach_function 'ftruncate', [:int, :off_t], :int # formatted strings attach_function 'ffi_sprintf_f', :sprintf_f, [:double, :int, :string], :strptr diff --git a/spec/ruby/1.8/core/file/truncate_spec.rb b/spec/ruby/1.8/core/file/truncate_spec.rb index b8924f3..54d2457 100644 --- a/spec/ruby/1.8/core/file/truncate_spec.rb +++ b/spec/ruby/1.8/core/file/truncate_spec.rb @@ -13,7 +13,7 @@ describe "File.truncate" do @name = nil end - it "truncate the a file" do + it "truncate a file" do File.open(@name, "w") { |f| f.puts "123456789" } platform_is :mswin do File.size(@name).should == 11 -- 1.5.3.8