iostreams 2.0.0 → 2.2.0
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.
- checksums.yaml +4 -4
- data/README.md +5 -22
- data/Rakefile +45 -0
- data/docs/CLAUDE.md +9 -0
- data/docs/config.md +161 -0
- data/docs/copy_files.md +93 -0
- data/docs/extensions.md +125 -0
- data/docs/formats.md +816 -0
- data/docs/index.md +388 -0
- data/docs/path.md +668 -0
- data/docs/pgp.md +436 -0
- data/docs/streams.md +382 -0
- data/docs/tutorial.md +495 -0
- data/docs/upgrading.md +664 -0
- data/lib/io_streams/builder/deprecations.rb +250 -0
- data/lib/io_streams/builder.rb +316 -39
- data/lib/io_streams/bzip2/reader.rb +41 -2
- data/lib/io_streams/bzip2/writer.rb +35 -2
- data/lib/io_streams/deprecation/methods.rb +51 -0
- data/lib/io_streams/deprecation/once.rb +27 -0
- data/lib/io_streams/deprecation/option_names.rb +26 -0
- data/lib/io_streams/deprecation/words.rb +42 -0
- data/lib/io_streams/deprecation.rb +259 -0
- data/lib/io_streams/encode/reader.rb +58 -3
- data/lib/io_streams/encode/writer.rb +23 -2
- data/lib/io_streams/encode.rb +99 -0
- data/lib/io_streams/errors.rb +55 -0
- data/lib/io_streams/gzip/reader.rb +15 -0
- data/lib/io_streams/gzip/writer.rb +11 -2
- data/lib/io_streams/io_streams.rb +468 -28
- data/lib/io_streams/line/reader.rb +9 -3
- data/lib/io_streams/line/utf8_reader.rb +23 -0
- data/lib/io_streams/path.rb +227 -28
- data/lib/io_streams/paths/file.rb +115 -6
- data/lib/io_streams/paths/http.rb +92 -5
- data/lib/io_streams/paths/matcher.rb +6 -3
- data/lib/io_streams/paths/s3.rb +242 -20
- data/lib/io_streams/paths/sftp/net_ssh.rb +104 -0
- data/lib/io_streams/paths/sftp.rb +141 -57
- data/lib/io_streams/paths.rb +23 -0
- data/lib/io_streams/pgp/reader.rb +60 -2
- data/lib/io_streams/pgp/writer.rb +42 -6
- data/lib/io_streams/pgp.rb +92 -21
- data/lib/io_streams/reader.rb +54 -1
- data/lib/io_streams/record/reader.rb +71 -10
- data/lib/io_streams/record/writer.rb +17 -10
- data/lib/io_streams/row/header_row.rb +21 -0
- data/lib/io_streams/row/reader.rb +52 -15
- data/lib/io_streams/row/writer.rb +64 -13
- data/lib/io_streams/schema/columns.rb +456 -0
- data/lib/io_streams/schema/constraints.rb +99 -0
- data/lib/io_streams/schema/field.rb +967 -0
- data/lib/io_streams/schema/json_number.rb +56 -0
- data/lib/io_streams/schema/keys.rb +78 -0
- data/lib/io_streams/schema/pattern.rb +257 -0
- data/lib/io_streams/schema/source.rb +31 -0
- data/lib/io_streams/schema.rb +616 -0
- data/lib/io_streams/stream.rb +843 -69
- data/lib/io_streams/symmetric_encryption/reader.rb +12 -0
- data/lib/io_streams/symmetric_encryption/writer.rb +4 -0
- data/lib/io_streams/tabular/header.rb +60 -4
- data/lib/io_streams/tabular/header_mapping.rb +332 -0
- data/lib/io_streams/tabular/mapping.rb +165 -0
- data/lib/io_streams/tabular/parser/array.rb +5 -0
- data/lib/io_streams/tabular/parser/base.rb +34 -0
- data/lib/io_streams/tabular/parser/csv.rb +5 -0
- data/lib/io_streams/tabular/parser/fixed.rb +200 -1
- data/lib/io_streams/tabular/parser/fixed_fields.rb +160 -0
- data/lib/io_streams/tabular/parser/hash.rb +10 -0
- data/lib/io_streams/tabular/parser/json.rb +17 -0
- data/lib/io_streams/tabular/parser/json_fields.rb +132 -0
- data/lib/io_streams/tabular/parser/psv.rb +6 -2
- data/lib/io_streams/tabular/schema_mapping.rb +361 -0
- data/lib/io_streams/tabular.rb +344 -30
- data/lib/io_streams/utils.rb +467 -1
- data/lib/io_streams/version.rb +1 -1
- data/lib/io_streams/writer.rb +37 -1
- data/lib/io_streams/xlsx/cells.rb +127 -0
- data/lib/io_streams/xlsx/reader.rb +61 -1
- data/lib/io_streams/xlsx/schema_reader.rb +90 -0
- data/lib/io_streams/xlsx/v3_writer.rb +16 -0
- data/lib/io_streams/zip/reader.rb +11 -0
- data/lib/io_streams/zip/writer.rb +28 -2
- data/lib/iostreams.rb +21 -17
- metadata +50 -7
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module IOStreams
|
|
2
|
+
module Line
|
|
3
|
+
# Reads the lines of a file whose text is UTF-8, but is read without an encoding, as UTF-8 strings, rather than the
|
|
4
|
+
# binary strings that a file is read as without one: the lines of a file with a schema, see
|
|
5
|
+
# `IOStreams::Stream#schema` and `IOStreams::Builder#utf8_text?`, as its rows and records are read.
|
|
6
|
+
#
|
|
7
|
+
# Raises `IOStreams::Errors::InvalidEncoding` for a line that is not valid UTF-8, naming the line that the first
|
|
8
|
+
# invalid character is on, see `IOStreams::Utils.invalid_utf8`.
|
|
9
|
+
class Utf8Reader < Reader
|
|
10
|
+
def readline
|
|
11
|
+
first_line_number = line_number + 1
|
|
12
|
+
line = super
|
|
13
|
+
return line unless line&.encoding == ::Encoding::BINARY
|
|
14
|
+
|
|
15
|
+
# Each line is a String of its own, sliced from the data read, so it is taken as UTF-8 in place.
|
|
16
|
+
line.force_encoding(::Encoding::UTF_8)
|
|
17
|
+
return line if line.valid_encoding?
|
|
18
|
+
|
|
19
|
+
raise(IOStreams::Utils.invalid_utf8(line, line_number: first_line_number))
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/io_streams/path.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module IOStreams
|
|
2
2
|
class Path < IOStreams::Stream
|
|
3
|
-
|
|
3
|
+
attr_reader :path
|
|
4
4
|
|
|
5
5
|
def initialize(path)
|
|
6
6
|
raise(ArgumentError, "Path cannot be nil") if path.nil?
|
|
@@ -9,20 +9,30 @@ module IOStreams
|
|
|
9
9
|
@path = path.frozen? ? path : path.dup.freeze
|
|
10
10
|
@io_stream = nil
|
|
11
11
|
@builder = nil
|
|
12
|
+
@root_name = nil
|
|
12
13
|
end
|
|
13
14
|
|
|
15
|
+
# A copy of a root path is not a root path, see #root!, so that it can be changed.
|
|
16
|
+
def initialize_copy(source)
|
|
17
|
+
super
|
|
18
|
+
@root_name = nil
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Returns [IOStreams::Path] a new path with the elements joined to this path, created with the same arguments as
|
|
22
|
+
# this path, such as the credentials of an SFTP path, but without the streams, options, format and schema set on
|
|
23
|
+
# this one. Without any elements it returns this path itself.
|
|
24
|
+
#
|
|
14
25
|
# If elements already contains the current path then it is used as is without
|
|
15
26
|
# adding the current path for a second time
|
|
16
27
|
def join(*elements)
|
|
17
28
|
return self if elements.empty?
|
|
18
29
|
|
|
19
30
|
elements = elements.collect(&:to_s)
|
|
20
|
-
relative = ::
|
|
31
|
+
relative = Utils::PathName.join(*elements)
|
|
21
32
|
|
|
22
|
-
new_path
|
|
23
|
-
new_path.
|
|
24
|
-
new_path.
|
|
25
|
-
new_path
|
|
33
|
+
new_path = dup
|
|
34
|
+
new_path.reset_builder
|
|
35
|
+
new_path.replace_path(contains?(relative) ? relative : Utils::PathName.join(path, relative))
|
|
26
36
|
end
|
|
27
37
|
|
|
28
38
|
def relative?
|
|
@@ -57,6 +67,24 @@ module IOStreams
|
|
|
57
67
|
path
|
|
58
68
|
end
|
|
59
69
|
|
|
70
|
+
# See Stream#reader.
|
|
71
|
+
#
|
|
72
|
+
# Raises [IOStreams::Errors::AccessDenied] when this path is not within any of the allowed paths,
|
|
73
|
+
# see `IOStreams.add_allowed_path`.
|
|
74
|
+
def reader(...)
|
|
75
|
+
authorize!
|
|
76
|
+
super
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# See Stream#writer.
|
|
80
|
+
#
|
|
81
|
+
# Raises [IOStreams::Errors::AccessDenied] when this path is not within any of the allowed paths,
|
|
82
|
+
# see `IOStreams.add_allowed_path`.
|
|
83
|
+
def writer(...)
|
|
84
|
+
authorize!
|
|
85
|
+
super
|
|
86
|
+
end
|
|
87
|
+
|
|
60
88
|
# Removes the last element of the path, the file name, before creating the entire path.
|
|
61
89
|
# Returns self
|
|
62
90
|
def mkpath
|
|
@@ -81,19 +109,6 @@ module IOStreams
|
|
|
81
109
|
raise NotImplementedError
|
|
82
110
|
end
|
|
83
111
|
|
|
84
|
-
# Cleanup an incomplete write to the target "file" if the copy fails.
|
|
85
|
-
# rubocop:disable Lint/SuppressedException
|
|
86
|
-
def copy_from(source, **args)
|
|
87
|
-
super
|
|
88
|
-
rescue StandardError => e
|
|
89
|
-
begin
|
|
90
|
-
delete
|
|
91
|
-
rescue NotImplementedError
|
|
92
|
-
end
|
|
93
|
-
raise(e)
|
|
94
|
-
end
|
|
95
|
-
# rubocop:enable Lint/SuppressedException
|
|
96
|
-
|
|
97
112
|
# Moves the file by copying it to the new path and then deleting the current path.
|
|
98
113
|
# Returns [IOStreams::Path] the target path.
|
|
99
114
|
#
|
|
@@ -107,7 +122,8 @@ module IOStreams
|
|
|
107
122
|
target
|
|
108
123
|
end
|
|
109
124
|
|
|
110
|
-
# Returns [IOStreams::Path] the directory for this file
|
|
125
|
+
# Returns [IOStreams::Path] the directory for this file, a new path created with the same arguments as this path,
|
|
126
|
+
# but without the streams, options, format and schema set on this one, see #join.
|
|
111
127
|
#
|
|
112
128
|
# If `path` does not include a directory name then "." is returned.
|
|
113
129
|
#
|
|
@@ -118,10 +134,9 @@ module IOStreams
|
|
|
118
134
|
# IOStreams.path("test").directory #=> "."
|
|
119
135
|
# IOStreams.path(".profile").directory #=> "."
|
|
120
136
|
def directory
|
|
121
|
-
new_path
|
|
122
|
-
new_path.
|
|
123
|
-
new_path.
|
|
124
|
-
new_path
|
|
137
|
+
new_path = dup
|
|
138
|
+
new_path.reset_builder
|
|
139
|
+
new_path.replace_path(Utils::PathName.dirname(path))
|
|
125
140
|
end
|
|
126
141
|
|
|
127
142
|
# When path is a file, deletes this file.
|
|
@@ -188,17 +203,201 @@ module IOStreams
|
|
|
188
203
|
path == other.path
|
|
189
204
|
end
|
|
190
205
|
|
|
206
|
+
# Returns [Array<Symbol>] the options for creating a path of this class whose values must not be displayed,
|
|
207
|
+
# such as a password, see .redact_options. A path class with sensitive options overrides it.
|
|
208
|
+
def self.sensitive_option_names
|
|
209
|
+
[]
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Returns [Hash] the supplied options for creating a path of this class, such as `password:`, with the value of
|
|
213
|
+
# each sensitive option replaced with "[FILTERED]", so that they can be displayed, for example by an application
|
|
214
|
+
# that stores them to create the path later. See .sensitive_option_names and `IOStreams::Utils.redact_options`.
|
|
215
|
+
def self.redact_options(options)
|
|
216
|
+
Utils.redact_options(options, sensitive_option_names)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Returns [String|nil] the deprecation warning for the supplied name or url of a path of this class, as the
|
|
220
|
+
# application supplies it to `IOStreams.path`, when IOStreams v3.0 reads it as another file or url, which says what
|
|
221
|
+
# to supply instead, so that it names the same one on both, or nil when v3.0 reads it the same way. A path class
|
|
222
|
+
# whose names v3.0 reads differently overrides it.
|
|
223
|
+
def self.name_deprecation(_name)
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Returns [String|nil] the deprecation warning for the supplied name or url, see .name_deprecation, only when
|
|
228
|
+
# IOStreams v3.0 reads it as another location, which the allowed paths compare, see `IOStreams.add_allowed_path`,
|
|
229
|
+
# rather than only spelling its url another way. A path class whose names v3.0 reads as another location overrides
|
|
230
|
+
# it.
|
|
231
|
+
def self.location_deprecation(_name)
|
|
232
|
+
nil
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Returns [true|false] whether the url has no path, such as `sftp://host`, or `https://host?a=1`, which IOStreams
|
|
236
|
+
# v3.0 reads with the path `/`, see `IOStreams::Paths::SFTP.name_deprecation`. Its text is checked rather than
|
|
237
|
+
# parsed, so that a pattern with characters that a url cannot hold, such as braces, can be checked too.
|
|
238
|
+
def self.url_without_path?(url)
|
|
239
|
+
url.match?(%r{\A[a-z][a-z0-9+.-]*://[^/?#]*(?:[?#]|\z)}i)
|
|
240
|
+
end
|
|
241
|
+
private_class_method :url_without_path?
|
|
242
|
+
|
|
243
|
+
# Shows the full name of the path, see #display_name, without any credentials, and the options of its streams
|
|
244
|
+
# without the values of sensitive options, such as a passphrase, see `IOStreams.redact_stream_options`.
|
|
191
245
|
def inspect
|
|
192
|
-
str = "#<#{self.class.name}:#{
|
|
193
|
-
str << " @builder=#{builder.streams.inspect}" if builder.streams
|
|
194
|
-
str << " @options=#{builder.options.inspect}" if builder.options
|
|
195
|
-
str << " pipeline=#{pipeline.inspect}>"
|
|
246
|
+
str = "#<#{self.class.name}:#{display_name}"
|
|
247
|
+
str << " @builder=#{redact(builder.streams).inspect}" if builder.streams
|
|
248
|
+
str << " @options=#{redact(builder.options).inspect}" if builder.options
|
|
249
|
+
str << " pipeline=#{redact(pipeline).inspect}>"
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Returns [String] the full name of this path, see #to_s, without any credentials, so that it can be logged
|
|
253
|
+
# or displayed. A user name, password or query in an SFTP or HTTP url is removed, since it can hold
|
|
254
|
+
# credentials, so unlike #to_s it cannot be used to create the path again. Each byte of a name that is not
|
|
255
|
+
# valid UTF-8 is shown as `\xHH`, see `IOStreams::Utils.display_text`.
|
|
256
|
+
#
|
|
257
|
+
# Example:
|
|
258
|
+
# IOStreams.path("sftp://jack:secret@example.org/data/a.csv").display_name
|
|
259
|
+
# # => "sftp://example.org/data/a.csv"
|
|
260
|
+
def display_name
|
|
261
|
+
Utils.display_text(to_s)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Deprecated: a path can be used by many objects at once, such as a root path that file names are joined to, so
|
|
265
|
+
# changing it in place changes it for all of them. Use #join or #directory, which return a new path, without the
|
|
266
|
+
# streams, options, format and schema set on this one, or `IOStreams.path` with the new name. Reports a
|
|
267
|
+
# deprecation warning, see `IOStreams::Deprecation`.
|
|
268
|
+
def path=(path)
|
|
269
|
+
Deprecation.warn("#path= is deprecated, use #join or #directory, which return a new path, or IOStreams.path " \
|
|
270
|
+
"with the new name")
|
|
271
|
+
@path = path
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
protected
|
|
275
|
+
|
|
276
|
+
# Returns [self] after setting its path in place, for IOStreams itself, see #path=.
|
|
277
|
+
def replace_path(path)
|
|
278
|
+
@path = path
|
|
279
|
+
self
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Raises [IOStreams::Errors::AccessDenied] when allowed paths have been added, see `IOStreams.add_allowed_path`,
|
|
283
|
+
# and this path is not within any of them, before #reader, #writer, `Stream#internal_reader` and
|
|
284
|
+
# `Stream#internal_writer` open it.
|
|
285
|
+
def authorize!
|
|
286
|
+
return if IOStreams.allowed_paths.empty? || (@permitted_path && @permitted_path == path)
|
|
287
|
+
|
|
288
|
+
authorize_location!(allowed_location)
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Returns [true|false] whether this path is within the allowed paths, see `#authorize!`.
|
|
292
|
+
def allowed?
|
|
293
|
+
authorize!
|
|
294
|
+
true
|
|
295
|
+
rescue Errors::AccessDenied
|
|
296
|
+
false
|
|
196
297
|
end
|
|
197
298
|
|
|
198
299
|
private
|
|
199
300
|
|
|
301
|
+
# Returns [String] the normalized location of this path, which is compared against the allowed paths.
|
|
302
|
+
#
|
|
303
|
+
# Each path class that can be used with allowed paths overrides this method. Without it every path
|
|
304
|
+
# of that class is denied once allowed paths have been added.
|
|
305
|
+
#
|
|
306
|
+
# Raises [IOStreams::Errors::AccessDenied] when the location cannot be determined.
|
|
307
|
+
def allowed_location
|
|
308
|
+
raise(Errors::AccessDenied, "Access denied: #{self.class.name} does not support allowed paths")
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# Raises [IOStreams::Errors::AccessDenied] when the supplied location, see `#allowed_location`,
|
|
312
|
+
# is not within any of the allowed paths.
|
|
313
|
+
def authorize_location!(location)
|
|
314
|
+
return if IOStreams.allowed_paths.any? { |allowed_path| within?(location, allowed_path) }
|
|
315
|
+
|
|
316
|
+
raise(Errors::AccessDenied, "Access denied to #{location}: it is not within any of the allowed paths")
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Returns [String] the supplied path with `.`, `..` and repeated `/` resolved, the way a remote server
|
|
320
|
+
# resolves them, without accessing it. The path always starts with `/`, and `..` cannot go above it.
|
|
321
|
+
def normalize_path(name)
|
|
322
|
+
segments = []
|
|
323
|
+
name.split("/").each do |segment|
|
|
324
|
+
case segment
|
|
325
|
+
when "", "."
|
|
326
|
+
next
|
|
327
|
+
when ".."
|
|
328
|
+
segments.pop
|
|
329
|
+
else
|
|
330
|
+
segments << segment
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
"/#{segments.join('/')}"
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# Returns [true|false] whether a child found by `#each_child` is within the allowed paths, logging it when it is not.
|
|
337
|
+
def allowed_child?(child)
|
|
338
|
+
return true if child.allowed?
|
|
339
|
+
|
|
340
|
+
IOStreams.logger&.warn("Skipping #{child.display_name} since it is not within any of the allowed paths")
|
|
341
|
+
false
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# Allows this exact path regardless of the allowed paths, for paths created by IOStreams itself such as temp files.
|
|
345
|
+
# Returns self
|
|
346
|
+
def permit!
|
|
347
|
+
@permitted_path = path
|
|
348
|
+
self
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# Makes this path the root path of the supplied name, which `IOStreams.add_root` adds, and the whole process shares,
|
|
352
|
+
# so that changing it in place reports a deprecation warning, see #warn_change. A copy of it is not a root path, see
|
|
353
|
+
# #initialize_copy.
|
|
354
|
+
# Returns self
|
|
355
|
+
def root!(name)
|
|
356
|
+
@root_name = name
|
|
357
|
+
self
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Returns [true|false] whether the streams of this path read a regular local file in v3.0: the file itself, or the
|
|
361
|
+
# temp file that it is downloaded into, as v2.2 reads, unless v3.0 reads it from another IO, see #v3_source, such as
|
|
362
|
+
# an S3 object, see `IOStreams::Stream#v3_reads_file?`.
|
|
363
|
+
def v3_reads_file?
|
|
364
|
+
v3_source.nil?
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# Changing a root path in place, see #root!, such as with #stream, is deprecated, since it changes the root for the
|
|
368
|
+
# whole process. v3.0 keeps its own frozen copy of the root instead, so that changing a path no longer changes the
|
|
369
|
+
# root: the path that `IOStreams.add_root` returns is that copy, so changing it raises FrozenError, while a path
|
|
370
|
+
# supplied to `IOStreams.add_root` is not, and `IOStreams.root`, `IOStreams.roots` and `IOStreams.join` without
|
|
371
|
+
# elements return copies of it, so changing them changes only the copy. A copy of the root, or a path joined to it,
|
|
372
|
+
# can be changed in either version, which the warning names for the root that this path is.
|
|
373
|
+
def warn_change(method_name)
|
|
374
|
+
return unless @root_name
|
|
375
|
+
|
|
376
|
+
root, join = @root_name == :default ? ["", ""] : ["(#{@root_name.inspect})", ", root: #{@root_name.inspect}"]
|
|
377
|
+
Deprecation.warn("#{method_name} is deprecated on a root path, which the whole process shares, since v3.0 " \
|
|
378
|
+
"keeps its own frozen copy of the root, and returns copies of it, so changing this path no " \
|
|
379
|
+
"longer changes the root, change a copy of it instead, such as IOStreams.root#{root}.dup, or a " \
|
|
380
|
+
"path joined to it, such as IOStreams.join(\"a.csv\"#{join})")
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# Returns [Hash<Symbol:Hash>] the streams with the values of sensitive options replaced.
|
|
384
|
+
def redact(streams)
|
|
385
|
+
IOStreams.redact_stream_options(streams)
|
|
386
|
+
end
|
|
387
|
+
|
|
200
388
|
def builder
|
|
201
389
|
@builder ||= IOStreams::Builder.new(path)
|
|
202
390
|
end
|
|
391
|
+
|
|
392
|
+
# Returns [true|false] whether the supplied path is this path, or is within this path.
|
|
393
|
+
# For example "a/b" contains "a/b/c.csv", but not "a/bc.csv".
|
|
394
|
+
def contains?(other)
|
|
395
|
+
path.empty? || within?(other, path)
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# Returns [true|false] whether the path `child` is `parent`, or is within `parent`.
|
|
399
|
+
def within?(child, parent)
|
|
400
|
+
child == parent || child.start_with?(parent.end_with?("/") ? parent : "#{parent}/")
|
|
401
|
+
end
|
|
203
402
|
end
|
|
204
403
|
end
|
|
@@ -3,13 +3,64 @@ require "fileutils"
|
|
|
3
3
|
module IOStreams
|
|
4
4
|
module Paths
|
|
5
5
|
class File < IOStreams::Path
|
|
6
|
-
|
|
6
|
+
# A pattern supplied to #each_child that is not a String, such as a Pathname, is deprecated.
|
|
7
|
+
PATTERN_DEPRECATION = "a pattern that is not a String is deprecated for #each_child and #children, since v3.0 " \
|
|
8
|
+
"raises ArgumentError for it, supply a String instead, such as pattern.to_s".freeze
|
|
9
|
+
private_constant :PATTERN_DEPRECATION
|
|
10
|
+
|
|
11
|
+
# Moving a file onto an existing directory is deprecated, see #move_to.
|
|
12
|
+
MOVE_DEPRECATION = "#move_to onto an existing directory is deprecated, since v3.0 raises ArgumentError for it, " \
|
|
13
|
+
"supply the file within the directory instead, such as " \
|
|
14
|
+
"move_to(IOStreams.path(directory).join(path.basename))".freeze
|
|
15
|
+
private_constant :MOVE_DEPRECATION
|
|
16
|
+
|
|
17
|
+
attr_reader :create_path
|
|
7
18
|
|
|
8
19
|
def initialize(file_name, create_path: true)
|
|
9
20
|
@create_path = create_path
|
|
10
21
|
super(file_name)
|
|
11
22
|
end
|
|
12
23
|
|
|
24
|
+
# Sets whether writing the file creates its directory first, see #mkpath.
|
|
25
|
+
def create_path=(create_path)
|
|
26
|
+
warn_change("#create_path=")
|
|
27
|
+
@create_path = create_path
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns [String|nil] the deprecation warning for a name that IOStreams v3.0 reads as another file, see
|
|
31
|
+
# `IOStreams::Path.name_deprecation`, or nil for one that it reads the same way:
|
|
32
|
+
# - A name of `~`, or starting with `~/`, which is within a directory called `~` in the current directory, while
|
|
33
|
+
# v3.0 reads it within the home directory. A name such as `~user/a.csv` or `~$Book1.xlsx` is the same on both.
|
|
34
|
+
# - A `file://` url, such as `file:///data/a.csv` or `file://~/a.csv`, which is a file name relative to the current
|
|
35
|
+
# directory, within a directory called `file:`, while v3.0 reads it as the local file that it names, such as
|
|
36
|
+
# `/data/a.csv`, or within the home directory.
|
|
37
|
+
def self.name_deprecation(name)
|
|
38
|
+
if file_url?(name)
|
|
39
|
+
"a file:// url is deprecated, since v3.0 reads it as the local file that it names, such as /data/a.csv for " \
|
|
40
|
+
"file:///data/a.csv, or a file within the home directory for file://~/a.csv, rather than as a file name " \
|
|
41
|
+
"relative to the current directory: supply the name of the file instead, such as /data/a.csv, or " \
|
|
42
|
+
"File.expand_path(\"~/a.csv\") for the home directory"
|
|
43
|
+
elsif name == "~" || name.start_with?("~/")
|
|
44
|
+
"a local file name of ~, or starting with ~/, is deprecated, since v3.0 reads it within the home directory, " \
|
|
45
|
+
"rather than within a directory called ~ in the current directory: expand it with File.expand_path for " \
|
|
46
|
+
"the home directory, or start it with ./~/ for the directory called ~"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Returns [String|nil] the deprecation warning for a name that IOStreams v3.0 reads as another file, see
|
|
51
|
+
# .name_deprecation, which is in another directory, and so another allowed path.
|
|
52
|
+
def self.location_deprecation(name)
|
|
53
|
+
name_deprecation(name)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Returns [true|false] whether the name is a `file://` url, in any case, as IOStreams v3.0 takes it, see
|
|
57
|
+
# .name_deprecation. Its first bytes are compared, so that a name that is not valid in its encoding, such as a
|
|
58
|
+
# Latin-1 name, can be checked too.
|
|
59
|
+
def self.file_url?(name)
|
|
60
|
+
name.include?("://") && name.byteslice(0, 7).b.casecmp?("file://")
|
|
61
|
+
end
|
|
62
|
+
private_class_method :file_url?
|
|
63
|
+
|
|
13
64
|
# Yields Paths within the current path.
|
|
14
65
|
#
|
|
15
66
|
# Examples:
|
|
@@ -88,18 +139,28 @@ module IOStreams
|
|
|
88
139
|
# "**.rb" "./main.rb" false
|
|
89
140
|
# "**.rb" "lib/song.rb" true
|
|
90
141
|
# "*" "dave/.profile" true
|
|
142
|
+
#
|
|
143
|
+
# A pattern that is not a String, such as a Pathname, is deprecated, and reports a deprecation warning, see
|
|
144
|
+
# `IOStreams::Deprecation`.
|
|
91
145
|
def each_child(pattern = "*", case_sensitive: false, directories: false, hidden: false)
|
|
92
146
|
unless block_given?
|
|
93
147
|
return to_enum(__method__, pattern,
|
|
94
148
|
case_sensitive: case_sensitive, directories: directories, hidden: hidden)
|
|
95
149
|
end
|
|
96
150
|
|
|
151
|
+
Deprecation.warn(PATTERN_DEPRECATION) unless pattern.is_a?(String)
|
|
152
|
+
authorize!
|
|
153
|
+
|
|
154
|
+
# A pattern that is a Pathname is taken by its #to_path, as `::File.join` takes it, which v2.1 joined the pattern
|
|
155
|
+
# with: `IOStreams::Utils::PathName.join` does not take it on JRuby, where it joins the names by their bytes, and
|
|
156
|
+
# the case-insensitive retry below matches and downcases the pattern as a String.
|
|
157
|
+
pattern = pattern.to_path if pattern.respond_to?(:to_path)
|
|
158
|
+
|
|
97
159
|
flags = 0
|
|
98
160
|
flags |= ::File::FNM_CASEFOLD unless case_sensitive
|
|
99
161
|
flags |= ::File::FNM_DOTMATCH if hidden
|
|
100
162
|
|
|
101
|
-
|
|
102
|
-
full_pattern = ::File.join(path, pattern)
|
|
163
|
+
full_pattern = Utils::PathName.join(path, pattern)
|
|
103
164
|
|
|
104
165
|
results = Dir.glob(full_pattern, flags)
|
|
105
166
|
|
|
@@ -109,14 +170,15 @@ module IOStreams
|
|
|
109
170
|
if results.empty? && !case_sensitive && pattern.match?(/[A-Z]/)
|
|
110
171
|
# Try converting the pattern to lowercase and re-matching
|
|
111
172
|
lowercase_pattern = pattern.downcase
|
|
112
|
-
lowercase_full_pattern = ::
|
|
173
|
+
lowercase_full_pattern = Utils::PathName.join(path, lowercase_pattern)
|
|
113
174
|
results = Dir.glob(lowercase_full_pattern, flags)
|
|
114
175
|
end
|
|
115
176
|
|
|
116
177
|
results.each do |full_path|
|
|
117
178
|
next if !directories && ::File.directory?(full_path)
|
|
118
179
|
|
|
119
|
-
|
|
180
|
+
child = self.class.new(full_path)
|
|
181
|
+
yield(child) if allowed_child?(child)
|
|
120
182
|
end
|
|
121
183
|
end
|
|
122
184
|
|
|
@@ -124,10 +186,16 @@ module IOStreams
|
|
|
124
186
|
#
|
|
125
187
|
# Notes:
|
|
126
188
|
# - Can copy across buckets.
|
|
189
|
+
# - A target that is an existing directory, which the file is moved into, as `mv` does, returning the directory, is
|
|
190
|
+
# deprecated, and reports a deprecation warning, see `IOStreams::Deprecation`: supply the file within it instead,
|
|
191
|
+
# such as `move_to(IOStreams.path("/archive").join(path.basename))`.
|
|
127
192
|
def move_to(target_path)
|
|
128
193
|
target = IOStreams.new(target_path)
|
|
129
194
|
return super(target) unless target.is_a?(self.class)
|
|
130
195
|
|
|
196
|
+
authorize!
|
|
197
|
+
target.authorize!
|
|
198
|
+
Deprecation.warn(MOVE_DEPRECATION) if ::File.directory?(target.path)
|
|
131
199
|
target.mkpath
|
|
132
200
|
# In case the file is being moved across partitions
|
|
133
201
|
FileUtils.move(path, target.to_s)
|
|
@@ -135,25 +203,30 @@ module IOStreams
|
|
|
135
203
|
end
|
|
136
204
|
|
|
137
205
|
def mkpath
|
|
138
|
-
|
|
206
|
+
authorize!
|
|
207
|
+
dir = Utils::PathName.dirname(path)
|
|
139
208
|
FileUtils.mkdir_p(dir)
|
|
140
209
|
self
|
|
141
210
|
end
|
|
142
211
|
|
|
143
212
|
def mkdir
|
|
213
|
+
authorize!
|
|
144
214
|
FileUtils.mkdir_p(path)
|
|
145
215
|
self
|
|
146
216
|
end
|
|
147
217
|
|
|
148
218
|
def exist?
|
|
219
|
+
authorize!
|
|
149
220
|
::File.exist?(path)
|
|
150
221
|
end
|
|
151
222
|
|
|
152
223
|
def size
|
|
224
|
+
authorize!
|
|
153
225
|
::File.size(path)
|
|
154
226
|
end
|
|
155
227
|
|
|
156
228
|
def delete
|
|
229
|
+
authorize!
|
|
157
230
|
return self unless exist?
|
|
158
231
|
|
|
159
232
|
::File.directory?(path) ? Dir.delete(path) : ::File.unlink(path)
|
|
@@ -161,6 +234,7 @@ module IOStreams
|
|
|
161
234
|
end
|
|
162
235
|
|
|
163
236
|
def delete_all
|
|
237
|
+
authorize!
|
|
164
238
|
return self unless exist?
|
|
165
239
|
|
|
166
240
|
::File.directory?(path) ? FileUtils.remove_dir(path) : ::File.unlink(path)
|
|
@@ -169,11 +243,46 @@ module IOStreams
|
|
|
169
243
|
|
|
170
244
|
# Returns the real path by stripping `.`, `..` and expands any symlinks.
|
|
171
245
|
def realpath
|
|
246
|
+
authorize!
|
|
172
247
|
self.class.new(::File.realpath(path))
|
|
173
248
|
end
|
|
174
249
|
|
|
175
250
|
private
|
|
176
251
|
|
|
252
|
+
# Returns [String] the real path of this file, following any symbolic links, which is compared
|
|
253
|
+
# against the allowed paths.
|
|
254
|
+
#
|
|
255
|
+
# The part of the path that does not exist yet, for example a file that is about to be written,
|
|
256
|
+
# is appended to the real path of the part that does exist. It cannot contain `.` or `..`, since
|
|
257
|
+
# what they refer to depends on directories that have not been created yet.
|
|
258
|
+
def allowed_location
|
|
259
|
+
existing = ::File.absolute_path?(path) ? path : Utils::PathName.join(Dir.pwd, path)
|
|
260
|
+
missing = []
|
|
261
|
+
until present?(existing)
|
|
262
|
+
parent = Utils::PathName.dirname(existing)
|
|
263
|
+
break if parent == existing
|
|
264
|
+
|
|
265
|
+
missing.unshift(Utils::PathName.basename(existing))
|
|
266
|
+
existing = parent
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
if missing.intersect?([".", ".."])
|
|
270
|
+
raise(Errors::AccessDenied, "Access denied to #{path}: '.' and '..' are not allowed after a missing directory")
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
Utils::PathName.join(::File.realpath(existing), *missing)
|
|
274
|
+
rescue SystemCallError => e
|
|
275
|
+
raise(Errors::AccessDenied, "Access denied to #{path}: #{e.message}")
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Returns [true|false] whether the file, directory or symbolic link exists, without following the link.
|
|
279
|
+
def present?(file_name)
|
|
280
|
+
::File.lstat(file_name)
|
|
281
|
+
true
|
|
282
|
+
rescue SystemCallError
|
|
283
|
+
false
|
|
284
|
+
end
|
|
285
|
+
|
|
177
286
|
# Read from file
|
|
178
287
|
def stream_reader(&block)
|
|
179
288
|
::File.open(path, "rb") { |io| builder.reader(io, &block) }
|