mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
fs.c: use statfs/statvfs
* ext/-test-/file/fs.c (get_fsname): return filesystem name by statfs/statvfs. [ruby-core:68624] [Bug #10998] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50071 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8ea6b7a137
commit
acae106c7c
3 changed files with 66 additions and 38 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Tue Mar 24 17:30:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* ext/-test-/file/fs.c (get_fsname): return filesystem name by
|
||||||
|
statfs/statvfs. [ruby-core:68624] [Bug #10998]
|
||||||
|
|
||||||
Tue Mar 24 16:46:02 2015 NAKAMURA Usaku <usa@ruby-lang.org>
|
Tue Mar 24 16:46:02 2015 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
* tool/redmine-backporter.rb: now doesn't required spaces just after
|
* tool/redmine-backporter.rb: now doesn't required spaces just after
|
||||||
|
|
|
@ -1,4 +1,18 @@
|
||||||
$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
|
$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
|
||||||
|
|
||||||
|
headers = %w[sys/param.h sys/mount.h sys/vfs.h].select {|h| have_header(h)}
|
||||||
|
if have_type("struct statfs", headers)
|
||||||
|
have_struct_member("struct statfs", "f_fstypename", headers)
|
||||||
|
have_struct_member("struct statfs", "f_type", headers)
|
||||||
|
end
|
||||||
|
|
||||||
|
headers = %w[sys/statvfs.h]
|
||||||
|
if have_type("struct statvfs", headers)
|
||||||
|
have_struct_member("struct statvfs", "f_fstypename", headers)
|
||||||
|
have_struct_member("struct statvfs", "f_basetype", headers)
|
||||||
|
have_struct_member("struct statvfs", "f_type", headers)
|
||||||
|
end
|
||||||
|
|
||||||
$srcs = Dir[File.join($srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
|
$srcs = Dir[File.join($srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
|
||||||
inits = $srcs.map {|s| File.basename(s, ".*")}
|
inits = $srcs.map {|s| File.basename(s, ".*")}
|
||||||
inits.delete("init")
|
inits.delete("init")
|
||||||
|
|
|
@ -1,55 +1,64 @@
|
||||||
#include "ruby/ruby.h"
|
#include "ruby/ruby.h"
|
||||||
#include "ruby/io.h"
|
#include "ruby/io.h"
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef HAVE_SYS_MOUNT_H
|
||||||
# define HAVE_GETMNTENT
|
#include <sys/mount.h>
|
||||||
|
#endif
|
||||||
|
#ifdef HAVE_SYS_VFS_H
|
||||||
|
#include <sys/vfs.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_GETMNTENT
|
#if defined HAVE_STRUCT_STATFS_F_FSTYPENAME
|
||||||
# include <stdio.h>
|
typedef struct statfs statfs_t;
|
||||||
# include <mntent.h>
|
# define STATFS(f, s) statfs((f), (s))
|
||||||
|
# define HAVE_STRUCT_STATFS_T_F_FSTYPENAME 1
|
||||||
|
#elif defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME) /* NetBSD */
|
||||||
|
typedef struct statvfs statfs_t;
|
||||||
|
# define STATFS(f, s) statvfs((f), (s))
|
||||||
|
# define HAVE_STRUCT_STATFS_T_F_FSTYPENAME 1
|
||||||
|
#elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE) /* AIX, HP-UX, Solaris */
|
||||||
|
typedef struct statvfs statfs_t;
|
||||||
|
# define STATFS(f, s) statvfs((f), (s))
|
||||||
|
# define HAVE_STRUCT_STATFS_T_F_FSTYPENAME 1
|
||||||
|
# define f_fstypename f_basetype
|
||||||
|
#elif defined HAVE_STRUCT_STATFS_F_TYPE
|
||||||
|
typedef struct statfs statfs_t;
|
||||||
|
# define STATFS(f, s) statfs((f), (s))
|
||||||
|
#elif defined HAVE_STRUCT_STATVFS_F_TYPE
|
||||||
|
typedef struct statvfs statfs_t;
|
||||||
|
# define STATFS(f, s) statvfs((f), (s))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
get_fsname(VALUE self, VALUE str)
|
get_fsname(VALUE self, VALUE str)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_GETMNTENT
|
#ifdef STATFS
|
||||||
const char *path;
|
statfs_t st;
|
||||||
struct mntent mntbuf;
|
# define CSTR(s) rb_str_new_cstr(s)
|
||||||
static const int buflen = 4096;
|
|
||||||
char *buf = alloca(buflen);
|
|
||||||
int len = 0;
|
|
||||||
FILE *fp;
|
|
||||||
#define FSNAME_LEN 100
|
|
||||||
char name[FSNAME_LEN] = "";
|
|
||||||
|
|
||||||
FilePathValue(str);
|
FilePathValue(str);
|
||||||
path = RSTRING_PTR(str);
|
str = rb_str_encode_ospath(str);
|
||||||
fp = setmntent("/etc/mtab", "r");
|
if (STATFS(StringValueCStr(str), &st) == -1) {
|
||||||
if (!fp) rb_sys_fail("setmntent(/etb/mtab)");;
|
rb_sys_fail_str(str);
|
||||||
|
}
|
||||||
while (getmntent_r(fp, &mntbuf, buf, buflen)) {
|
# ifdef HAVE_STRUCT_STATFS_T_F_FSTYPENAME
|
||||||
int i;
|
if (st.f_fstypename[0])
|
||||||
char *mnt_dir = mntbuf.mnt_dir;
|
return CSTR(st.f_fstypename);
|
||||||
for (i=0; mnt_dir[i]; i++) {
|
# endif
|
||||||
if (mnt_dir[i] != path[i]) {
|
switch (st.f_type) {
|
||||||
goto next_entry;
|
case 0x9123683E: /* BTRFS_SUPER_MAGIC */
|
||||||
}
|
return CSTR("btrfs");
|
||||||
}
|
case 0x7461636f: /* OCFS2_SUPER_MAGIC */
|
||||||
if (i >= len) {
|
return CSTR("ocfs");
|
||||||
len = i;
|
case 0xEF53: /* EXT2_SUPER_MAGIC EXT3_SUPER_MAGIC EXT4_SUPER_MAGIC */
|
||||||
strlcpy(name, mntbuf.mnt_type, FSNAME_LEN);
|
return CSTR("ext4");
|
||||||
}
|
case 0x58465342: /* XFS_SUPER_MAGIC */
|
||||||
next_entry:
|
return CSTR("xfs");
|
||||||
;
|
case 0x01021994: /* TMPFS_MAGIC */
|
||||||
|
return CSTR("tmpfs");
|
||||||
}
|
}
|
||||||
endmntent(fp);
|
|
||||||
|
|
||||||
if (!len) rb_sys_fail("no matching entry");;
|
|
||||||
return rb_str_new_cstr(name);
|
|
||||||
#else
|
|
||||||
return Qnil;
|
|
||||||
#endif
|
#endif
|
||||||
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue