sys-filesystem 1.1.1 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ ######################################################################
2
+ # example_mount.rb
3
+ #
4
+ # Example program that demonstrates the Filesystem.mount method.
5
+ # Simulates the `mount` command in ruby
6
+ ######################################################################
7
+ require 'optparse'
8
+
9
+ options = {:mount_options => []}
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: #$0 [-o options] [-t external_type] special node"
13
+
14
+ opts.on("-o=OPTIONS", "Set one or many mount options (comma delimited)") do |cli_opts|
15
+ options[:mount_options] += cli_opts.split(',')
16
+ end
17
+
18
+ opts.on("-r", "Set readonly flag") do
19
+ options[:read_only] = true
20
+ end
21
+
22
+ opts.on("-t=EXTERNAL_TYPE", "Set the filesystem type") do |type|
23
+ options[:type] = type
24
+ end
25
+
26
+ opts.on("-v", "--version", "Display version") do
27
+ options[:version] = true
28
+ end
29
+
30
+ opts.separator ""
31
+ opts.separator "Examples:"
32
+ opts.separator ""
33
+ opts.separator " NFS: ruby #$0 -t nfs 192.168.0.10:/var/nfs /mnt/nfs"
34
+ opts.separator ""
35
+ opts.separator " SMB: ruby #$0 -t cifs //192.168.0.10/share /mnt/smb/ -o username=user,password=pass,domain=example.com"
36
+ opts.separator ""
37
+ end.parse!
38
+
39
+ require 'sys/filesystem'
40
+ include Sys
41
+
42
+ if options[:version]
43
+ puts "Sys::Filesystem::VERSION: #{Filesystem::VERSION}"
44
+ exit
45
+ end
46
+
47
+ def die msg
48
+ warn msg
49
+ exit 1
50
+ end
51
+
52
+ mount_flags = options[:read_only] ? Filesystem::MNT_RDONLY : 0
53
+ mnt_path, mnt_point = ARGV[0,2]
54
+
55
+ case options[:type]
56
+ when "cifs"
57
+ # keep mnt_path as is
58
+ when "nfs"
59
+ host, path, err = mnt_path.split(":")
60
+
61
+ die "ERROR: mount_pount '#{mnt_path}' should only contain 1 ':'" if err
62
+
63
+ mnt_path = ":#{path}"
64
+ options[:mount_options] << "addr=#{host}"
65
+ else
66
+ die "ERROR: unknown mount type!"
67
+ end
68
+
69
+ Filesystem.mount mnt_path,
70
+ mnt_point,
71
+ options[:type],
72
+ mount_flags,
73
+ options[:mount_options].join(',')
@@ -15,6 +15,8 @@ puts "Block size: " + stat.block_size.to_s
15
15
  puts "Fragment size: " + stat.fragment_size.to_s
16
16
  puts "Blocks free: " + stat.blocks_free.to_s
17
17
  puts "Blocks available: " + stat.blocks_available.to_s
18
+ puts "Bytes free: " + stat.bytes_free.to_s
19
+ puts "Bytes available: " + stat.bytes_available.to_s
18
20
  puts "Files/Inodes: " + stat.files.to_s
19
21
  puts "Files/Inodes free: " + stat.files_free.to_s
20
22
  puts "Files/Inodes available: " + stat.files_available.to_s
@@ -1,5 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbconfig'
4
+
1
5
  if File::ALT_SEPARATOR
2
- require 'windows/sys/filesystem'
6
+ require_relative 'windows/sys/filesystem'
3
7
  else
4
- require 'unix/sys/filesystem'
8
+ require_relative 'unix/sys/filesystem'
9
+ end
10
+
11
+ # Methods and properties universal to all platforms
12
+
13
+ # The Sys module serves as a namespace only.
14
+ module Sys
15
+ # The Filesystem class serves as an abstract base class. Its methods
16
+ # return objects of other types. Do not instantiate.
17
+ class Filesystem
18
+ # The version of the sys-filesystem library
19
+ VERSION = '1.5.1'
20
+
21
+ # Stat objects are returned by the Sys::Filesystem.stat method. Here
22
+ # we're adding universal methods.
23
+ class Stat
24
+ # Returns true if the filesystem is case sensitive for the current path.
25
+ # Typically this will be any path on MS Windows or Macs using HFS.
26
+ #
27
+ # For a root path (really any path without actual a-z characters) we
28
+ # take a best guess based on the host operating system. However, as a
29
+ # general rule, I do not recommend using this method for a root path.
30
+ #
31
+ def case_insensitive?
32
+ if path =~ /\w+/
33
+ File.identical?(path, path.swapcase)
34
+ elsif RbConfig::CONFIG['host_os'] =~ /darwin|mac|windows|mswin|mingw/i
35
+ true # Assumes HFS/APFS on Mac
36
+ else
37
+ false
38
+ end
39
+ end
40
+
41
+ # Opposite of case_insensitive?
42
+ #
43
+ def case_sensitive?
44
+ !case_insensitive?
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ # Reopen the Numeric class and add some convenient methods
51
+ # for converting bytes to kb, mb, and gb.
52
+ class Numeric
53
+ # call-seq:
54
+ # <tt>num</tt>.to_kb
55
+ #
56
+ # Returns +num+ in terms of kilobytes.
57
+ def to_kb
58
+ self / 1024
59
+ end
60
+
61
+ # call-seq:
62
+ # <tt>num</tt>.to_mb
63
+ #
64
+ # Returns +num+ in terms of megabytes.
65
+ def to_mb
66
+ self / 1048576
67
+ end
68
+
69
+ # call-seq:
70
+ # <tt>num</tt>.to_gb
71
+ #
72
+ # Returns +num+ in terms of gigabytes.
73
+ def to_gb
74
+ self / 1073741824
75
+ end
76
+
77
+ # call-seq:
78
+ # <tt>num</tt>.to_gb
79
+ #
80
+ # Returns +num+ in terms of terabytes.
81
+ def to_tb
82
+ self / 1099511627776
83
+ end
5
84
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sys
4
+ class Filesystem
5
+ module Constants
6
+ MNT_RDONLY = 0x00000001 # read only filesystem
7
+ MNT_SYNCHRONOUS = 0x00000002 # file system written synchronously
8
+ MNT_NOEXEC = 0x00000004 # can't exec from filesystem
9
+ MNT_NOSUID = 0x00000008 # don't honor setuid bits on fs
10
+ MNT_NODEV = 0x00000010 # don't interpret special files
11
+ MNT_UNION = 0x00000020 # union with underlying filesystem
12
+ MNT_ASYNC = 0x00000040 # file system written asynchronously
13
+ MNT_CPROTECT = 0x00000080 # file system supports content protection
14
+ MNT_EXPORTED = 0x00000100 # file system is exported
15
+ MNT_QUARANTINE = 0x00000400 # file system is quarantined
16
+ MNT_LOCAL = 0x00001000 # filesystem is stored locally
17
+ MNT_QUOTA = 0x00002000 # quotas are enabled on filesystem
18
+ MNT_ROOTFS = 0x00004000 # identifies the root filesystem
19
+ MNT_DOVOLFS = 0x00008000 # FS supports volfs (deprecated)
20
+ MNT_DONTBROWSE = 0x00100000 # FS is not appropriate path to user data
21
+ MNT_IGNORE_OWNERSHIP = 0x00200000 # VFS will ignore ownership info on FS objects
22
+ MNT_AUTOMOUNTED = 0x00400000 # filesystem was mounted by automounter
23
+ MNT_JOURNALED = 0x00800000 # filesystem is journaled
24
+ MNT_NOUSERXATTR = 0x01000000 # Don't allow user extended attributes
25
+ MNT_DEFWRITE = 0x02000000 # filesystem should defer writes
26
+ MNT_MULTILABEL = 0x04000000 # MAC support for individual labels
27
+ MNT_NOATIME = 0x10000000 # disable update of file access time
28
+ MNT_NOCLUSTERR = 0x40000000 # disable cluster read
29
+ MNT_NOCLUSTERW = 0x80000000 # disable cluster write
30
+
31
+ MNT_VISFLAGMASK = (
32
+ MNT_RDONLY | MNT_SYNCHRONOUS | MNT_NOEXEC |
33
+ MNT_NOSUID | MNT_NODEV | MNT_UNION |
34
+ MNT_ASYNC | MNT_EXPORTED | MNT_QUARANTINE |
35
+ MNT_LOCAL | MNT_QUOTA |
36
+ MNT_ROOTFS | MNT_DOVOLFS | MNT_DONTBROWSE |
37
+ MNT_IGNORE_OWNERSHIP | MNT_AUTOMOUNTED | MNT_JOURNALED |
38
+ MNT_NOUSERXATTR | MNT_DEFWRITE | MNT_MULTILABEL |
39
+ MNT_NOATIME | MNT_CPROTECT
40
+ )
41
+
42
+ MS_RDONLY = 1
43
+ MS_NOSUID = 2
44
+ MS_NODEV = 4
45
+ MS_NOEXEC = 8
46
+ MS_SYNCHRONOUS = 16
47
+ MS_REMOUNT = 32
48
+ MS_MANDLOCK = 64
49
+ MS_DIRSYNC = 128
50
+ MS_NOATIME = 1024
51
+ MS_NODIRATIME = 2048
52
+ MS_BIND = 4096
53
+ MS_MOVE = 8192
54
+ MS_REC = 16384
55
+ MS_SILENT = 32768
56
+ MS_POSIXACL = 1 << 16
57
+ MS_UNBINDABLE = 1 << 17
58
+ MS_PRIVATE = 1 << 18
59
+ MS_SLAVE = 1 << 19
60
+ MS_SHARED = 1 << 20
61
+ MS_RELATIME = 1 << 21
62
+ MS_KERNMOUNT = 1 << 22
63
+ MS_I_VERSION = 1 << 23
64
+ MS_STRICTATIME = 1 << 24
65
+ MS_ACTIVE = 1 << 30
66
+ MS_NOUSER = 1 << 31
67
+
68
+ MNT_FORCE = 1
69
+ MNT_DETACH = 2
70
+ MNT_EXPIRE = 4
71
+ UMOUNT_NOFOLLOW = 8
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+
5
+ module Sys
6
+ class Filesystem
7
+ # A scoped module for internal FFI functions to be used by the main code.
8
+ module Functions
9
+ extend FFI::Library
10
+
11
+ ffi_lib FFI::Library::LIBC
12
+
13
+ def self.linux64?
14
+ if RUBY_PLATFORM == 'java'
15
+ RbConfig::CONFIG['host_os'] =~ /linux/i &&
16
+ ENV_JAVA['sun.arch.data.model'].to_i == 64
17
+ else
18
+ RbConfig::CONFIG['host_os'] =~ /linux/i &&
19
+ (RbConfig::CONFIG['arch'] =~ /64/ || RbConfig::CONFIG['DEFS'] =~ /64/)
20
+ end
21
+ end
22
+
23
+ private_class_method :linux64?
24
+
25
+ if linux64?
26
+ begin
27
+ attach_function(:statvfs, :statvfs64, %i[string pointer], :int)
28
+ rescue FFI::NotFoundError # Not every Linux distro has an alias
29
+ attach_function(:statvfs, %i[string pointer], :int)
30
+ end
31
+ else
32
+ attach_function(:statvfs, %i[string pointer], :int)
33
+ end
34
+
35
+ attach_function(:strerror, [:int], :string)
36
+ attach_function(:mount_c, :mount, %i[string string string ulong string], :int)
37
+
38
+ if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach|bsd|dragonfly/i
39
+ attach_function(:umount_c, :unmount, %i[string int], :int)
40
+ else
41
+ attach_function(:umount_c, :umount2, %i[string int], :int)
42
+ end
43
+
44
+ private_class_method :statvfs, :strerror, :mount_c, :umount_c
45
+
46
+ begin
47
+ attach_function(:getmntent, [:pointer], :pointer)
48
+ attach_function(:setmntent, %i[string string], :pointer)
49
+ attach_function(:endmntent, [:pointer], :int)
50
+ private_class_method :getmntent, :setmntent, :endmntent
51
+ rescue FFI::NotFoundError
52
+ if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
53
+ begin
54
+ attach_function(:getmntinfo, :getmntinfo64, %i[pointer int], :int)
55
+ rescue FFI::NotFoundError
56
+ attach_function(:getmntinfo, %i[pointer int], :int) # Big Sur and later
57
+ end
58
+ else
59
+ attach_function(:getmntinfo, %i[pointer int], :int)
60
+ end
61
+ private_class_method :getmntinfo
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,272 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require 'rbconfig'
5
+
6
+ module Sys
7
+ class Filesystem
8
+ module Structs
9
+ # Used by DragonFlyBSD
10
+ class UUID < FFI::Struct
11
+ UUID_NODE_LEN = 6
12
+
13
+ layout(
14
+ :time_low, :uint32,
15
+ :time_mid, :uint16,
16
+ :time_hi_and_version, :uint16,
17
+ :clock_seq_hi_and_reserved, :uint8,
18
+ :clock_seq_low, :uint8,
19
+ :node, [:uint8, UUID_NODE_LEN]
20
+ )
21
+ end
22
+
23
+ # The Statfs struct is a subclass of FFI::Struct that corresponds to a struct statfs.
24
+ class Statfs < FFI::Struct
25
+ # Private method that will determine the layout of the struct on Linux.
26
+ def self.linux64?
27
+ if RUBY_PLATFORM == 'java'
28
+ ENV_JAVA['sun.arch.data.model'].to_i == 64
29
+ else
30
+ RbConfig::CONFIG['host_os'] =~ /linux/i &&
31
+ (RbConfig::CONFIG['arch'] =~ /64/ || RbConfig::CONFIG['DEFS'] =~ /64/)
32
+ end
33
+ end
34
+
35
+ private_class_method :linux64?
36
+
37
+ # FreeBSD 12.0 MNAMELEN from 88 => 1024.
38
+ MNAMELEN =
39
+ if RbConfig::CONFIG['host_os'] =~ /freebsd(.*)/i
40
+ Regexp.last_match(1).to_f < 12.0 ? 88 : 1024
41
+ else
42
+ 88
43
+ end
44
+
45
+ case RbConfig::CONFIG['host_os']
46
+ when /bsd/i
47
+ layout(
48
+ :f_version, :uint32,
49
+ :f_type, :uint32,
50
+ :f_flags, :uint64,
51
+ :f_bsize, :uint64,
52
+ :f_iosize, :int64,
53
+ :f_blocks, :uint64,
54
+ :f_bfree, :uint64,
55
+ :f_bavail, :int64,
56
+ :f_files, :uint64,
57
+ :f_ffree, :uint64,
58
+ :f_syncwrites, :uint64,
59
+ :f_asyncwrites, :uint64,
60
+ :f_syncreads, :uint64,
61
+ :f_asyncreads, :uint64,
62
+ :f_spare, [:uint64, 10],
63
+ :f_namemax, :uint32,
64
+ :f_owner, :int32,
65
+ :f_fsid, [:int32, 2],
66
+ :f_charspare, [:char, 80],
67
+ :f_fstypename, [:char, 16],
68
+ :f_mntfromname, [:char, MNAMELEN],
69
+ :f_mntonname, [:char, MNAMELEN]
70
+ )
71
+ when /linux/i
72
+ if linux64?
73
+ layout(
74
+ :f_type, :ulong,
75
+ :f_bsize, :ulong,
76
+ :f_blocks, :uint64,
77
+ :f_bfree, :uint64,
78
+ :f_bavail, :uint64,
79
+ :f_files, :uint64,
80
+ :f_ffree, :uint64,
81
+ :f_fsid, [:int, 2],
82
+ :f_namelen, :ulong,
83
+ :f_frsize, :ulong,
84
+ :f_flags, :ulong,
85
+ :f_spare, [:ulong, 4]
86
+ )
87
+ else
88
+ layout(
89
+ :f_type, :ulong,
90
+ :f_bsize, :ulong,
91
+ :f_blocks, :uint32,
92
+ :f_bfree, :uint32,
93
+ :f_bavail, :uint32,
94
+ :f_files, :uint32,
95
+ :f_ffree, :uint32,
96
+ :f_fsid, [:int, 2],
97
+ :f_namelen, :ulong,
98
+ :f_frsize, :ulong,
99
+ :f_flags, :ulong,
100
+ :f_spare, [:ulong, 4]
101
+ )
102
+ end
103
+ when /dragonfly/i
104
+ layout(
105
+ :f_spare2, :long,
106
+ :f_bsize, :long,
107
+ :f_iosize, :long,
108
+ :f_blocks, :long,
109
+ :f_bfree, :long,
110
+ :f_bavail, :long,
111
+ :f_files, :long,
112
+ :f_ffree, :long,
113
+ :f_fsid, [:int32_t, 2],
114
+ :f_owner, :uid_t,
115
+ :f_type, :int,
116
+ :f_flags, :int,
117
+ :f_syncwrites, :long,
118
+ :f_asyncwrites, :long,
119
+ :f_fstypename, [:char, 16],
120
+ :f_mntonname, [:char, 80],
121
+ :f_syncreads, :long,
122
+ :f_asyncreads, :long,
123
+ :f_spares1, :short,
124
+ :f_mntfromname, [:char, 80],
125
+ :f_spares2, :short,
126
+ :f_spare, [:long, 2]
127
+ )
128
+ else
129
+ layout(
130
+ :f_bsize, :uint32,
131
+ :f_iosize, :int32,
132
+ :f_blocks, :uint64,
133
+ :f_bfree, :uint64,
134
+ :f_bavail, :uint64,
135
+ :f_files, :uint64,
136
+ :f_ffree, :uint64,
137
+ :f_fsid, [:int32, 2],
138
+ :f_owner, :int32,
139
+ :f_type, :uint32,
140
+ :f_flags, :uint32,
141
+ :f_fssubtype, :uint32,
142
+ :f_fstypename, [:char, 16],
143
+ :f_mntonname, [:char, 1024],
144
+ :f_mntfromname, [:char, 1024],
145
+ :f_reserved, [:uint32, 8]
146
+ )
147
+ end
148
+ end
149
+
150
+ # The Statvfs struct represents struct statvfs from sys/statvfs.h.
151
+ class Statvfs < FFI::Struct
152
+ # Private method that will determine the layout of the struct on Linux.
153
+ def self.linux64?
154
+ if RUBY_PLATFORM == 'java'
155
+ ENV_JAVA['sun.arch.data.model'].to_i == 64
156
+ else
157
+ RbConfig::CONFIG['host_os'] =~ /linux/i &&
158
+ (RbConfig::CONFIG['arch'] =~ /64/ || RbConfig::CONFIG['DEFS'] =~ /64/)
159
+ end
160
+ end
161
+
162
+ if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
163
+ layout(
164
+ :f_bsize, :ulong,
165
+ :f_frsize, :ulong,
166
+ :f_blocks, :uint,
167
+ :f_bfree, :uint,
168
+ :f_bavail, :uint,
169
+ :f_files, :uint,
170
+ :f_ffree, :uint,
171
+ :f_favail, :uint,
172
+ :f_fsid, :ulong,
173
+ :f_flag, :ulong,
174
+ :f_namemax, :ulong
175
+ )
176
+ elsif RbConfig::CONFIG['host'] =~ /bsd/i
177
+ layout(
178
+ :f_bavail, :uint64,
179
+ :f_bfree, :uint64,
180
+ :f_blocks, :uint64,
181
+ :f_favail, :uint64,
182
+ :f_ffree, :uint64,
183
+ :f_files, :uint64,
184
+ :f_bsize, :ulong,
185
+ :f_flag, :ulong,
186
+ :f_frsize, :ulong,
187
+ :f_fsid, :ulong,
188
+ :f_namemax, :ulong
189
+ )
190
+ elsif RbConfig::CONFIG['host'] =~ /dragonfly/i
191
+ layout(
192
+ :f_bsize, :ulong,
193
+ :f_frsize, :ulong,
194
+ :f_blocks, :uint64,
195
+ :f_bfree, :uint64,
196
+ :f_bavail, :uint64,
197
+ :f_files, :uint64,
198
+ :f_ffree, :uint64,
199
+ :f_favail, :uint64,
200
+ :f_fsid, :ulong,
201
+ :f_flag, :ulong,
202
+ :f_namemax, :ulong,
203
+ :f_owner, :uid_t,
204
+ :f_type, :uint,
205
+ :f_syncreads, :uint64,
206
+ :f_syncwrites, :uint64,
207
+ :f_asyncreads, :uint64,
208
+ :f_asyncwrites, :uint64,
209
+ :f_fsid_uuid, UUID,
210
+ :f_uid_uuid, UUID
211
+ )
212
+ elsif !linux64?
213
+ layout(
214
+ :f_bsize, :ulong,
215
+ :f_frsize, :ulong,
216
+ :f_blocks, :uint,
217
+ :f_bfree, :uint,
218
+ :f_bavail, :uint,
219
+ :f_files, :uint,
220
+ :f_ffree, :uint,
221
+ :f_favail, :uint,
222
+ :f_fsid, :ulong,
223
+ :f_unused, :int,
224
+ :f_flag, :ulong,
225
+ :f_namemax, :ulong,
226
+ :f_type, :uint,
227
+ :f_spare, [:int, 5]
228
+ )
229
+ else
230
+ require 'mkmf-lite'
231
+ extend Mkmf::Lite
232
+ layout_array = [
233
+ :f_bsize, :ulong,
234
+ :f_frsize, :ulong,
235
+ :f_blocks, :uint64,
236
+ :f_bfree, :uint64,
237
+ :f_bavail, :uint64,
238
+ :f_files, :uint64,
239
+ :f_ffree, :uint64,
240
+ :f_favail, :uint64,
241
+ :f_fsid, :ulong,
242
+ :f_flag, :ulong,
243
+ :f_namemax, :ulong,
244
+ :f_type, :uint,
245
+ :f_spare, [:int, 5]
246
+ ]
247
+
248
+ # Check if we need to add __f_unused
249
+ f_unused_bytes = 2 * check_sizeof('int') - check_sizeof('long')
250
+ if f_unused_bytes > 0
251
+ # For Big Endian, this should be inserted at -9
252
+ layout_array.insert(-7, :__f_unused, [:char, f_unused_bytes])
253
+ end
254
+
255
+ layout(*layout_array)
256
+ end
257
+ end
258
+
259
+ # The Mntent struct represents struct mntent from sys/mount.h on Unix.
260
+ class Mntent < FFI::Struct
261
+ layout(
262
+ :mnt_fsname, :string,
263
+ :mnt_dir, :string,
264
+ :mnt_type, :string,
265
+ :mnt_opts, :string,
266
+ :mnt_freq, :int,
267
+ :mnt_passno, :int
268
+ )
269
+ end
270
+ end
271
+ end
272
+ end