nfs-rb 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f89588ae594babd596c23383c2b1ad83cf363a4c1085a6cd7318bc8800f3aa04
4
- data.tar.gz: f56bcb66d4e4b6b87025cb9a2cf233e61f7c4fcafd0ad8847e6312068325f1ec
3
+ metadata.gz: da83f2d0b02d0b4510cfee2eb9e0901e27f135c8061f7e3af4903a6107a38610
4
+ data.tar.gz: e2f99227ed65807cc4eae2b7ef6f94607eb3d71daa0cf510d34e6a754e3ae962
5
5
  SHA512:
6
- metadata.gz: e290e627fbbbe509b5504df9560aea5613a16a81966ff64dfbd307599ef4595e38b7238e48cc626e36e0a819f15728ef55fa4c03f9413cb537e9ad529166e4c9
7
- data.tar.gz: '038b038715b5a04f0a7be70ea0ae1361bdabf16db509faf0792682cecbf346ae6817e3663f7b4467139eab7fc46e574b7b2e1a15480c8ebccb46ddd81651b0cf'
6
+ metadata.gz: 0e8249ea929bd6800341b14658aafb8f43e4b45bc15c7a83636ae6c91779c25f59ccb56cbd960520f115cbfe2cc2537ffb734e18a5eb6f55fc9a8f919e89baed
7
+ data.tar.gz: 125b3f234f74932584aa1a543114541f6bd70a4d56e8000254f295627df33068ec653b9c0d2240cf281aa4cd3c06ce1ce663ad953e2168c619f2345cc926dc78
@@ -1,3 +1,6 @@
1
+ ## 1.0.2
2
+ * Limit open file handles to prevent Errno::EMFILE errors.
3
+
1
4
  ## 1.0.1
2
5
  * Fix missing constant error in `UDPClient`.
3
6
  * Add protocol to startup log message.
data/lib/nfs.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module NFS
2
2
  class << self
3
- attr_accessor :logger
3
+ def logger
4
+ @logger ||= DefaultLogger.new(STDOUT)
5
+ end
4
6
  end
5
7
 
6
8
  autoload :DefaultLogger, 'nfs/default_logger'
@@ -13,5 +15,3 @@ module NFS
13
15
  autoload :SUNRPC, 'nfs/sunrpc'
14
16
  autoload :XDR, 'nfs/xdr'
15
17
  end
16
-
17
- NFS.logger = NFS::DefaultLogger.new(STDOUT)
@@ -1,44 +1,28 @@
1
- module NFS
2
- class FileProxy < ::File
3
- class << self
4
- def new(*args, &block)
5
- super(*args, &block)._nfs_setup
6
- end
7
-
8
- def open(*args)
9
- f = super(*args)._nfs_setup
1
+ require 'fileutils'
10
2
 
11
- if block_given?
12
- begin
13
- return yield(f)
14
- ensure
15
- f.close
16
- end
17
- end
18
-
19
- f
20
- end
21
- end
3
+ module NFS
4
+ class FileProxy
5
+ attr_reader :path
22
6
 
23
- def _nfs_setup
7
+ def initialize(path)
8
+ @path = path
24
9
  @absolute_path = File.expand_path(path)
25
10
  @looked_up = {}
26
- self
27
11
  end
28
12
 
29
13
  def create(name, mode, uid, gid)
30
- f = nil
14
+ path = _lookup(name)
15
+ f = self.class.new(path)
31
16
 
32
- begin
33
- f = self.class.new(_lookup(name), File::RDWR | File::CREAT, mode)
34
- rescue
35
- f = self.class.new(_lookup(name), File::RDONLY | File::CREAT, mode)
17
+ unless File.exist?(path)
18
+ FileUtils.touch(path)
36
19
  end
37
20
 
38
- stat = f.lstat
21
+ f.chmod(mode)
22
+ f.chown(uid, gid)
39
23
 
24
+ stat = f.lstat
40
25
  @looked_up[[stat.ino, name]] = f
41
-
42
26
  [f, stat]
43
27
  end
44
28
 
@@ -47,13 +31,7 @@ module NFS
47
31
  end
48
32
 
49
33
  def lookup(name)
50
- f = nil
51
-
52
- begin
53
- f = self.class.new(_lookup(name), File::RDWR)
54
- rescue
55
- f = self.class.new(_lookup(name), File::RDONLY)
56
- end
34
+ f = self.class.new(_lookup(name))
57
35
 
58
36
  stat = f.lstat
59
37
  key = [stat.ino, name]
@@ -81,16 +59,11 @@ module NFS
81
59
  File.symlink(to_name, _lookup(name))
82
60
  end
83
61
 
84
- def readlink
85
- File.readlink(@absolute_path)
86
- end
87
-
88
- def mkdir(name, mode, uid, gid)
62
+ def mkdir(name, mode)
89
63
  path = _lookup(name)
90
64
  Dir.mkdir(path, mode)
91
65
 
92
66
  f = self.class.new(path)
93
- #f.chown(uid, gid)
94
67
 
95
68
  stat = f.lstat
96
69
  @looked_up[[stat.ino, name]] = f
@@ -113,5 +86,21 @@ module NFS
113
86
  def utime(atime, mtime)
114
87
  File.utime(atime, mtime, @absolute_path)
115
88
  end
89
+
90
+ def lstat
91
+ File.lstat(@absolute_path)
92
+ end
93
+
94
+ def truncate(len)
95
+ File.truncate(@absolute_path, len)
96
+ end
97
+
98
+ def chmod(new_mode)
99
+ File.chmod(new_mode, @absolute_path)
100
+ end
101
+
102
+ def chown(uid, gid)
103
+ File.chown(uid, gid, @absolute_path)
104
+ end
116
105
  end
117
106
  end
@@ -1,5 +1,7 @@
1
1
  module NFS
2
2
  class Handler
3
+ MAX_READDIR_ENTRIES = 200
4
+
3
5
  def initialize(root = nil, fsid = 0)
4
6
  @mount_prog = Mount::MOUNTPROG.dup
5
7
  @mount_vers = Mount::MOUNTVERS
@@ -8,6 +10,7 @@ module NFS
8
10
 
9
11
  @exports = {}
10
12
  @fh_table = {}
13
+ @write_handles = {}
11
14
  @file_objects = {}
12
15
  @next_fh = Filehandle.new
13
16
 
@@ -21,6 +24,12 @@ module NFS
21
24
  define_nfs_procedures
22
25
 
23
26
  instance_eval(&block) if block_given?
27
+
28
+ at_exit do
29
+ @write_handles.each do |_, wh|
30
+ wh.close
31
+ end
32
+ end
24
33
  end
25
34
 
26
35
  def programs
@@ -284,7 +293,7 @@ module NFS
284
293
  f.utime(atime, mtime)
285
294
  end
286
295
 
287
- ::NFS.logger.info("SETATTR #{f} #{changes.join(', ')}")
296
+ ::NFS.logger.info("SETATTR #{f.path} #{changes.join(', ')}")
288
297
 
289
298
  {
290
299
  _discriminant: :NFS_OK,
@@ -334,19 +343,22 @@ module NFS
334
343
 
335
344
  @nfs_prog.on_call(@nfs_vers, :READ) do |arg, auth, verf|
336
345
  handle_errors do
337
- f = @fh_table[arg[:file][:data]]
338
- ::NFS.logger.info("READ #{f.path}")
339
- attrs = f.lstat
340
- f.pos = arg[:offset]
341
- result = f.read(arg[:count])
342
-
343
- {
344
- _discriminant: :NFS_OK,
345
- reply: {
346
- attributes: convert_attrs(attrs),
347
- data: result
346
+ fh = @fh_table[arg[:file][:data]]
347
+ ::NFS.logger.info("READ #{fh.path}")
348
+ attrs = fh.lstat
349
+
350
+ File.open(fh.path, 'r') do |f|
351
+ f.pos = arg[:offset]
352
+ result = f.read(arg[:count])
353
+
354
+ {
355
+ _discriminant: :NFS_OK,
356
+ reply: {
357
+ attributes: convert_attrs(attrs),
358
+ data: result
359
+ }
348
360
  }
349
- }
361
+ end
350
362
  end
351
363
  end
352
364
 
@@ -357,12 +369,14 @@ module NFS
357
369
  @nfs_prog.on_call(@nfs_vers, :WRITE) do |arg, auth, verf|
358
370
 
359
371
  handle_errors do
360
- f = @fh_table[arg[:file][:data]]
361
- ::NFS.logger.info("WRITE #{f.path}")
362
- f.pos = arg[:offset]
363
- f.write(arg[:data])
364
- f.flush
365
- attrs = f.lstat
372
+ fh = @fh_table[arg[:file][:data]]
373
+ ::NFS.logger.info("WRITE #{fh.path}")
374
+
375
+ wh = @write_handles[arg[:file][:data]] ||= File.open(fh.path, 'w')
376
+ wh.pos = arg[:offset]
377
+ wh.write(arg[:data])
378
+ wh.flush
379
+ attrs = fh.lstat
366
380
 
367
381
  {
368
382
  _discriminant: :NFS_OK,
@@ -503,8 +517,9 @@ module NFS
503
517
 
504
518
  result_entries = nil
505
519
  last_entry = nil
520
+ total = 0
506
521
 
507
- while cookie < entries.size && need_bytes < count
522
+ while cookie < entries.size && total < MAX_READDIR_ENTRIES && need_bytes < count
508
523
  need_bytes += NFS::Filename.encode(entries[cookie]).size
509
524
 
510
525
  next_entry = {
@@ -513,7 +528,7 @@ module NFS
513
528
  cookie: cookie
514
529
  }
515
530
 
516
- if not last_entry.nil?
531
+ if !last_entry.nil?
517
532
  last_entry[:nextentry] = next_entry
518
533
  last_entry = next_entry
519
534
  end
@@ -524,6 +539,7 @@ module NFS
524
539
  end
525
540
 
526
541
  cookie += 1
542
+ total += 1
527
543
  need_bytes += 16
528
544
  end
529
545
 
@@ -533,7 +549,7 @@ module NFS
533
549
  eof = :FALSE
534
550
  end
535
551
 
536
- if not last_entry.nil?
552
+ if !last_entry.nil?
537
553
  last_entry[:nextentry] = nil
538
554
  end
539
555
 
@@ -8,7 +8,7 @@ module NFS
8
8
  @port = port
9
9
  @protocol = protocol
10
10
 
11
- @handler = Handler.new(FileProxy.open(dir))
11
+ @handler = Handler.new(FileProxy.new(dir))
12
12
  @server = server_class.new(@handler.programs, port, host)
13
13
  end
14
14
 
@@ -44,8 +44,8 @@ module NFS
44
44
  begin
45
45
  result = @returntype.encode(result_object)
46
46
  rescue => e
47
- NFS.logger.error(e.message)
48
- NFS.logger.error(e.backtrace.join("\n"))
47
+ ::NFS.logger.error(e.message)
48
+ ::NFS.logger.error(e.backtrace.join("\n"))
49
49
  raise IgnoreRequest
50
50
  end
51
51
 
@@ -1,3 +1,3 @@
1
1
  module NFS
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nfs-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Dutro
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-08-16 00:00:00.000000000 Z
12
+ date: 2020-08-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: An NFS v2 server implemented in pure Ruby.
15
15
  email: