rubypath 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +165 -0
- data/README.md +29 -0
- data/doc/file.README.html +111 -0
- data/lib/rubypath/backend/mock.rb +315 -0
- data/lib/rubypath/backend/sys.rb +139 -0
- data/lib/rubypath/backend.rb +87 -0
- data/lib/rubypath/comparison.rb +22 -0
- data/lib/rubypath/construction.rb +109 -0
- data/lib/rubypath/dir_operations.rb +76 -0
- data/lib/rubypath/extensions.rb +157 -0
- data/lib/rubypath/file_operations.rb +164 -0
- data/lib/rubypath/file_predicates.rb +32 -0
- data/lib/rubypath/identity.rb +59 -0
- data/lib/rubypath/io_operations.rb +82 -0
- data/lib/rubypath/mock.rb +42 -0
- data/lib/rubypath/path_operations.rb +253 -0
- data/lib/rubypath/path_predicates.rb +61 -0
- data/lib/rubypath/version.rb +11 -0
- data/lib/rubypath.rb +28 -0
- data/rubypath.gemspec +22 -0
- data/spec/rubypath/comparison_spec.rb +46 -0
- data/spec/rubypath/construction_spec.rb +101 -0
- data/spec/rubypath/dir_operations_spec.rb +146 -0
- data/spec/rubypath/extensions_spec.rb +270 -0
- data/spec/rubypath/file_operations_spec.rb +385 -0
- data/spec/rubypath/file_predicates_spec.rb +66 -0
- data/spec/rubypath/identity_spec.rb +21 -0
- data/spec/rubypath/io_operations_spec.rb +126 -0
- data/spec/rubypath/path_operations_spec.rb +383 -0
- data/spec/rubypath/path_predicates_spec.rb +75 -0
- data/spec/spec_helper.rb +42 -0
- data/spec/support/describe_method.rb +18 -0
- data/spec/support/with_backend.rb +31 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9897ff8546e2a25f86e5a9feb820d2462755153
|
4
|
+
data.tar.gz: 0d8c6cc18c678673c599aa6b45e316039d1c16b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f95207874afd05e04c4878c4fb7df9e55f0e4548336551ebfdcbe31b103d830e6340861ab4f1fe3a2d5e06e8d5b776c4b7404cf771e269ac54549227fb9c50a5
|
7
|
+
data.tar.gz: 9ea4c73e67e6361c4befa3ec2e37227241f80373631e5e380133a26fbc0f12ff32b78265e21bdbf950e335ecbd08dbe8bb991f49ad57d9d46397012edee2b55f
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Ruby Path
|
2
|
+
|
3
|
+
*Ruby Path* introduces a global `Path` class unifying most `File`, `Dir`, `FileUtils`, `Pathname` and `IO` operations with a flexible and powerful Object-Interface and still adding new useful methods and functions like mocking a while file system for fast and reliable testing.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add `rubypath` to your Gemfile, `gemspec` or install manually.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
TODO
|
12
|
+
|
13
|
+
## Contributing
|
14
|
+
|
15
|
+
1. Fork it
|
16
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
17
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
18
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
19
|
+
5. Create new Pull Request
|
20
|
+
|
21
|
+
## License
|
22
|
+
|
23
|
+
Copyright (C) 2014 Jan Graichen
|
24
|
+
|
25
|
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
26
|
+
|
27
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
28
|
+
|
29
|
+
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
@@ -0,0 +1,111 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
6
|
+
<title>
|
7
|
+
File: README
|
8
|
+
|
9
|
+
— Documentation by YARD 0.8.7.3
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
hasFrames = window.top.frames.main ? true : false;
|
19
|
+
relpath = '';
|
20
|
+
framesUrl = "frames.html#!" + escape(window.location.href);
|
21
|
+
</script>
|
22
|
+
|
23
|
+
|
24
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
25
|
+
|
26
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
27
|
+
|
28
|
+
|
29
|
+
</head>
|
30
|
+
<body>
|
31
|
+
<div id="header">
|
32
|
+
<div id="menu">
|
33
|
+
|
34
|
+
<a href="_index.html">Index</a> »
|
35
|
+
<span class="title">File: README</span>
|
36
|
+
|
37
|
+
|
38
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
39
|
+
</div>
|
40
|
+
|
41
|
+
<div id="search">
|
42
|
+
|
43
|
+
<a class="full_list_link" id="class_list_link"
|
44
|
+
href="class_list.html">
|
45
|
+
Class List
|
46
|
+
</a>
|
47
|
+
|
48
|
+
<a class="full_list_link" id="method_list_link"
|
49
|
+
href="method_list.html">
|
50
|
+
Method List
|
51
|
+
</a>
|
52
|
+
|
53
|
+
<a class="full_list_link" id="file_list_link"
|
54
|
+
href="file_list.html">
|
55
|
+
File List
|
56
|
+
</a>
|
57
|
+
|
58
|
+
</div>
|
59
|
+
<div class="clear"></div>
|
60
|
+
</div>
|
61
|
+
|
62
|
+
<iframe id="search_frame"></iframe>
|
63
|
+
|
64
|
+
<div id="content"><div id='filecontents'><h1>Ruby::Path</h1>
|
65
|
+
|
66
|
+
<p><em>Ruby::Path</em> introduces a global <code>Path</code> class unifying most <code>File</code>, <code>Dir</code>, <code>FileUtils</code>, <code>Pathname</code> and <code>IO</code> operations with a flexible and powerful Object-Interface and still adding new useful methods and functions like mocking a while file system for fast and reliable testing.</p>
|
67
|
+
|
68
|
+
<h2>Installation</h2>
|
69
|
+
|
70
|
+
<p>Add <code>ruby-path</code> to your Gemfile, <code>gemspec</code> or install manually.</p>
|
71
|
+
|
72
|
+
<h2>Usage</h2>
|
73
|
+
|
74
|
+
<p>TODO</p>
|
75
|
+
|
76
|
+
<h2>Contributing</h2>
|
77
|
+
|
78
|
+
<ol>
|
79
|
+
<li>Fork it</li>
|
80
|
+
<li>Create your feature branch (<code>git checkout -b my-new-feature</code>)</li>
|
81
|
+
<li>Commit your changes (<code>git commit -am 'Add some feature'</code>)</li>
|
82
|
+
<li>Push to the branch (<code>git push origin my-new-feature</code>)</li>
|
83
|
+
<li>Create new Pull Request</li>
|
84
|
+
</ol>
|
85
|
+
|
86
|
+
<h2>License</h2>
|
87
|
+
|
88
|
+
<p>Copyright (C) 2014 Jan Graichen</p>
|
89
|
+
|
90
|
+
<p>This program is free software: you can redistribute it and/or modify
|
91
|
+
it under the terms of the GNU General Public License as published by
|
92
|
+
the Free Software Foundation, either version 3 of the License, or
|
93
|
+
(at your option) any later version.</p>
|
94
|
+
|
95
|
+
<p>This program is distributed in the hope that it will be useful,
|
96
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
97
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
98
|
+
GNU General Public License for more details.</p>
|
99
|
+
|
100
|
+
<p>You should have received a copy of the GNU General Public License
|
101
|
+
along with this program. If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.</p>
|
102
|
+
</div></div>
|
103
|
+
|
104
|
+
<div id="footer">
|
105
|
+
Generated on Mon Feb 17 00:33:05 2014 by
|
106
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
107
|
+
0.8.7.3 (ruby-2.1.0).
|
108
|
+
</div>
|
109
|
+
|
110
|
+
</body>
|
111
|
+
</html>
|
@@ -0,0 +1,315 @@
|
|
1
|
+
class Path::Backend
|
2
|
+
|
3
|
+
class Mock
|
4
|
+
attr_reader :user, :homes
|
5
|
+
|
6
|
+
# @!group Virtual File System Configuration
|
7
|
+
|
8
|
+
# Set user that owns the current process.
|
9
|
+
def current_user=(user)
|
10
|
+
@user = user.to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
# Define new home directories. Must be given as has a hash that is
|
14
|
+
# interpreted as a user name to home directory mapping.
|
15
|
+
attr_writer :homes
|
16
|
+
|
17
|
+
# Set current working directory.
|
18
|
+
attr_writer :cwd
|
19
|
+
|
20
|
+
# @!group Internal Methods
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@user = 'root'
|
24
|
+
@homes = {'root' => '/root'}
|
25
|
+
@cwd = '/root'
|
26
|
+
@umask = 0022
|
27
|
+
end
|
28
|
+
|
29
|
+
def home(user)
|
30
|
+
homes.fetch(user){ raise ArgumentError.new "user #{user} doesn't exist" }
|
31
|
+
end
|
32
|
+
|
33
|
+
# @!group Backend Operations
|
34
|
+
|
35
|
+
def expand_path(path, base = getwd)
|
36
|
+
if %r{^~(?<name>[^/]*)(/(?<rest>.*))?$} =~ path
|
37
|
+
::File.expand_path rest.to_s, home(name.empty? ? user : name)
|
38
|
+
else
|
39
|
+
::File.expand_path(path, base)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
alias_method :expand, :expand_path
|
43
|
+
|
44
|
+
def getwd
|
45
|
+
@cwd ||= '/'
|
46
|
+
end
|
47
|
+
|
48
|
+
def file?(path)
|
49
|
+
lookup(path).is_a?(File)
|
50
|
+
end
|
51
|
+
|
52
|
+
def directory?(path)
|
53
|
+
lookup(path).is_a?(Dir)
|
54
|
+
end
|
55
|
+
|
56
|
+
def exists?(path)
|
57
|
+
lookup(path) ? true : false
|
58
|
+
end
|
59
|
+
|
60
|
+
def mkdir(path)
|
61
|
+
return if path.to_s == '/'
|
62
|
+
|
63
|
+
node = lookup_parent! path
|
64
|
+
dir = node.lookup ::File.basename path
|
65
|
+
unless dir.is_a?(Dir)
|
66
|
+
if dir.nil?
|
67
|
+
node.add Dir.new(self, ::File.basename(path))
|
68
|
+
else
|
69
|
+
raise ArgumentError.new \
|
70
|
+
"Node #{dir.path} exists and is no directory."
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def mkpath(path)
|
76
|
+
path = expand_path(path)
|
77
|
+
::Pathname.new(path).descend do |p|
|
78
|
+
mkdir(p.to_s)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def touch(path)
|
83
|
+
node = lookup_parent! path
|
84
|
+
file = node.lookup ::File.basename path
|
85
|
+
if file
|
86
|
+
file.mtime = Time.now
|
87
|
+
else
|
88
|
+
node.add File.new(self, ::File.basename(path))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def write(path, content, *args)
|
93
|
+
node = lookup_parent! path
|
94
|
+
file = node.lookup ::File.basename(path)
|
95
|
+
unless file
|
96
|
+
file = File.new self, ::File.basename(path)
|
97
|
+
node.add file
|
98
|
+
end
|
99
|
+
|
100
|
+
case file
|
101
|
+
when File
|
102
|
+
if args.empty?
|
103
|
+
file.content = content
|
104
|
+
else
|
105
|
+
offset = args[0].to_i
|
106
|
+
file.content[offset, content.length] = content
|
107
|
+
end
|
108
|
+
file.mtime = Time.now
|
109
|
+
when Dir
|
110
|
+
raise Errno::EISDIR.new path
|
111
|
+
else
|
112
|
+
raise ArgumentError.new
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def mtime(path)
|
117
|
+
lookup!(path).mtime
|
118
|
+
end
|
119
|
+
|
120
|
+
def mtime=(path, time)
|
121
|
+
lookup!(path).mtime = time
|
122
|
+
end
|
123
|
+
|
124
|
+
def atime(path)
|
125
|
+
lookup!(path).atime
|
126
|
+
end
|
127
|
+
|
128
|
+
def atime=(path, time)
|
129
|
+
lookup!(path).atime = time
|
130
|
+
end
|
131
|
+
|
132
|
+
def read(path, *args)
|
133
|
+
file = lookup_file!(path)
|
134
|
+
file.atime = Time.now
|
135
|
+
content = file.content
|
136
|
+
if args[0]
|
137
|
+
length = args[0].to_i
|
138
|
+
offset = args[1] ? args[1].to_i : 0
|
139
|
+
content = content.slice(offset, length)
|
140
|
+
end
|
141
|
+
content
|
142
|
+
end
|
143
|
+
|
144
|
+
def entries(path)
|
145
|
+
node = lookup_dir! path
|
146
|
+
node.children.map(&:name) + %w(. ..)
|
147
|
+
end
|
148
|
+
|
149
|
+
def glob(pattern, flags = 0, &block)
|
150
|
+
root.all.select do |node|
|
151
|
+
::File.fnmatch pattern, node.path, (flags | ::File::FNM_PATHNAME)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def get_umask
|
156
|
+
@umask
|
157
|
+
end
|
158
|
+
|
159
|
+
def set_umask(mask)
|
160
|
+
@umask = Integer(mask)
|
161
|
+
end
|
162
|
+
|
163
|
+
def mode(path)
|
164
|
+
lookup!(path).mode
|
165
|
+
end
|
166
|
+
|
167
|
+
# @!group Internal Virtual File System
|
168
|
+
|
169
|
+
# Return root node.
|
170
|
+
def root
|
171
|
+
@root ||= Dir.new(self, '')
|
172
|
+
end
|
173
|
+
|
174
|
+
def to_lookup(path)
|
175
|
+
path = expand path
|
176
|
+
path.sub(/^\/+/, '')
|
177
|
+
end
|
178
|
+
|
179
|
+
def lookup(path)
|
180
|
+
root.lookup to_lookup path
|
181
|
+
end
|
182
|
+
|
183
|
+
def lookup!(path)
|
184
|
+
if (node = lookup(path))
|
185
|
+
node
|
186
|
+
else
|
187
|
+
raise Errno::ENOENT.new path
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def lookup_file!(path)
|
192
|
+
node = lookup! path
|
193
|
+
case node
|
194
|
+
when File
|
195
|
+
node
|
196
|
+
when Dir
|
197
|
+
raise Errno::EISDIR.new path
|
198
|
+
else
|
199
|
+
raise ArgumentError.new "NOT A FILE: #{path}"
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def lookup_dir!(path)
|
204
|
+
if (node = lookup!(path)).is_a?(Dir)
|
205
|
+
node
|
206
|
+
else
|
207
|
+
raise Errno::ENOENT.new path
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def lookup_parent!(path)
|
212
|
+
node = lookup ::File.dirname expand path
|
213
|
+
if node
|
214
|
+
node.is_a?(Dir) ? node : raise(Errno::ENOTDIR.new path)
|
215
|
+
else
|
216
|
+
raise Errno::ENOENT.new path
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
#
|
221
|
+
class Node
|
222
|
+
attr_reader :sys, :name, :parent
|
223
|
+
attr_accessor :mtime, :atime, :mode
|
224
|
+
|
225
|
+
def initialize(backend, name, ops = {})
|
226
|
+
@sys = backend
|
227
|
+
@name = name
|
228
|
+
@mtime = Time.now
|
229
|
+
@atime = Time.now
|
230
|
+
end
|
231
|
+
|
232
|
+
def mtime=(time)
|
233
|
+
if time.is_a?(Time)
|
234
|
+
@mtime = time
|
235
|
+
else
|
236
|
+
raise "Not Time but `#{time.inspect}` of `#{time.class.name}` given."
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def lookup(path)
|
241
|
+
raise NotImplementError.new 'Subclass responsibility.'
|
242
|
+
end
|
243
|
+
|
244
|
+
def added(parent)
|
245
|
+
@parent = parent
|
246
|
+
end
|
247
|
+
|
248
|
+
def path
|
249
|
+
parent ? "#{parent.path}/#{name}" : name
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
#
|
254
|
+
class Dir < Node
|
255
|
+
def initialize(backend, name, opts = {})
|
256
|
+
super
|
257
|
+
self.mode = 0777 - backend.get_umask
|
258
|
+
end
|
259
|
+
|
260
|
+
def lookup(path)
|
261
|
+
name, rest = path.to_s.split('/', 2).map(&:to_s)
|
262
|
+
|
263
|
+
if name.nil?
|
264
|
+
if rest.nil?
|
265
|
+
self
|
266
|
+
else
|
267
|
+
lookup rest
|
268
|
+
end
|
269
|
+
else
|
270
|
+
child = children.find{|c| c.name == name }
|
271
|
+
if child
|
272
|
+
rest.nil? ? child : child.lookup(rest)
|
273
|
+
else
|
274
|
+
nil
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
def add(node)
|
280
|
+
if children.any?{|c| c.name == node.name }
|
281
|
+
raise ArgumentError.new "Node #{path}/#{node.name} already exists."
|
282
|
+
else
|
283
|
+
children << node
|
284
|
+
node.added self
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
def all
|
289
|
+
children.reduce([]) do |memo, child|
|
290
|
+
memo << child
|
291
|
+
memo += child.all if child.is_a?(Dir)
|
292
|
+
memo
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
def children
|
297
|
+
@children ||= []
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
#
|
302
|
+
class File < Node
|
303
|
+
attr_accessor :content
|
304
|
+
|
305
|
+
def initialize(backend, name, opts = {})
|
306
|
+
super
|
307
|
+
self.mode = 0666 - backend.get_umask
|
308
|
+
end
|
309
|
+
|
310
|
+
def lookup(path)
|
311
|
+
nil
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|