File: demo.rb

package info (click to toggle)
ruby-fusefs 0.7.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: ansic: 1,137; ruby: 760; sh: 8; makefile: 2
file content (100 lines) | stat: -rw-r--r-- 1,870 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require "rubygems"
require 'fusefs'
include FuseFS

root = MetaDir.new

# if (ARGV.size != 1)
#   puts "Usage: #{$0} <directory>"
#   exit
# end

dirname = ARGV.shift

unless File.directory?(dirname)
  puts "Usage: #{$0} <directory>"
  exit
end

class DirLink
  def initialize(dir)
    File.directory?(dir) or raise ArgumentError, "DirLink.initialize expects a valid directory!"
    @base = dir
  end
  def directory?(path)
    File.directory?(File.join(@base,path))
  end
  def file?(path)
    File.file?(File.join(@base,path))
  end
  def contents(path)
    fn = File.join(@base,path)
    Dir.entries(fn).map { |file|
      file = file.sub(/^#{fn}\/?/,'')
      if ['..','.'].include?(file)
        nil
      else
        file
      end
    }.compact.sort
  end
  def read_file(path)
    fn = File.join(@base,path)
    if File.file?(fn)
      IO.read(fn)
    else
      'No such file'
    end
  end
end

class Counter
  def initialize
    @counter = 0
  end
  def to_s
    @counter += 1
    @counter.to_s + "\n"
  end
  def size
    @counter.to_s.size
  end
end

class Randwords
  def initialize(*ary)
    @ary = ary.flatten
  end
  def to_s
    @ary[rand(@ary.size)].to_s + "\n"
  end
  def size
    @size ||= @ary.map{|v| v.size}.max
  end
end

root.write_to('/hello',"Hello, World!\n")

progress = '.'

root.write_to('/progress',progress)

Thread.new do
  20.times do
    sleep 5
    progress << '.'
  end
end

root.write_to('/counter',Counter.new)
root.write_to('/color',Randwords.new('red','blue','green','purple','yellow','bistre','burnt sienna','jade'))
root.write_to('/animal',Randwords.new('duck','dog','cat','duck billed platypus','silly fella'))

root.mkdir("/#{ENV['USER']}",DirLink.new(ENV['HOME']))

# Set the root FuseFS
FuseFS.set_root(root)

FuseFS.mount_under(dirname, 'nolocalcaches', *ARGV)

FuseFS.run # This doesn't return until we're unmounted.