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

ruby-changes:33960

From: naruse <ko1@a...>
Date: Thu, 22 May 2014 19:39:34 +0900 (JST)
Subject: [ruby-changes:33960] naruse:r46041 (trunk): add Bug::File::Fs.fsname for tests

naruse	2014-05-22 19:39:29 +0900 (Thu, 22 May 2014)

  New Revision: 46041

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

  Log:
    add Bug::File::Fs.fsname for tests
    
    get filesystem type name for given path

  Added files:
    trunk/ext/-test-/file/fs.c
Index: ext/-test-/file/fs.c
===================================================================
--- ext/-test-/file/fs.c	(revision 0)
+++ ext/-test-/file/fs.c	(revision 46041)
@@ -0,0 +1,60 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/file/fs.c#L1
+#include "ruby/ruby.h"
+#include "ruby/io.h"
+
+#ifdef __linux__
+# define HAVE_GETMNTENT
+#endif
+
+#ifdef HAVE_GETMNTENT
+# include <stdio.h>
+# include <mntent.h>
+#endif
+
+VALUE
+get_fsname(VALUE self, VALUE str)
+{
+#ifdef HAVE_GETMNTENT
+    const char *path;
+    struct mntent mntbuf;
+    static const int buflen = 4096;
+    char *buf = alloca(buflen);
+    int len = 0;
+    FILE *fp;
+#define FSNAME_LEN 100
+    char name[FSNAME_LEN] = "";
+
+    FilePathValue(str);
+    path = RSTRING_PTR(str);
+    fp = setmntent("/etc/mtab", "r"); 
+    if (!fp) rb_sys_fail("setmntent(/etb/mtab)");;
+
+    while (getmntent_r(fp, &mntbuf, buf, buflen)) {
+	int i;
+	char *mnt_dir = mntbuf.mnt_dir;
+	for (i=0; mnt_dir[i]; i++) {
+	    if (mnt_dir[i] != path[i]) {
+		goto next_entry;
+	    }
+	}
+	if (i >= len) {
+	    len = i;
+	    strlcpy(name, mntbuf.mnt_type, FSNAME_LEN);
+	}
+next_entry:
+	;
+    }
+    endmntent(fp);
+
+    if (!len) rb_sys_fail("no matching entry");;
+    return rb_str_new_cstr(name);
+#else
+    return Qnil;
+#endif
+}
+
+void
+Init_fs(VALUE module)
+{
+    VALUE fs = rb_define_module_under(module, "Fs");
+    rb_define_module_function(fs, "fsname", get_fsname, 1);
+}

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

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