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.
@@ -1,566 +0,0 @@
1
- require 'ffi'
2
- require 'rbconfig'
3
-
4
- # The Sys module serves as a namespace only.
5
- module Sys
6
- # The Filesystem class serves as an abstract base class. Its methods
7
- # return objects of other types. Do not instantiate.
8
- class Filesystem
9
- extend FFI::Library
10
- ffi_lib FFI::Library::LIBC
11
-
12
- # The version of the sys-filesystem library.
13
- VERSION = '1.1.1'
14
-
15
- private
16
-
17
- if RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i
18
- attach_function(:statvfs, :statvfs64, [:string, :pointer], :int)
19
- else
20
- attach_function(:statvfs, [:string, :pointer], :int)
21
- end
22
-
23
- attach_function(:strerror, [:int], :string)
24
-
25
- private_class_method :statvfs, :strerror
26
-
27
- begin
28
- if RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i
29
- attach_function(:fopen, [:string, :string], :pointer)
30
- attach_function(:fclose, [:pointer], :int)
31
- attach_function(:getmntent, [:pointer, :pointer], :int)
32
- private_class_method :fopen, :fclose, :getmntent
33
- else
34
- attach_function(:getmntent, [:pointer], :pointer)
35
- attach_function(:setmntent, [:string, :string], :pointer)
36
- attach_function(:endmntent, [:pointer], :int)
37
- private_class_method :getmntent, :setmntent, :endmntent
38
- end
39
- rescue FFI::NotFoundError
40
- if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
41
- attach_function(:getmntinfo, :getmntinfo64, [:pointer, :int], :int)
42
- else
43
- attach_function(:getmntinfo, [:pointer, :int], :int)
44
- end
45
- private_class_method :getmntinfo
46
- end
47
-
48
- MNT_RDONLY = 0x00000001 # read only filesystem
49
- MNT_SYNCHRONOUS = 0x00000002 # file system written synchronously
50
- MNT_NOEXEC = 0x00000004 # can't exec from filesystem
51
- MNT_NOSUID = 0x00000008 # don't honor setuid bits on fs
52
- MNT_NODEV = 0x00000010 # don't interpret special files
53
- MNT_UNION = 0x00000020 # union with underlying filesystem
54
- MNT_ASYNC = 0x00000040 # file system written asynchronously
55
- MNT_CPROTECT = 0x00000080 # file system supports content protection
56
- MNT_EXPORTED = 0x00000100 # file system is exported
57
- MNT_QUARANTINE = 0x00000400 # file system is quarantined
58
- MNT_LOCAL = 0x00001000 # filesystem is stored locally
59
- MNT_QUOTA = 0x00002000 # quotas are enabled on filesystem
60
- MNT_ROOTFS = 0x00004000 # identifies the root filesystem
61
- MNT_DOVOLFS = 0x00008000 # FS supports volfs (deprecated)
62
- MNT_DONTBROWSE = 0x00100000 # FS is not appropriate path to user data
63
- MNT_IGNORE_OWNERSHIP = 0x00200000 # VFS will ignore ownership info on FS objects
64
- MNT_AUTOMOUNTED = 0x00400000 # filesystem was mounted by automounter
65
- MNT_JOURNALED = 0x00800000 # filesystem is journaled
66
- MNT_NOUSERXATTR = 0x01000000 # Don't allow user extended attributes
67
- MNT_DEFWRITE = 0x02000000 # filesystem should defer writes
68
- MNT_MULTILABEL = 0x04000000 # MAC support for individual labels
69
- MNT_NOATIME = 0x10000000 # disable update of file access time
70
-
71
- MNT_VISFLAGMASK = (
72
- MNT_RDONLY | MNT_SYNCHRONOUS | MNT_NOEXEC |
73
- MNT_NOSUID | MNT_NODEV | MNT_UNION |
74
- MNT_ASYNC | MNT_EXPORTED | MNT_QUARANTINE |
75
- MNT_LOCAL | MNT_QUOTA |
76
- MNT_ROOTFS | MNT_DOVOLFS | MNT_DONTBROWSE |
77
- MNT_IGNORE_OWNERSHIP | MNT_AUTOMOUNTED | MNT_JOURNALED |
78
- MNT_NOUSERXATTR | MNT_DEFWRITE | MNT_MULTILABEL |
79
- MNT_NOATIME | MNT_CPROTECT
80
- )
81
-
82
- @@opt_names = {
83
- MNT_RDONLY => 'read-only',
84
- MNT_SYNCHRONOUS => 'synchronous',
85
- MNT_NOEXEC => 'noexec',
86
- MNT_NOSUID => 'nosuid',
87
- MNT_NODEV => 'nodev',
88
- MNT_UNION => 'union',
89
- MNT_ASYNC => 'asynchronous',
90
- MNT_CPROTECT => 'content-protection',
91
- MNT_EXPORTED => 'exported',
92
- MNT_QUARANTINE => 'quarantined',
93
- MNT_LOCAL => 'local',
94
- MNT_QUOTA => 'quotas',
95
- MNT_ROOTFS => 'rootfs',
96
- MNT_DONTBROWSE => 'nobrowse',
97
- MNT_IGNORE_OWNERSHIP => 'noowners',
98
- MNT_AUTOMOUNTED => 'automounted',
99
- MNT_JOURNALED => 'journaled',
100
- MNT_NOUSERXATTR => 'nouserxattr',
101
- MNT_DEFWRITE => 'defwrite',
102
- MNT_NOATIME => 'noatime'
103
- }
104
-
105
- # File used to read mount informtion from.
106
- if File.exists?('/etc/mtab')
107
- MOUNT_FILE = '/etc/mtab'
108
- elsif File.exists?('/etc/mnttab')
109
- MOUNT_FILE = '/etc/mnttab'
110
- else
111
- MOUNT_FILE = 'getmntinfo'
112
- end
113
-
114
- class Statfs < FFI::Struct
115
- if RbConfig::CONFIG['host_os'] =~ /bsd/i
116
- layout(
117
- :f_version, :uint32,
118
- :f_type, :uint32,
119
- :f_flags, :uint64,
120
- :f_bsize, :uint64,
121
- :f_iosize, :int64,
122
- :f_blocks, :uint64,
123
- :f_bfree, :uint64,
124
- :f_bavail, :int64,
125
- :f_files, :uint64,
126
- :f_ffree, :uint64,
127
- :f_syncwrites, :uint64,
128
- :f_asyncwrites, :uint64,
129
- :f_syncreads, :uint64,
130
- :f_asyncreads, :uint64,
131
- :f_spare, [:uint64, 10],
132
- :f_namemax, :uint32,
133
- :f_owner, :int32,
134
- :f_fsid, [:int32, 2],
135
- :f_charspare, [:char, 80],
136
- :f_fstypename, [:char, 16],
137
- :f_mntfromname, [:char, 88],
138
- :f_mntonname, [:char, 88]
139
- )
140
- else
141
- layout(
142
- :f_bsize, :uint32,
143
- :f_iosize, :int32,
144
- :f_blocks, :uint64,
145
- :f_bfree, :uint64,
146
- :f_bavail, :uint64,
147
- :f_files, :uint64,
148
- :f_ffree, :uint64,
149
- :f_fsid, [:int32, 2],
150
- :f_owner, :int32,
151
- :f_type, :uint32,
152
- :f_flags, :uint32,
153
- :f_fssubtype, :uint32,
154
- :f_fstypename, [:char, 16],
155
- :f_mntonname, [:char, 1024],
156
- :f_mntfromname, [:char, 1024],
157
- :f_reserved, [:uint32, 8]
158
- )
159
- end
160
- end
161
-
162
- # The Statvfs struct represents struct statvfs from sys/statvfs.h.
163
- class Statvfs < FFI::Struct
164
- if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
165
- layout(
166
- :f_bsize, :ulong,
167
- :f_frsize, :ulong,
168
- :f_blocks, :uint,
169
- :f_bfree, :uint,
170
- :f_bavail, :uint,
171
- :f_files, :uint,
172
- :f_ffree, :uint,
173
- :f_favail, :uint,
174
- :f_fsid, :ulong,
175
- :f_flag, :ulong,
176
- :f_namemax, :ulong
177
- )
178
- elsif RbConfig::CONFIG['host'] =~ /bsd/i
179
- layout(
180
- :f_bavail, :uint64,
181
- :f_bfree, :uint64,
182
- :f_blocks, :uint64,
183
- :f_favail, :uint64,
184
- :f_ffree, :uint64,
185
- :f_files, :uint64,
186
- :f_bsize, :ulong,
187
- :f_flag, :ulong,
188
- :f_frsize, :ulong,
189
- :f_fsid, :ulong,
190
- :f_namemax, :ulong
191
- )
192
- elsif RbConfig::CONFIG['host'] =~ /sunos|solaris/i
193
- layout(
194
- :f_bsize, :ulong,
195
- :f_frsize, :ulong,
196
- :f_blocks, :uint64_t,
197
- :f_bfree, :uint64_t,
198
- :f_bavail, :uint64_t,
199
- :f_files, :uint64_t,
200
- :f_ffree, :uint64_t,
201
- :f_favail, :uint64_t,
202
- :f_fsid, :ulong,
203
- :f_basetype, [:char, 16],
204
- :f_flag, :ulong,
205
- :f_namemax, :ulong,
206
- :f_fstr, [:char, 32],
207
- :f_filler, [:ulong, 16]
208
- )
209
- else
210
- layout(
211
- :f_bsize, :ulong,
212
- :f_frsize, :ulong,
213
- :f_blocks, :ulong,
214
- :f_bfree, :ulong,
215
- :f_bavail, :ulong,
216
- :f_files, :ulong,
217
- :f_ffree, :ulong,
218
- :f_favail, :ulong,
219
- :f_fsid, :ulong,
220
- :f_flag, :ulong,
221
- :f_namemax, :ulong,
222
- :f_ftype, :ulong,
223
- :f_basetype, [:char, 16],
224
- :f_str, [:char, 16]
225
- )
226
- end
227
- end
228
-
229
- # The Mnttab struct represents struct mnnttab from sys/mnttab.h on Solaris.
230
- class Mnttab < FFI::Struct
231
- layout(
232
- :mnt_special, :string,
233
- :mnt_mountp, :string,
234
- :mnt_fstype, :string,
235
- :mnt_mntopts, :string,
236
- :mnt_time, :string
237
- )
238
- end
239
-
240
- # The Mntent struct represents struct mntent from sys/mount.h on Unix.
241
- class Mntent < FFI::Struct
242
- layout(
243
- :mnt_fsname, :string,
244
- :mnt_dir, :string,
245
- :mnt_type, :string,
246
- :mnt_opts, :string,
247
- :mnt_freq, :int,
248
- :mnt_passno, :int
249
- )
250
- end
251
-
252
- public
253
-
254
- # The error raised if any of the Filesystem methods fail.
255
- class Error < StandardError; end
256
-
257
- # Stat objects are returned by the Sys::Filesystem.stat method.
258
- class Stat
259
- # Read-only filesystem
260
- RDONLY = 1
261
-
262
- # Filesystem does not support suid or sgid semantics.
263
- NOSUID = 2
264
-
265
- # Filesystem does not truncate file names longer than +name_max+.
266
- NOTRUNC = 3
267
-
268
- # The path of the filesystem.
269
- attr_accessor :path
270
-
271
- # The preferred system block size.
272
- attr_accessor :block_size
273
-
274
- # The fragment size, i.e. fundamental filesystem block size.
275
- attr_accessor :fragment_size
276
-
277
- # The total number of +fragment_size+ blocks in the filesystem.
278
- attr_accessor :blocks
279
-
280
- # The total number of free blocks in the filesystem.
281
- attr_accessor :blocks_free
282
-
283
- # The number of free blocks available to unprivileged processes.
284
- attr_accessor :blocks_available
285
-
286
- # The total number of files/inodes that can be created.
287
- attr_accessor :files
288
-
289
- # The total number of files/inodes on the filesystem.
290
- attr_accessor :files_free
291
-
292
- # The number of free files/inodes available to unprivileged processes.
293
- attr_accessor :files_available
294
-
295
- # The filesystem identifier.
296
- attr_accessor :filesystem_id
297
-
298
- # A bit mask of flags.
299
- attr_accessor :flags
300
-
301
- # The maximum length of a file name permitted on the filesystem.
302
- attr_accessor :name_max
303
-
304
- # The filesystem type, e.g. UFS.
305
- attr_accessor :base_type
306
-
307
- alias inodes files
308
- alias inodes_free files_free
309
- alias inodes_available files_available
310
-
311
- # Creates a new Sys::Filesystem::Stat object. This is meant for
312
- # internal use only. Do not instantiate directly.
313
- #
314
- def initialize
315
- @path = nil
316
- @block_size = nil
317
- @fragment_size = nil
318
- @blocks = nil
319
- @blocks_free = nil
320
- @blocks_available = nil
321
- @files = nil
322
- @files_free = nil
323
- @files_available = nil
324
- @filesystem_id = nil
325
- @flags = nil
326
- @name_max = nil
327
- @base_type = nil
328
- end
329
- end
330
-
331
- # Mount objects are returned by the Sys::Filesystem.mounts method.
332
- class Mount
333
- # The name of the mounted resource.
334
- attr_accessor :name
335
-
336
- # The mount point/directory.
337
- attr_accessor :mount_point
338
-
339
- # The type of filesystem mount, e.g. ufs, nfs, etc.
340
- attr_accessor :mount_type
341
-
342
- # A list of comma separated options for the mount, e.g. nosuid, etc.
343
- attr_accessor :options
344
-
345
- # The time the filesystem was mounted. May be nil.
346
- attr_accessor :mount_time
347
-
348
- # The dump frequency in days. May be nil.
349
- attr_accessor :dump_frequency
350
-
351
- # The pass number of the filessytem check. May be nil.
352
- attr_accessor :pass_number
353
-
354
- alias fsname name
355
- alias dir mount_point
356
- alias opts options
357
- alias passno pass_number
358
- alias freq dump_frequency
359
-
360
- # Creates a Sys::Filesystem::Mount object. This is meant for internal
361
- # use only. Do no instantiate directly.
362
- #
363
- def initialize
364
- @name = nil
365
- @mount_point = nil
366
- @mount_type = nil
367
- @options = nil
368
- @mount_time = nil
369
- @dump_frequency = nil
370
- @pass_number = nil
371
- end
372
- end
373
-
374
- # Returns a Sys::Filesystem::Stat object containing information about the
375
- # +path+ on the filesystem.
376
- #
377
- def self.stat(path)
378
- fs = Statvfs.new
379
-
380
- if statvfs(path, fs) < 0
381
- raise Error, 'statvfs() function failed: ' + strerror(FFI.errno)
382
- end
383
-
384
- obj = Sys::Filesystem::Stat.new
385
- obj.path = path
386
- obj.block_size = fs[:f_bsize]
387
- obj.fragment_size = fs[:f_frsize]
388
- obj.blocks = fs[:f_blocks]
389
- obj.blocks_free = fs[:f_bfree]
390
- obj.blocks_available = fs[:f_bavail]
391
- obj.files = fs[:f_files]
392
- obj.files_free = fs[:f_ffree]
393
- obj.files_available = fs[:f_favail]
394
- obj.filesystem_id = fs[:f_fsid]
395
- obj.flags = fs[:f_flag]
396
- obj.name_max = fs[:f_namemax]
397
-
398
- # OSX does things a little differently
399
- if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
400
- obj.block_size /= 256
401
- end
402
-
403
- if fs.members.include?(:f_basetype)
404
- obj.base_type = fs[:f_basetype].to_s
405
- end
406
-
407
- obj.freeze
408
- end
409
-
410
- # In block form, yields a Sys::Filesystem::Mount object for each mounted
411
- # filesytem on the host. Otherwise it returns an array of Mount objects.
412
- #
413
- # Example:
414
- #
415
- # Sys::Filesystem.mounts{ |fs|
416
- # p fs.name # => '/dev/dsk/c0t0d0s0'
417
- # p fs.mount_time # => Thu Dec 11 15:07:23 -0700 2008
418
- # p fs.mount_type # => 'ufs'
419
- # p fs.mount_point # => '/'
420
- # p fs.options # => local, noowner, nosuid
421
- # }
422
- #
423
- def self.mounts
424
- array = block_given? ? nil : []
425
-
426
- if respond_to?(:getmntinfo, true)
427
- buf = FFI::MemoryPointer.new(:pointer)
428
-
429
- num = getmntinfo(buf, 2)
430
-
431
- if num == 0
432
- raise Error, 'getmntinfo() function failed: ' + strerror(FFI.errno)
433
- end
434
-
435
- ptr = buf.get_pointer(0)
436
-
437
- num.times{ |i|
438
- mnt = Statfs.new(ptr)
439
- obj = Sys::Filesystem::Mount.new
440
- obj.name = mnt[:f_mntfromname].to_s
441
- obj.mount_point = mnt[:f_mntonname].to_s
442
- obj.mount_type = mnt[:f_fstypename].to_s
443
-
444
- string = ""
445
- flags = mnt[:f_flags] & MNT_VISFLAGMASK
446
-
447
- @@opt_names.each{ |key,val|
448
- if flags & key > 0
449
- if string.empty?
450
- string << val
451
- else
452
- string << ", #{val}"
453
- end
454
- end
455
- flags &= ~key
456
- }
457
-
458
- obj.options = string
459
-
460
- if block_given?
461
- yield obj.freeze
462
- else
463
- array << obj.freeze
464
- end
465
-
466
- ptr += Statfs.size
467
- }
468
- else
469
- begin
470
- if respond_to?(:setmntent, true)
471
- fp = setmntent(MOUNT_FILE, 'r')
472
- else
473
- fp = fopen(MOUNT_FILE, 'r')
474
- end
475
-
476
- if RbConfig::CONFIG['host_os'] =~ /sunos|solaris/i
477
- mt = Mnttab.new
478
- while getmntent(fp, mt) == 0
479
- obj = Sys::Filesystem::Mount.new
480
- obj.name = mt[:mnt_special].to_s
481
- obj.mount_point = mt[:mnt_mountp].to_s
482
- obj.mount_type = mt[:mnt_fstype].to_s
483
- obj.options = mt[:mnt_mntopts].to_s
484
- obj.mount_time = Time.at(Integer(mt[:mnt_time]))
485
-
486
- if block_given?
487
- yield obj.freeze
488
- else
489
- array << obj.freeze
490
- end
491
- end
492
- else
493
- while ptr = getmntent(fp)
494
- break if ptr.null?
495
- mt = Mntent.new(ptr)
496
-
497
- obj = Sys::Filesystem::Mount.new
498
- obj.name = mt[:mnt_fsname]
499
- obj.mount_point = mt[:mnt_dir]
500
- obj.mount_type = mt[:mnt_type]
501
- obj.options = mt[:mnt_opts]
502
- obj.mount_time = nil
503
- obj.dump_frequency = mt[:mnt_freq]
504
- obj.pass_number = mt[:mnt_passno]
505
-
506
- if block_given?
507
- yield obj.freeze
508
- else
509
- array << obj.freeze
510
- end
511
- end
512
- end
513
- ensure
514
- if fp && !fp.null?
515
- if respond_to?(:endmntent, true)
516
- endmntent(fp)
517
- else
518
- fclose(fp)
519
- end
520
- end
521
- end
522
- end
523
-
524
- array
525
- end
526
-
527
- # Returns the mount point of the given +file+, or itself if it cannot
528
- # be found.
529
- #
530
- # Example:
531
- #
532
- # Sys::Filesystem.mount_point('/home/some_user') # => /home
533
- #
534
- def self.mount_point(file)
535
- dev = File.stat(file).dev
536
- val = file
537
-
538
- self.mounts.each{ |mnt|
539
- mp = mnt.mount_point
540
- if File.stat(mp).dev == dev
541
- val = mp
542
- break
543
- end
544
- }
545
-
546
- val
547
- end
548
- end
549
- end
550
-
551
- class Fixnum
552
- # Converts a number to kilobytes.
553
- def to_kb
554
- self / 1024
555
- end
556
-
557
- # Converts a number to megabytes.
558
- def to_mb
559
- self / 1048576
560
- end
561
-
562
- # Converts a number to gigabytes.
563
- def to_gb
564
- self / 1073741824
565
- end
566
- end
@@ -1,36 +0,0 @@
1
- require 'ffi'
2
-
3
- module Sys
4
- class Filesystem
5
- module Functions
6
- extend FFI::Library
7
- ffi_lib :kernel32
8
-
9
- # Make FFI functions private
10
- module FFI::Library
11
- def attach_pfunc(*args)
12
- attach_function(*args)
13
- private args[0]
14
- end
15
- end
16
-
17
- attach_pfunc :GetDiskFreeSpaceW, [:buffer_in, :pointer, :pointer, :pointer, :pointer], :bool
18
- attach_pfunc :GetDiskFreeSpaceExW, [:buffer_in, :pointer, :pointer, :pointer], :bool
19
- attach_pfunc :GetLogicalDriveStringsA, [:ulong, :pointer], :ulong
20
-
21
- attach_pfunc :GetVolumeInformationA,
22
- [:buffer_in, :pointer, :ulong, :pointer, :pointer, :pointer, :pointer, :ulong],
23
- :bool
24
-
25
- attach_pfunc :GetVolumeInformationW,
26
- [:buffer_in, :pointer, :ulong, :pointer, :pointer, :pointer, :pointer, :ulong],
27
- :bool
28
-
29
- attach_pfunc :QueryDosDeviceA, [:buffer_in, :buffer_out, :ulong], :ulong
30
-
31
- ffi_lib :shlwapi
32
-
33
- attach_pfunc :PathStripToRootW, [:pointer], :bool
34
- end
35
- end
36
- end
@@ -1,7 +0,0 @@
1
- class String
2
- # Convenience method for converting strings to UTF-16LE for wide character
3
- # functions that require it.
4
- def wincode
5
- (self.tr(File::SEPARATOR, File::ALT_SEPARATOR) + 0.chr).encode('UTF-16LE')
6
- end
7
- end
File without changes
@@ -1,7 +0,0 @@
1
- $LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__))
2
-
3
- if File::ALT_SEPARATOR
4
- require 'test_sys_filesystem_windows'
5
- else
6
- require 'test_sys_filesystem_unix'
7
- end