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

ruby-changes:39623

From: nobu <ko1@a...>
Date: Thu, 27 Aug 2015 13:01:53 +0900 (JST)
Subject: [ruby-changes:39623] nobu:r51704 (trunk): win32.c: fchmod

nobu	2015-08-27 13:01:37 +0900 (Thu, 27 Aug 2015)

  New Revision: 51704

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

  Log:
    win32.c: fchmod
    
    * win32/win32.c (fchmod): implement by using
      SetFileInformationByHandle.

  Modified files:
    trunk/file.c
    trunk/win32/file.h
    trunk/win32/win32.c
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 51703)
+++ win32/win32.c	(revision 51704)
@@ -7315,6 +7315,47 @@ rb_w32_uchmod(const char *path, int mode https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L7315
 
 /* License: Ruby's */
 int
+fchmod(int fd, int mode)
+{
+    typedef BOOL (WINAPI *set_file_information_by_handle_func)
+	(HANDLE, int, void*, DWORD);
+    static set_file_information_by_handle_func set_file_info =
+	(set_file_information_by_handle_func)-1;
+
+    /* from winbase.h of the mingw-w64 runtime package. */
+    struct {
+	LARGE_INTEGER CreationTime;
+	LARGE_INTEGER LastAccessTime;
+	LARGE_INTEGER LastWriteTime;
+	LARGE_INTEGER ChangeTime;
+	DWORD         FileAttributes;
+    } info = {0, 0, 0};		/* fields with 0 are unchanged */
+    HANDLE h = (HANDLE)_get_osfhandle(fd);
+
+    if (h == INVALID_HANDLE_VALUE) {
+	errno = EBADF;
+	return -1;
+    }
+    if (set_file_info == (set_file_information_by_handle_func)-1) {
+	set_file_info = (set_file_information_by_handle_func)
+	    get_proc_address("kernel32", "SetFileInformationByHandle", NULL);
+    }
+    if (!set_file_info) {
+	errno = ENOSYS;
+	return -1;
+    }
+
+    info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
+    if (!(mode & 0200)) info.FileAttributes |= FILE_ATTRIBUTE_READONLY;
+    if (!set_file_info(h, 0, &info, sizeof(info))) {
+	errno = map_errno(GetLastError());
+	return -1;
+    }
+    return 0;
+}
+
+/* License: Ruby's */
+int
 rb_w32_isatty(int fd)
 {
     DWORD mode;
Index: win32/file.h
===================================================================
--- win32/file.h	(revision 51703)
+++ win32/file.h	(revision 51704)
@@ -39,5 +39,7 @@ int rb_w32_read_reparse_point(const WCHA https://github.com/ruby/ruby/blob/trunk/win32/file.h#L39
 
 int lchown(const char *path, int owner, int group);
 int rb_w32_ulchown(const char *path, int owner, int group);
+int fchmod(int fd, int mode);
+#define HAVE_FCHMOD 0
 
 #endif	/* RUBY_WIN32_FILE_H */
Index: file.c
===================================================================
--- file.c	(revision 51703)
+++ file.c	(revision 51704)
@@ -2357,7 +2357,7 @@ rb_file_chmod(VALUE obj, VALUE vmode) https://github.com/ruby/ruby/blob/trunk/file.c#L2357
 {
     rb_io_t *fptr;
     int mode;
-#ifndef HAVE_FCHMOD
+#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
     VALUE path;
 #endif
 
@@ -2365,9 +2365,15 @@ rb_file_chmod(VALUE obj, VALUE vmode) https://github.com/ruby/ruby/blob/trunk/file.c#L2365
 
     GetOpenFile(obj, fptr);
 #ifdef HAVE_FCHMOD
-    if (fchmod(fptr->fd, mode) == -1)
-	rb_sys_fail_path(fptr->pathv);
-#else
+    if (fchmod(fptr->fd, mode) == -1) {
+	if (HAVE_FCHMOD || errno != ENOSYS)
+	    rb_sys_fail_path(fptr->pathv);
+    }
+    else {
+	if (!HAVE_FCHMOD) return INT2FIX(0);
+    }
+#endif
+#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
     if (NIL_P(fptr->pathv)) return Qnil;
     path = rb_str_encode_ospath(fptr->pathv);
     if (chmod(RSTRING_PTR(path), mode) == -1)

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

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