sys-filesystem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ == 0.1.0 - 17-Nov-2006
2
+ * Initial release. Alpha. Code is stable, but API is not.
data/MANIFEST ADDED
@@ -0,0 +1,14 @@
1
+ MANIFEST
2
+ CHANGES
3
+ README
4
+ install.rb
5
+ extconf.rb
6
+ sys-fileystem.gemspec
7
+
8
+ examples/example_stat.rb
9
+
10
+ ext/filesystem.c
11
+
12
+ lib/sys/filesystem.rb
13
+
14
+ test/tc_filesystem.rb
data/README ADDED
@@ -0,0 +1,49 @@
1
+ = Description
2
+ A Ruby interface for getting file system information.
3
+
4
+ = Prerequisites
5
+ === MS Windows
6
+ * windows-pr, 0.6.0 or later.
7
+
8
+ = Installation
9
+ === MS Windows:
10
+ ruby test/tc_filesystem.rb (optional)
11
+ ruby install.rb
12
+
13
+ === All other platforms:
14
+ ruby extconf.rb
15
+ make
16
+ ruby test/tc_filesystem.rb (optional)
17
+ make install
18
+
19
+ = Notes
20
+ === MS Windows
21
+ This is a pure Ruby implementation using the windows-pr package, which in
22
+ turn wraps native Windows functions.
23
+ === UNIX
24
+ This is a C extension that wraps statvfs, etc.
25
+
26
+ = Known Bugs
27
+ None that I'm aware of. Please report bugs on the project page at
28
+ http://www.rubyforge.org/projects/sysutils.
29
+
30
+ = Future Plans
31
+ Add support for mount points/volumes.
32
+
33
+ = Acknowledgements
34
+ Mike Hall, for ideas and code that I borrowed from his 'filesystem' package.
35
+ Park Heesob, for implementation and API ideas for the MS Windows version.
36
+
37
+ = Copyright
38
+ (C) 2003-2006 Daniel J. Berger
39
+ All Rights Reserved
40
+
41
+ = Warranty
42
+ This package is provided "as is" and without any express or
43
+ implied warranties, including, without limitation, the implied
44
+ warranties of merchantability and fitness for a particular purpose.
45
+
46
+ = Author
47
+ Daniel J. Berger
48
+ djberg96 at gmail dot com
49
+ imperator on IRC (irc.freenode.net)
data/ext/filesystem.c ADDED
@@ -0,0 +1,181 @@
1
+ /********************************************************************
2
+ * filesystem.c
3
+ *
4
+ * This is the source file for the sys-filesystem package for Ruby.
5
+ * This is for UNIX platforms only.
6
+ ********************************************************************/
7
+ #include <ruby.h>
8
+
9
+ #include <sys/types.h>
10
+ #include <sys/statvfs.h>
11
+
12
+ #ifdef HAVE_SYS_MNTTAB_H
13
+
14
+ /* Solaris */
15
+ #include <sys/mnttab.h>
16
+ #define MNTENT mnttab
17
+ #define START_MNT(F,M) fopen(F,M)
18
+ #define GET_MNT(FP,MP) (getmntent(FP,MP) == 0)
19
+ #define END_MNT(F) fclose(F)
20
+ #define MOUNTLIST "/etc/mnttab"
21
+
22
+ #else
23
+
24
+ /* All other flavors of UNIX */
25
+ #include <mntent.h>
26
+ #define MNTENT mntent
27
+ #define START_MNT(F,M) setmntent(F,M)
28
+ #define GET_MNT(FP,MP) ((MP = getmntent(FP)) != NULL)
29
+ #define END_MNT(F) endmntent(F)
30
+ #define MOUNTLIST "/etc/mtab"
31
+
32
+ #endif
33
+
34
+ VALUE mSys, cFilesys, cStat;
35
+
36
+ /* call-seq:
37
+ * Filesystem.stat(path)
38
+ *
39
+ * Returns a a Filesystem::Stat object containing information about the +path+
40
+ * file system.
41
+ */
42
+ static VALUE fs_stat(VALUE klass, VALUE v_path){
43
+ VALUE v_stat;
44
+ struct statvfs fs;
45
+ char* path = StringValuePtr(v_path);
46
+
47
+ if(statvfs(path, &fs) < 0)
48
+ rb_sys_fail("statvfs");
49
+
50
+ v_stat = rb_funcall(cStat, rb_intern("new"), 0, 0);
51
+
52
+ rb_iv_set(v_stat, "@path", v_path);
53
+ rb_iv_set(v_stat, "@block_size", ULONG2NUM(fs.f_bsize));
54
+ rb_iv_set(v_stat, "@fragment_size", ULONG2NUM(fs.f_frsize));
55
+ rb_iv_set(v_stat, "@blocks", LONG2NUM(fs.f_blocks));
56
+ rb_iv_set(v_stat, "@blocks_free", LONG2NUM(fs.f_bfree));
57
+ rb_iv_set(v_stat, "@blocks_available", LONG2NUM(fs.f_bavail));
58
+ rb_iv_set(v_stat, "@files", LONG2NUM(fs.f_files));
59
+ rb_iv_set(v_stat, "@files_free", LONG2NUM(fs.f_ffree));
60
+ rb_iv_set(v_stat, "@files_available", LONG2NUM(fs.f_favail));
61
+ rb_iv_set(v_stat, "@filesystem_id", ULONG2NUM(fs.f_fsid));
62
+ rb_iv_set(v_stat, "@flags", ULONG2NUM(fs.f_flag));
63
+ rb_iv_set(v_stat, "@name_max", ULONG2NUM(fs.f_namemax));
64
+
65
+ #ifdef HAVE_ST_F_BASETYPE
66
+ rb_iv_set(v_stat, "@base_type", rb_str_new2(fs.f_basetype));
67
+ #endif
68
+
69
+ return rb_obj_freeze(v_stat);
70
+ }
71
+
72
+ /* Convenient methods for converting bytes to kilobytes, megabytes or
73
+ * gigabytes.
74
+ */
75
+
76
+ /*
77
+ * call-seq:
78
+ * <tt>fix</tt>.to_kb
79
+ *
80
+ * Returns +fix+ in terms of kilobytes.
81
+ */
82
+ static VALUE fixnum_to_kb(VALUE self){
83
+ return ULL2NUM(NUM2ULONG(self) / 1024);
84
+ }
85
+
86
+ /*
87
+ * call-seq:
88
+ * <tt>fix</tt>.to_mb
89
+ *
90
+ * Returns +fix+ in terms of megabytes.
91
+ */
92
+ static VALUE fixnum_to_mb(VALUE self){
93
+ return ULL2NUM(NUM2ULONG(self) / 1048576);
94
+ }
95
+
96
+ /*
97
+ * call-seq:
98
+ * <tt>fix</tt>.to_gb
99
+ *
100
+ * Returns +fix+ in terms of gigabytes.
101
+ */
102
+ static VALUE fixnum_to_gb(VALUE self){
103
+ return ULL2NUM(NUM2ULONG(self) / 1073741824);
104
+ }
105
+
106
+ void Init_filesystem(){
107
+ /* Classes and Modules */
108
+ mSys = rb_define_module("Sys");
109
+ cFilesys = rb_define_class_under(mSys, "Filesystem", rb_cObject);
110
+ cStat = rb_define_class_under(cFilesys, "Stat", rb_cObject);
111
+
112
+ /* Singleton methods */
113
+ rb_define_singleton_method(cFilesys, "stat", fs_stat, 1);
114
+
115
+ /* Stat accessors */
116
+
117
+ /* The path of the file system */
118
+ rb_define_attr(cStat, "path", 1, 0);
119
+
120
+ /* The preferred system block size */
121
+ rb_define_attr(cStat, "block_size", 1, 0);
122
+
123
+ /* The fragment size, i.e. fundamental file system block size */
124
+ rb_define_attr(cStat, "fragment_size", 1, 0);
125
+
126
+ /* The total number of +fragment_size+ blocks in the file system */
127
+ rb_define_attr(cStat, "blocks", 1, 0);
128
+
129
+ /* The total number of free blocks in the file system */
130
+ rb_define_attr(cStat, "blocks_free", 1, 0);
131
+
132
+ /* The number of free blocks available to unprivileged processes */
133
+ rb_define_attr(cStat, "blocks_available", 1, 0);
134
+
135
+ /* The total number of files/inodes that can be created */
136
+ rb_define_attr(cStat, "files", 1, 0);
137
+
138
+ /* The total number of free files/inodes on the file system */
139
+ rb_define_attr(cStat, "files_free", 1, 0);
140
+
141
+ /* The number of free files/inodes available to unprivileged processes */
142
+ rb_define_attr(cStat, "files_available", 1, 0);
143
+
144
+ /* The file system identifier */
145
+ rb_define_attr(cStat, "filesystem_id", 1, 0);
146
+
147
+ /* The file system type, e.g. UFS */
148
+ rb_define_attr(cStat, "base_type", 1, 0);
149
+
150
+ /* A bit mask of flags. See the <tt>Constants</tt> for a list of flags */
151
+ rb_define_attr(cStat, "flags", 1, 0);
152
+
153
+ /* The maximum length of a file name permitted on the file system */
154
+ rb_define_attr(cStat, "name_max", 1, 0);
155
+
156
+ /* Constants */
157
+
158
+ /* The version of this package */
159
+ rb_define_const(cFilesys, "VERSION", rb_str_new2("0.1.0"));
160
+
161
+ /* Read only file system */
162
+ rb_define_const(cStat, "RDONLY", INT2FIX(ST_RDONLY));
163
+
164
+ /* File system does not support suid or sgid semantics */
165
+ rb_define_const(cStat, "NOSUID", INT2FIX(ST_NOSUID));
166
+
167
+ #ifdef ST_NOTRUNC
168
+ /* File system does not truncate file names longer than +name_max+ */
169
+ rb_define_const(cStat, "NOTRUNC", INT2FIX(ST_NOTRUNC));
170
+ #endif
171
+
172
+ /* Aliases */
173
+ rb_define_alias(cStat, "inodes", "files");
174
+ rb_define_alias(cStat, "inodes_free", "files_free");
175
+ rb_define_alias(cStat, "inodes_available", "files_available");
176
+
177
+ /* Convenient methods for Fixnum */
178
+ rb_define_method(rb_cFixnum, "to_kb", fixnum_to_kb, 0);
179
+ rb_define_method(rb_cFixnum, "to_mb", fixnum_to_mb, 0);
180
+ rb_define_method(rb_cFixnum, "to_gb", fixnum_to_gb, 0);
181
+ }
data/extconf.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+ require 'fileutils'
3
+
4
+ have_header("sys/mnttab.h")
5
+ have_struct_member("struct statvfs", "f_basetype", "sys/statvfs.h")
6
+
7
+ FileUtils.cp('ext/filesystem.c', '.')
8
+
9
+ create_makefile("sys/filesystem")
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__))
2
+
3
+ if RUBY_PLATFORM.match('mswin')
4
+ require 'tc_windows'
5
+ else
6
+ require 'tc_unix'
7
+ end
data/test/tc_unix.rb ADDED
@@ -0,0 +1,129 @@
1
+ ####################################################################
2
+ # tc_unix.rb
3
+ #
4
+ # Test case for the Sys::Filesystem.stat method and related stuff.
5
+ ####################################################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == 'test' || base =~ /filesystem/i
9
+ require 'fileutils'
10
+ require 'rbconfig'
11
+ Dir.chdir('..') if base == 'test'
12
+ Dir.mkdir('sys') rescue nil
13
+ file = 'filesystem.' + Config::CONFIG['DLEXT']
14
+ FileUtils.cp(file, 'sys')
15
+ $LOAD_PATH.unshift(Dir.pwd)
16
+ end
17
+
18
+ require 'test/unit'
19
+ require 'sys/filesystem'
20
+ include Sys
21
+
22
+ class TC_Filesystem_Unix < Test::Unit::TestCase
23
+ def setup
24
+ @dir = "/"
25
+ @stat = Filesystem.stat(@dir)
26
+ @size = 58720256
27
+ end
28
+
29
+ def test_version
30
+ assert_equal('0.1.0', Filesystem::VERSION)
31
+ end
32
+
33
+ def test_stat_path
34
+ assert_respond_to(@stat, :path)
35
+ assert_equal("/", @stat.path)
36
+ end
37
+
38
+ def test_stat_block_size
39
+ assert_respond_to(@stat, :block_size)
40
+ assert_kind_of(Fixnum, @stat.block_size)
41
+ end
42
+
43
+ def test_stat_fragment_size
44
+ assert_respond_to(@stat, :fragment_size)
45
+ assert_kind_of(Fixnum, @stat.fragment_size)
46
+ end
47
+
48
+ def test_stat_blocks
49
+ assert_respond_to(@stat, :blocks)
50
+ assert_kind_of(Fixnum, @stat.blocks)
51
+ end
52
+
53
+ def test_stat_blocks_free
54
+ assert_respond_to(@stat, :blocks_free)
55
+ assert_kind_of(Fixnum, @stat.blocks_free)
56
+ end
57
+
58
+ def test_stat_blocks_available
59
+ assert_respond_to(@stat, :blocks_available)
60
+ assert_kind_of(Fixnum, @stat.blocks_available)
61
+ end
62
+
63
+ def test_stat_files
64
+ assert_respond_to(@stat, :files)
65
+ assert_respond_to(@stat, :inodes) # Alias
66
+ assert_kind_of(Fixnum, @stat.files)
67
+ end
68
+
69
+ def test_stat_files_free
70
+ assert_respond_to(@stat, :files_free)
71
+ assert_respond_to(@stat, :inodes_free) # Alias
72
+ assert_kind_of(Fixnum, @stat.files_free)
73
+ end
74
+
75
+ def test_stat_files_available
76
+ assert_respond_to(@stat, :files_available)
77
+ assert_respond_to(@stat, :inodes_available) # Alias
78
+ assert_kind_of(Fixnum, @stat.files_available)
79
+ end
80
+
81
+ def test_stat_filesystem_id
82
+ assert_respond_to(@stat, :filesystem_id)
83
+ assert_kind_of(Fixnum, @stat.filesystem_id)
84
+ end
85
+
86
+ def test_stat_flags
87
+ assert_respond_to(@stat, :flags)
88
+ assert_kind_of(Fixnum, @stat.flags)
89
+ end
90
+
91
+ def test_stat_name_max
92
+ assert_respond_to(@stat, :name_max)
93
+ assert_kind_of(Fixnum, @stat.name_max)
94
+ end
95
+
96
+ unless RUBY_PLATFORM.match('linux')
97
+ def test_stat_base_type
98
+ assert_respond_to(@stat, :base_type)
99
+ assert_kind_of(String, @stat.base_type)
100
+ end
101
+ end
102
+
103
+ def test_stat_constants
104
+ assert_not_nil(Filesystem::Stat::RDONLY)
105
+ assert_not_nil(Filesystem::Stat::NOSUID)
106
+ unless RUBY_PLATFORM.match('linux')
107
+ assert_not_nil(Filesystem::Stat::NOTRUNC)
108
+ end
109
+ end
110
+
111
+ def test_stat_expected_errors
112
+ assert_raises(ArgumentError){ Filesystem.stat }
113
+ end
114
+
115
+ def test_fixnum_methods
116
+ assert_respond_to(@size, :to_kb)
117
+ assert_respond_to(@size, :to_mb)
118
+ assert_respond_to(@size, :to_gb)
119
+
120
+ assert_equal(57344, @size.to_kb)
121
+ assert_equal(56, @size.to_mb)
122
+ assert_equal(0, @size.to_gb)
123
+ end
124
+
125
+ def teardown
126
+ @dir = nil
127
+ @stat = nil
128
+ end
129
+ end
@@ -0,0 +1,129 @@
1
+ ####################################################################
2
+ # tc_unix.rb
3
+ #
4
+ # Test case for the Sys::Filesystem.stat method and related stuff.
5
+ ####################################################################
6
+ Dir.chdir('..') if File.basename(Dir.pwd) == 'test'
7
+ $LOAD_PATH.unshift(Dir.pwd + '/lib')
8
+
9
+ require 'test/unit'
10
+ require 'sys/filesystem'
11
+ include Sys
12
+
13
+ class TC_Filesystem_Unix < Test::Unit::TestCase
14
+ def setup
15
+ @dir = "/"
16
+ @stat = Filesystem.stat(@dir)
17
+ @size = 58720256
18
+ end
19
+
20
+ def test_version
21
+ assert_equal('0.1.0', Filesystem::VERSION)
22
+ end
23
+
24
+ def test_stat_path
25
+ assert_respond_to(@stat, :path)
26
+ assert_equal("/", @stat.path)
27
+ end
28
+
29
+ def test_stat_block_size
30
+ assert_respond_to(@stat, :block_size)
31
+ assert_kind_of(Fixnum, @stat.block_size)
32
+ end
33
+
34
+ def test_stat_fragment_size
35
+ assert_respond_to(@stat, :fragment_size)
36
+ assert_nil(@stat.fragment_size)
37
+ end
38
+
39
+ def test_stat_blocks
40
+ assert_respond_to(@stat, :blocks)
41
+ assert_kind_of(Fixnum, @stat.blocks)
42
+ end
43
+
44
+ def test_stat_blocks_free
45
+ assert_respond_to(@stat, :blocks_free)
46
+ assert_kind_of(Fixnum, @stat.blocks_free)
47
+ end
48
+
49
+ def test_stat_blocks_available
50
+ assert_respond_to(@stat, :blocks_available)
51
+ assert_kind_of(Fixnum, @stat.blocks_available)
52
+ end
53
+
54
+ def test_stat_files
55
+ assert_respond_to(@stat, :files)
56
+ assert_respond_to(@stat, :inodes) # Alias
57
+ assert_nil(@stat.files)
58
+ end
59
+
60
+ def test_stat_files_free
61
+ assert_respond_to(@stat, :files_free)
62
+ assert_respond_to(@stat, :inodes_free) # Alias
63
+ assert_nil(@stat.files_free)
64
+ end
65
+
66
+ def test_stat_files_available
67
+ assert_respond_to(@stat, :files_available)
68
+ assert_respond_to(@stat, :inodes_available) # Alias
69
+ assert_nil(@stat.files_available)
70
+ end
71
+
72
+ def test_stat_filesystem_id
73
+ assert_respond_to(@stat, :filesystem_id)
74
+ assert_kind_of(Integer, @stat.filesystem_id)
75
+ end
76
+
77
+ def test_stat_flags
78
+ assert_respond_to(@stat, :flags)
79
+ assert_kind_of(Fixnum, @stat.flags)
80
+ end
81
+
82
+ def test_stat_name_max
83
+ assert_respond_to(@stat, :name_max)
84
+ assert_kind_of(Fixnum, @stat.name_max)
85
+ end
86
+
87
+ unless RUBY_PLATFORM.match('linux')
88
+ def test_stat_base_type
89
+ assert_respond_to(@stat, :base_type)
90
+ assert_kind_of(String, @stat.base_type)
91
+ end
92
+ end
93
+
94
+ def test_stat_constants
95
+ assert_not_nil(Filesystem::Stat::CASE_SENSITIVE_SEARCH)
96
+ assert_not_nil(Filesystem::Stat::CASE_PRESERVED_NAMES)
97
+ assert_not_nil(Filesystem::Stat::UNICODE_ON_DISK)
98
+ assert_not_nil(Filesystem::Stat::PERSISTENT_ACLS)
99
+ assert_not_nil(Filesystem::Stat::FILE_COMPRESSION)
100
+ assert_not_nil(Filesystem::Stat::VOLUME_QUOTAS)
101
+ assert_not_nil(Filesystem::Stat::SUPPORTS_SPARSE_FILES)
102
+ assert_not_nil(Filesystem::Stat::SUPPORTS_REPARSE_POINTS)
103
+ assert_not_nil(Filesystem::Stat::SUPPORTS_REMOTE_STORAGE)
104
+ assert_not_nil(Filesystem::Stat::VOLUME_IS_COMPRESSED)
105
+ assert_not_nil(Filesystem::Stat::SUPPORTS_OBJECT_IDS)
106
+ assert_not_nil(Filesystem::Stat::SUPPORTS_ENCRYPTION)
107
+ assert_not_nil(Filesystem::Stat::NAMED_STREAMS)
108
+ assert_not_nil(Filesystem::Stat::READ_ONLY_VOLUME)
109
+ end
110
+
111
+ def test_stat_expected_errors
112
+ assert_raises(ArgumentError){ Filesystem.stat }
113
+ end
114
+
115
+ def test_fixnum_methods
116
+ assert_respond_to(@size, :to_kb)
117
+ assert_respond_to(@size, :to_mb)
118
+ assert_respond_to(@size, :to_gb)
119
+
120
+ assert_equal(57344, @size.to_kb)
121
+ assert_equal(56, @size.to_mb)
122
+ assert_equal(0, @size.to_gb)
123
+ end
124
+
125
+ def teardown
126
+ @dir = nil
127
+ @stat = nil
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: sys-filesystem
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-11-17 00:00:00 -07:00
8
+ summary: A Ruby interface for getting file system information.
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/sysutils
13
+ rubyforge_project: sysutils
14
+ description: A Ruby interface for getting file system information.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - test/tc_filesystem.rb
33
+ - test/tc_unix.rb
34
+ - test/tc_windows.rb
35
+ - CHANGES
36
+ - MANIFEST
37
+ - README
38
+ - extconf.rb
39
+ - ext/filesystem.c
40
+ test_files:
41
+ - test/tc_filesystem.rb
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files:
45
+ - CHANGES
46
+ - README
47
+ - MANIFEST
48
+ executables: []
49
+
50
+ extensions:
51
+ - extconf.rb
52
+ requirements: []
53
+
54
+ dependencies: []
55
+