rbmount 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of rbmount.
5
+ #
6
+ # rbmount is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # rbmount is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with rbmount. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'rbmount/c'
21
+
22
+ module Mount
23
+ class Iterator
24
+ def initialize (direction)
25
+ @pointer = Mount::C.mnt_new_iter(direction)
26
+ raise if @pointer.null?
27
+
28
+ ObjectSpace.define_finalizer(self, method(:finalize))
29
+ end
30
+
31
+ def direction
32
+ Mount::C.mnt_iter_get_direction(@pointer)
33
+ end
34
+
35
+ def reset
36
+ Mount::C.mnt_reset_iter(@pointer)
37
+ self
38
+ end
39
+
40
+ def finalize (id=nil)
41
+ Mount::C.mnt_free_iter(@pointer)
42
+ end
43
+
44
+ def to_c
45
+ @pointer
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,51 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of rbmount.
5
+ #
6
+ # rbmount is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # rbmount is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with rbmount. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'rbmount/c'
21
+
22
+ module Mount
23
+ class Lock
24
+ def initialize (datafile, id)
25
+ @pointer = Mount::C.mnt_new_lock(datafile, id)
26
+ raise unless @pointer
27
+
28
+ ObjectSpace.define_finalizer(self, method(:finalize))
29
+ end
30
+
31
+ def lock_file
32
+ Mount::C.mnt_lock_file(@pointer)
33
+ end
34
+
35
+ def unlock_file
36
+ Mount::C.mnt_unlock_file(@pointer)
37
+ end
38
+
39
+ def block_signals
40
+ Mount::C.mnt_lock_block_signals(@pointer)
41
+ end
42
+
43
+ def finalize
44
+ Mount::C.mnt_free_lock(@pointer)
45
+ end
46
+
47
+ def to_c
48
+ @pointer
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,103 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of rbmount.
5
+ #
6
+ # rbmount is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # rbmount is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with rbmount. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'rbmount/c'
21
+
22
+ module Mount
23
+ MNT_MS_COMMENT = (1 << 8)
24
+ MNT_MS_GROUP = (1 << 6)
25
+ MNT_MS_HELPER = (1 << 12)
26
+ MNT_MS_LOOP = (1 << 9)
27
+ MNT_MS_NETDEV = (1 << 7)
28
+ MNT_MS_NOAUTO = (1 << 2)
29
+ MNT_MS_NOFAIL = (1 << 10)
30
+ MNT_MS_OFFSET = (1 << 14)
31
+ MNT_MS_OWNER = (1 << 5)
32
+ MNT_MS_SIZELIMIT = (1 << 15)
33
+ MNT_MS_UHELPER = (1 << 11)
34
+ MNT_MS_USER = (1 << 3)
35
+ MNT_MS_USERS = (1 << 4)
36
+ MNT_MS_XCOMMENT = (1 << 13)
37
+ MS_BIND = 0x1000
38
+ MS_DIRSYNC = 128
39
+ MS_I_VERSION = (1<<23)
40
+ MS_MANDLOCK = 64
41
+ MS_MGC_MSK = 0xffff0000
42
+ MS_MGC_VAL = 0xC0ED0000
43
+ MS_MOVE = 0x2000
44
+ MS_NOATIME = 0x400
45
+ MS_NODEV = 4
46
+ MS_NODIRATIME = 0x800
47
+ MS_NOEXEC = 8
48
+ MS_NOSUID = 2
49
+ MS_PRIVATE = (1<<18)
50
+ MS_SHARED = (1<<20)
51
+ MS_RDONLY = 1
52
+ MS_REC = 0x4000
53
+ MS_RELATIME = 0x200000
54
+ MS_REMOUNT = 32
55
+ MS_SILENT = 0x8000
56
+ MS_SLAVE = (1<<19)
57
+ MS_STRICTATIME = (1<<24)
58
+ MS_SYNCHRONOUS = 16
59
+ MS_UNBINDABLE = (1<<17)
60
+ MS_PROPAGATION = (MS_SHARED|MS_SLAVE|MS_UNBINDABLE|MS_PRIVATE)
61
+ MS_SECURE = (MS_NOEXEC|MS_NOSUID|MS_NODEV)
62
+ MS_OWNERSECURE = (MS_NOSUID|MS_NODEV)
63
+
64
+ MNT_INVERT = (1 << 1)
65
+ MNT_NOMTAB = (1 << 2)
66
+ MNT_PREFIX = (1 << 3)
67
+
68
+ MNT_ITER_FORWARD = 0
69
+ MNT_ITER_BACKWARD = 1
70
+
71
+ VERSION = "2.19.0"
72
+
73
+ class OptMap < Struct.new(:name, :id, :mask)
74
+ end
75
+
76
+
77
+ def self.fs_type (devname, amb=false)
78
+ ambi = amb ? FFI::MemoryPointer.new(:int) : nil
79
+ fs = Mount::C.mnt_get_fstype(devname, ambi, nil)
80
+
81
+ amb ? [fs, ambi.read_int] : fs
82
+ end
83
+
84
+ def self.pretty_path (devname)
85
+ Mount::C.mnt_pretty_path(devname, nil)
86
+ end
87
+
88
+ def self.resolve_path (path)
89
+ Mount::C.mnt_resolve_path(path, nil)
90
+ end
91
+
92
+ def self.resolve_tag (token, value)
93
+ Mount::C.mnt_resolve_tag(token, value, nil)
94
+ end
95
+
96
+ def self.init_debug (mask)
97
+ Mount::C.mnt_init_debug(mask)
98
+ end
99
+
100
+ def self.builtin_optmap (id)
101
+ Mount::C.mnt_get_builtin_optmap(1).read_array_of_libmnt_optmap
102
+ end
103
+ end
@@ -0,0 +1,166 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of rbmount.
5
+ #
6
+ # rbmount is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # rbmount is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with rbmount. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'rbmount/c'
21
+
22
+ module Mount
23
+ module OptionString
24
+ class << self
25
+ def append_option (optstr=nil, name, value)
26
+ sptr = optstr.is_a?(::String) ? FFI::MemoryPointer.from_string(optstr) : FFI::MemoryPointer.new(:string)
27
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(sptr)
28
+
29
+ raise unless Mount::C.mnt_optstr_append_option(ptr, name, value)
30
+ ptr.read_pointer.read_string
31
+ end
32
+
33
+ def apply_flags (optstr=nil, flags, map)
34
+ sptr = optstr.is_a?(::String) ? FFI::MemoryPointer.from_string(optstr) : FFI::MemoryPointer.new(:string)
35
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(sptr)
36
+
37
+ raise unless Mount::C.mnt_optstr_apply_flags(ptr, flags, FFI::MemoryPointer.from_array_of_libmnt_optmap(map))
38
+ ptr.read_pointer.read_string
39
+ end
40
+
41
+ def flags (optstr, map)
42
+ optstr = FFI::MemoryPointer.new(:string).write_string(optstr)
43
+ ptr = FFI::MemoryPointer.new(:ulong)
44
+
45
+ raise unless Mount::C.mnt_optstr_get_flags(optstr, ptr, FFI::MemoryPointer.from_array_of_libmnt_optmap(map))
46
+ ptr.read_ulong
47
+ end
48
+
49
+ def option (optstr, name)
50
+ ptr = FFI::MemoryPointer.new(:pointer)
51
+ ol = FFI::MemoryPointer.new(:ulong)
52
+
53
+ raise unless Mount::C.mnt_optstr_get_option(optstr, name, ptr, ol)
54
+ ptr.read_pointer.read_string[0, ol.read_ulong]
55
+ end
56
+
57
+ def options (optstr, map, ignore=0)
58
+ ptr = FFI::MemoryPointer.new(:pointer)
59
+
60
+ raise unless Mount::C.mnt_optstr_get_options(optstr, ptr, FFI::MemoryPointer.from_array_of_libmnt_optmap(map), ignore)
61
+ ptr.read_pointer.read_string
62
+ end
63
+
64
+ def each_option (optstr)
65
+ optstr = FFI::MemoryPointer.new(:pointer).write_pointer(FFI::MemoryPointer.from_string(optstr))
66
+
67
+ Enumerator.new {|y|
68
+ loop {
69
+ res = Mount::String.next_option(optstr)
70
+ break unless res
71
+ y << res
72
+ }
73
+ }.each {|args|
74
+ yield *args if block_given?
75
+ }
76
+ end
77
+
78
+ def prepend_option (optstr=nil, name, value)
79
+ sptr = optstr.is_a?(::String) ? FFI::MemoryPointer.from_string(optstr) : FFI::MemoryPointer.new(:string)
80
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(sptr)
81
+
82
+ raise unless Mount::C.mnt_optstr_prepend_option(ptr, name, value)
83
+ ptr.read_pointer.read_string
84
+ end
85
+
86
+ def remove_option (optstr=nil, name)
87
+ sptr = optstr.is_a?(::String) ? FFI::MemoryPointer.from_string(optstr) : FFI::MemoryPointer.new(:string)
88
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(sptr)
89
+
90
+ raise unless Mount::C.mnt_optstr_remove_option(ptr, name)
91
+ ptr.read_pointer.read_string
92
+ end
93
+
94
+ def set_option (optstr, name, value=nil)
95
+ sptr = optstr.is_a?(::String) ? FFI::MemoryPointer.from_string(optstr) : FFI::MemoryPointer.new(:string)
96
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(sptr)
97
+
98
+ raise unless Mount::C.mnt_optstr_set_option(ptr, name, value)
99
+ ptr.read_pointer.read_string
100
+ end
101
+
102
+ def split_optstr (optstr, ignore_user=0, ignore_vfs=0)
103
+ pi = (1..3).map { FFI::MemoryPointer.new(:pointer).write_pointer(FFI::MemoryPointer.new(:string)) }
104
+ raise unless Mount::C.mnt_split_optstr(optstr, *pi, ignore_user, ignore_vfs)
105
+ pi.map {|x| x.read_pointer.read_string }
106
+ end
107
+
108
+ protected
109
+ def next_option (optstr)
110
+ name = FFI::MemoryPointer.new(:pointer)
111
+ ns = FFI::MemoryPointer.new(:ulong)
112
+ value = FFI::MemoryPointer.new(:pointer)
113
+ vs = FFI::MemoryPointer.new(:ulong)
114
+
115
+ case Mount::C.mnt_optstr_next_option(optstr, name, ns, value, vs)
116
+ when true
117
+ [[name, ns], [value, vs]].map {|x, i| x.read_pointer.read_string[0, i.read_ulong] }
118
+ when false
119
+ raise
120
+ else
121
+ nil
122
+ end
123
+ end
124
+ end
125
+
126
+ def append_option (name, value)
127
+ self.replace(Mount::OptionString.append_option(self, name, value))
128
+ end
129
+
130
+ def apply_flags (flags, map)
131
+ self.replace(Mount::OptionString.apply_flags(self, flags, map))
132
+ end
133
+
134
+ def flags (map)
135
+ Mount::OptionString.flags(self, map)
136
+ end
137
+
138
+ def option (name)
139
+ Mount::OptionString.option(self, name)
140
+ end
141
+
142
+ def options (map, ignore=0)
143
+ Mount::OptionString.options(self, map, ignore)
144
+ end
145
+
146
+ def each_option (&blk)
147
+ Mount::OptionString.each_option(self, &blk)
148
+ end
149
+
150
+ def prepend_option (name, value)
151
+ self.replace(Mount::OptionString.prepend_option(self, name, value))
152
+ end
153
+
154
+ def remove_option (name)
155
+ self.replace(Mount::OptionString.remove_option(self, name))
156
+ end
157
+
158
+ def set_option (name, value=nil)
159
+ self.replace(Mount::OptionString.set_option(self, name, value))
160
+ end
161
+
162
+ def split_optstr (ignore_user=0, ignore_vfs=0)
163
+ Mount::OptionString.split_optstr(self, ignore_user, ignore_vfs)
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,188 @@
1
+ #--
2
+ # Copyleft shura. [ shura1991@gmail.com ]
3
+ #
4
+ # This file is part of rbmount.
5
+ #
6
+ # rbmount is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # rbmount is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with rbmount. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'rbmount/c'
21
+ require 'rbmount/iterator'
22
+ require 'rbmount/cache'
23
+ require 'rbmount/fs'
24
+
25
+ module Mount
26
+ class Table
27
+ DIRECTION = Hash.new {|*|
28
+ :forward
29
+ }.merge({
30
+ forward: Mount::MNT_ITER_FORWARD,
31
+ 'forward' => Mount::MNT_ITER_FORWARD,
32
+ backward: Mount::MNT_ITER_BACKWARD,
33
+ 'backward' => Mount::MNT_ITER_BACKWARD,
34
+
35
+ Mount::MNT_ITER_FORWARD => Mount::MNT_ITER_FORWARD,
36
+ Mount::MNT_ITER_BACKWARD => Mount::MNT_ITER_BACKWARD,
37
+ }).freeze
38
+
39
+ def initialize (path=nil)
40
+ @pointer = Mount::C.mnt_new_table unless path
41
+ if File.directory?(path)
42
+ @pointer = Mount::C.mnt_new_table_from_dir(path)
43
+ elsif File.file?(path)
44
+ @pointer = Mount::C.mnt_new_table_from_file(path)
45
+ else
46
+ raise ArgumentError
47
+ end
48
+
49
+ raise if !@pointer or @pointer.null?
50
+
51
+ ObjectSpace.define_finalizer(self, method(:finalize))
52
+ end
53
+
54
+ def add_fs (fs)
55
+ Mount::C.mnt_table_add_fs(@pointer, fs.to_c)
56
+ end
57
+
58
+ def next_fs (direction=:forward)
59
+ Mount::FS.new.tap {|fs|
60
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(fs.to_c)
61
+
62
+ res = Mount::C.mnt_table_next_fs(@pointer, Mount::Iter.new(DIRECTION[direction]).to_c, ptr)
63
+ raise unless res
64
+ break nil if res == 1
65
+ }
66
+ end
67
+
68
+ def each_fs (direction=:forward, &blk)
69
+ Enumerator.new {|y|
70
+ loop {
71
+ res = next_fs(direction)
72
+ break unless res
73
+ y << res
74
+ }
75
+ }.each(&blk)
76
+ end
77
+
78
+ def find_pair (source, target, direction=:forward)
79
+ Mount::FS.new(Mount::C.mnt_table_find_pair(@pointer, source, target, DIRECTION[direction]))
80
+ end
81
+
82
+ def find_source (source, direction=:forward)
83
+ Mount::FS.new(Mount::C.mnt_table_find_source(@pointer, source, DIRECTION[direction]))
84
+ end
85
+
86
+ def find_srcpath (path, direction=:forward)
87
+ Mount::FS.new(Mount::C.mnt_table_find_srcpath(@pointer, source, DIRECTION[direction]))
88
+ end
89
+
90
+ def find_tag (tag, val, direction=:forward)
91
+ Mount::FS.new(Mount::C.mnt_table_find_tag(@pointer, tag, val, DIRECTION[direction]))
92
+ end
93
+
94
+ def find_target (path, direction=:forward)
95
+ Mount::FS.new(Mount::C.mnt_table_find_target(@pointer, path, DIRECTION[direction]))
96
+ end
97
+
98
+ def cache
99
+ Mount::Cache.new(Mount::C.mnt_table_get_cache(@pointer))
100
+ end
101
+
102
+ def name
103
+ Mount::C.mnt_table_get_name(@pointer)
104
+ end
105
+
106
+ def nents
107
+ Mount::C.mnt_table_get_nents(@pointer)
108
+ end
109
+
110
+ def root_fs
111
+ Mount::FS.new.tap {|fs|
112
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(fs.to_c)
113
+ raise unless Mount::C.mnt_table_get_root_fs(@pointer, ptr)
114
+ }
115
+ end
116
+
117
+ def fs_mounted?(fstab_fs)
118
+ Mount::C.mnt_table_is_fs_mounted(@pointer, fstab_fs.to_c)
119
+ end
120
+
121
+ def next_child_fs (parent, direction=:forward)
122
+ Mount::FS.new.tap {|fs|
123
+ ptr = FFI::MemoryPointer.new(:pointer).write_pointer(fs.to_c)
124
+
125
+ res = Mount::C.mnt_table_next_child_fs(@pointer, Mount::Iter.new(DIRECTION[direction]).to_c,
126
+ parent.to_c, ptr)
127
+ raise unless res
128
+ break nil if res == 1
129
+ }
130
+ end
131
+
132
+ def each_child_fs (parent, direction=:forward, &blk)
133
+ Enumerator.new {|y|
134
+ loop {
135
+ res = next_child_fs(parent, direction)
136
+ break unless res
137
+ y << res
138
+ }
139
+ }.each(&blk)
140
+ end
141
+
142
+ def parse_file (path)
143
+ Mount::C.mnt_table_parse_file(@pointer, path)
144
+ end
145
+
146
+ def parse_fstab (path=nil)
147
+ Mount::C.mnt_table_parse_fstab(@pointer, path)
148
+ end
149
+
150
+ def parse_mtab (path=nil)
151
+ Mount::C.mnt_table_parse_mtab(@pointer, path)
152
+ end
153
+
154
+ def parse_stream (io, filename)
155
+ Mount::C.mnt_table_parse_stream(@pointer, io, filename)
156
+ end
157
+
158
+ def remove_fs (fs)
159
+ Mount::C.mnt_table_remove_fs(@pointer, fs.to_c)
160
+ end
161
+
162
+ def cache= (mpc)
163
+ Mount::C.mnt_table_set_cache(@pointer, mpc.to_c)
164
+ end
165
+
166
+ def set_iter (fs, direction=:forward)
167
+ Mount::C.mnt_table_set_iter(@pointer, Mount::Iter.new(DIRECTION[direction]).to_c, fs.to_c)
168
+ end
169
+
170
+ def on_error (&blk)
171
+ if blk.arity == 3
172
+ Mount::C.mnt_table_set_iter(@pointer, blk)
173
+ true
174
+ else
175
+ false
176
+ end
177
+ end
178
+
179
+ def reset!
180
+ raise unless Mount::C.mnt_reset_table(@pointer)
181
+ self
182
+ end
183
+
184
+ def finalize
185
+ Mount::C.mnt_free_table(@pointer)
186
+ end
187
+ end
188
+ end