[前][次][番号順一覧][スレッド一覧]

ruby-changes:15076

From: nobu <ko1@a...>
Date: Tue, 16 Mar 2010 15:20:41 +0900 (JST)
Subject: [ruby-changes:15076] Ruby:r26952 (trunk): * ext/tmpdir: split from lib/tmpdir.rb.

nobu	2010-03-16 15:20:23 +0900 (Tue, 16 Mar 2010)

  New Revision: 26952

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26952

  Log:
    * ext/tmpdir: split from lib/tmpdir.rb.
    
    * win32/win32.c (rb_w32_system_tmpdir): extracted from init_env.

  Added directories:
    trunk/ext/tmpdir/
  Added files:
    trunk/ext/tmpdir/extconf.rb
    trunk/ext/tmpdir/tmpdir.c
  Modified files:
    trunk/ChangeLog
    trunk/lib/tmpdir.rb
    trunk/win32/win32.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26951)
+++ ChangeLog	(revision 26952)
@@ -1,5 +1,9 @@
-Tue Mar 16 14:47:55 2010  Nobuyoshi Nakada  <nobu@r...>
+Tue Mar 16 15:20:13 2010  Nobuyoshi Nakada  <nobu@r...>
 
+	* ext/tmpdir: split from lib/tmpdir.rb.
+
+	* win32/win32.c (rb_w32_system_tmpdir): extracted from init_env.
+
 	* win32/win32.c (rb_w32_getcwd): remvoed duplicated code.
 
 Tue Mar 16 14:06:13 2010  Nobuyoshi Nakada  <nobu@r...>
Index: lib/tmpdir.rb
===================================================================
--- lib/tmpdir.rb	(revision 26951)
+++ lib/tmpdir.rb	(revision 26952)
@@ -5,37 +5,15 @@
 #
 
 require 'fileutils'
+begin
+  require 'tmpdir.so'
+rescue LoadError
+end
 
 class Dir
 
-  @@systmpdir = '/tmp'
+  @@systmpdir ||= '/tmp'
 
-  if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM and
-      begin
-        require 'Win32API'
-        true
-      rescue LoadError
-      end
-    CSIDL_LOCAL_APPDATA = 0x001c
-    max_pathlen = 260
-    windir = "\0"*(max_pathlen+1)
-    begin
-      getdir = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L')
-      raise RuntimeError if getdir.call(0, CSIDL_LOCAL_APPDATA, 0, 0, windir) != 0
-      windir.rstrip!
-    rescue RuntimeError
-      begin
-        getdir = Win32API.new('kernel32', 'GetSystemWindowsDirectory', 'PL', 'L')
-      rescue RuntimeError
-        getdir = Win32API.new('kernel32', 'GetWindowsDirectory', 'PL', 'L')
-      end
-      windir[getdir.call(windir, windir.size)..-1] = ""
-    end
-    windir.force_encoding(Dir.pwd.encoding)
-    temp = File.expand_path('temp', windir.untaint)
-    @@systmpdir = temp if File.directory?(temp) and File.writable?(temp)
-  end
-
   ##
   # Returns the operating system's temporary file path.
 
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 26951)
+++ win32/win32.c	(revision 26952)
@@ -430,6 +430,22 @@
 
 #define numberof(array) (sizeof(array) / sizeof(*array))
 
+UINT
+rb_w32_system_tmpdir(WCHAR *path, UINT len)
+{
+    static const WCHAR temp[] = L"temp";
+    WCHAR *p;
+
+    if (!get_special_folder(CSIDL_LOCAL_APPDATA, path)) {
+	if (get_system_directory(path, len)) return 0;
+    }
+    p = translate_wchar(path, L'\\', L'/');
+    if (*(p - 1) != L'/') *p++ = L'/';
+    if (p - path + numberof(temp) >= len) return 0;
+    memcpy(p, temp, sizeof(temp));
+    return p - path + numberof(temp) - 1;
+}
+
 static void
 init_env(void)
 {
@@ -484,15 +500,8 @@
     if (!GetEnvironmentVariableW(TMPDIR, env, numberof(env)) &&
 	!GetEnvironmentVariableW(L"TMP", env, numberof(env)) &&
 	!GetEnvironmentVariableW(L"TEMP", env, numberof(env)) &&
-	(get_special_folder(CSIDL_LOCAL_APPDATA, env) ||
-	 get_system_directory(env, numberof(env)))) {
-	static const WCHAR temp[] = L"temp";
-	WCHAR *p = translate_wchar(env, L'\\', L'/');
-	if (*(p - 1) != L'/') *p++ = L'/';
-	if (p - env + numberof(temp) < numberof(env)) {
-	    memcpy(p, temp, sizeof(temp));
-	    set_env_val(TMPDIR);
-	}
+	rb_w32_system_tmpdir(env, numberof(env))) {
+	set_env_val(TMPDIR);
     }
 
 #undef env
Index: ext/tmpdir/tmpdir.c
===================================================================
--- ext/tmpdir/tmpdir.c	(revision 0)
+++ ext/tmpdir/tmpdir.c	(revision 26952)
@@ -0,0 +1,32 @@
+#include <ruby/ruby.h>
+#include <ruby/encoding.h>
+
+#define numberof(array) (sizeof(array) / sizeof(*array))
+
+#ifdef HAVE_RB_W32_SYSTEM_TMPDIR
+UINT rb_w32_system_tmpdir(WCHAR *path, UINT len);
+VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc);
+#endif
+
+static VALUE
+system_tmpdir(void)
+{
+#ifdef HAVE_RB_W32_SYSTEM_TMPDIR
+    WCHAR path[_MAX_PATH];
+    UINT len = rb_w32_system_tmpdir(path, numberof(path));
+    if (!len) return Qnil;
+    return rb_w32_conv_from_wchar(path, rb_filesystem_encoding());
+#else
+    return rb_filesystem_str_new_cstr("/tmp");
+#endif
+}
+
+/*
+ * sets Dir.@@systmpdir.
+ */
+void
+Init_tmpdir(void)
+{
+    rb_cvar_set(rb_cDir, rb_intern_const("@@systmpdir"),
+		rb_obj_freeze(system_tmpdir()));
+}

Property changes on: ext/tmpdir/tmpdir.c
___________________________________________________________________
Name: svn:keywords
   + Author Id Revision
Name: svn:eol-style
   + LF

Index: ext/tmpdir/extconf.rb
===================================================================
--- ext/tmpdir/extconf.rb	(revision 0)
+++ ext/tmpdir/extconf.rb	(revision 26952)
@@ -0,0 +1,6 @@
+case
+when have_func("rb_w32_system_tmpdir")
+  ok = true # win32
+else
+end
+create_makefile("tmpdir") if ok

Property changes on: ext/tmpdir/extconf.rb
___________________________________________________________________
Name: svn:keywords
   + Author Id Revision
Name: svn:eol-style
   + LF


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]