opal-file 0.1.0 → 1.0.0

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
  SHA1:
3
- metadata.gz: 4cdaf48c60594a394f17039dd51c54f6f84e3311
4
- data.tar.gz: 4935595496fbe84a04aff2b1955ce29f5a2608d6
3
+ metadata.gz: b7e6065ecbca73e83f6eedfe6be90d7bdbf17f3c
4
+ data.tar.gz: d0295aee4b30ce3608cd7b13e8dba7cc65f9858c
5
5
  SHA512:
6
- metadata.gz: 4e6da2e9346f5ee018e1a8482456acf0f09abdf9327cbe4c9bccb4af670532b88dd9f48ecc0e3d64718420b2a401220583cbacd09c6efb6aa7388876117ed1d6
7
- data.tar.gz: d12a6a647a146f9ebdbba4e0f3fdc19b149d037492fa7abac6c76cb9862ebfd62ccf397b8a2b8f85e0d8be6fa258914fb46ae6dcc3d2851cec920a46dc101bcd
6
+ metadata.gz: caf7dbb1c3daf3086b8c33d73b95808293e92c1ddabb179231ee8eec326ea0bfe4702225219a4ae28a94970964c2dad5b71b72e1f4cfa5d0067f3286ae8a2c0d
7
+ data.tar.gz: 41b1823fdb3d1d9ab710222a4ee0ee8e52362f6345da844a353d69f36a0566242480065760798d369292a281ea43f22a566a78d608cf29c8db06b2ef015d179c
data/README.md CHANGED
@@ -6,13 +6,12 @@ native File.read, File.write etc... for Opal
6
6
 
7
7
  Opal's File class does not have methods for file IO but this module provides them.
8
8
 
9
- ### Limitation
9
+ This provides...
10
10
 
11
- This provides only methods listed below.
12
-
13
- - read
14
- - write
15
- - unlink
11
+ - `File` (partial; no #open and #new)
12
+ - `File::Stat` (partial)
13
+ - `FileTest` (partial)
14
+ - `Errno::EXXX`
16
15
 
17
16
  Pull requests are welcome!
18
17
 
@@ -0,0 +1,137 @@
1
+ if RUBY_ENGINE == "opal"
2
+ module Errno
3
+ class EPERM < SystemCallError; Errno = 1; end
4
+ class ENOENT < SystemCallError; Errno = 2; end
5
+ class ESRCH < SystemCallError; Errno = 3; end
6
+ class EINTR < SystemCallError; Errno = 4; end
7
+ class EIO < SystemCallError; Errno = 5; end
8
+ class ENXIO < SystemCallError; Errno = 6; end
9
+ class E2BIG < SystemCallError; Errno = 7; end
10
+ class ENOEXEC < SystemCallError; Errno = 8; end
11
+ class EBADF < SystemCallError; Errno = 9; end
12
+ class ECHILD < SystemCallError; Errno = 10; end
13
+ class EAGAIN < SystemCallError; Errno = 11; end
14
+ class ENOMEM < SystemCallError; Errno = 12; end
15
+ class EACCES < SystemCallError; Errno = 13; end
16
+ class EFAULT < SystemCallError; Errno = 14; end
17
+ class ENOTBLK < SystemCallError; Errno = 15; end
18
+ class EBUSY < SystemCallError; Errno = 16; end
19
+ class EEXIST < SystemCallError; Errno = 17; end
20
+ class EXDEV < SystemCallError; Errno = 18; end
21
+ class ENODEV < SystemCallError; Errno = 19; end
22
+ class ENOTDIR < SystemCallError; Errno = 20; end
23
+ class EISDIR < SystemCallError; Errno = 21; end
24
+ class EINVAL < SystemCallError; Errno = 22; end
25
+ class ENFILE < SystemCallError; Errno = 23; end
26
+ class EMFILE < SystemCallError; Errno = 24; end
27
+ class ENOTTY < SystemCallError; Errno = 25; end
28
+ class ETXTBSY < SystemCallError; Errno = 26; end
29
+ class EFBIG < SystemCallError; Errno = 27; end
30
+ class ENOSPC < SystemCallError; Errno = 28; end
31
+ class ESPIPE < SystemCallError; Errno = 29; end
32
+ class EROFS < SystemCallError; Errno = 30; end
33
+ class EMLINK < SystemCallError; Errno = 31; end
34
+ class EPIPE < SystemCallError; Errno = 32; end
35
+ class EDOM < SystemCallError; Errno = 33; end
36
+ class ERANGE < SystemCallError; Errno = 34; end
37
+ class EDEADLK < SystemCallError; Errno = 35; end
38
+ class ENAMETOOLONG < SystemCallError; Errno = 36; end
39
+ class ENOLCK < SystemCallError; Errno = 37; end
40
+ class ENOSYS < SystemCallError; Errno = 38; end
41
+ class ENOTEMPTY < SystemCallError; Errno = 39; end
42
+ class ELOOP < SystemCallError; Errno = 40; end
43
+ class EWOULDBLOCK < SystemCallError; Errno = EAGAIN::Errno; end
44
+ class ENOMSG < SystemCallError; Errno = 42; end
45
+ class EIDRM < SystemCallError; Errno = 43; end
46
+ class ECHRNG < SystemCallError; Errno = 44; end
47
+ class EL2NSYNC < SystemCallError; Errno = 45; end
48
+ class EL3HLT < SystemCallError; Errno = 46; end
49
+ class EL3RST < SystemCallError; Errno = 47; end
50
+ class ELNRNG < SystemCallError; Errno = 48; end
51
+ class EUNATCH < SystemCallError; Errno = 49; end
52
+ class ENOCSI < SystemCallError; Errno = 50; end
53
+ class EL2HLT < SystemCallError; Errno = 51; end
54
+ class EBADE < SystemCallError; Errno = 52; end
55
+ class EBADR < SystemCallError; Errno = 53; end
56
+ class EXFULL < SystemCallError; Errno = 54; end
57
+ class ENOANO < SystemCallError; Errno = 55; end
58
+ class EBADRQC < SystemCallError; Errno = 56; end
59
+ class EBADSLT < SystemCallError; Errno = 57; end
60
+ class EDEADLOCK < SystemCallError; Errno = EDEADLK::Errno; end
61
+ class EBFONT < SystemCallError; Errno = 59 ; end
62
+ class ENOSTR < SystemCallError; Errno = 60 ; end
63
+ class ENODATA < SystemCallError; Errno = 61 ; end
64
+ class ETIME < SystemCallError; Errno = 62 ; end
65
+ class ENOSR < SystemCallError; Errno = 63 ; end
66
+ class ENONET < SystemCallError; Errno = 64 ; end
67
+ class ENOPKG < SystemCallError; Errno = 65 ; end
68
+ class EREMOTE < SystemCallError; Errno = 66 ; end
69
+ class ENOLINK < SystemCallError; Errno = 67 ; end
70
+ class EADV < SystemCallError; Errno = 68 ; end
71
+ class ESRMNT < SystemCallError; Errno = 69 ; end
72
+ class ECOMM < SystemCallError; Errno = 70 ; end
73
+ class EPROTO < SystemCallError; Errno = 71 ; end
74
+ class EMULTIHOP < SystemCallError; Errno = 72 ; end
75
+ class EDOTDOT < SystemCallError; Errno = 73 ; end
76
+ class EBADMSG < SystemCallError; Errno = 74 ; end
77
+ class EOVERFLOW < SystemCallError; Errno = 75 ; end
78
+ class ENOTUNIQ < SystemCallError; Errno = 76 ; end
79
+ class EBADFD < SystemCallError; Errno = 77 ; end
80
+ class EREMCHG < SystemCallError; Errno = 78 ; end
81
+ class ELIBACC < SystemCallError; Errno = 79 ; end
82
+ class ELIBBAD < SystemCallError; Errno = 80 ; end
83
+ class ELIBSCN < SystemCallError; Errno = 81 ; end
84
+ class ELIBMAX < SystemCallError; Errno = 82 ; end
85
+ class ELIBEXEC < SystemCallError; Errno = 83 ; end
86
+ class EILSEQ < SystemCallError; Errno = 84 ; end
87
+ class ERESTART < SystemCallError; Errno = 85 ; end
88
+ class ESTRPIPE < SystemCallError; Errno = 86 ; end
89
+ class EUSERS < SystemCallError; Errno = 87 ; end
90
+ class ENOTSOCK < SystemCallError; Errno = 88 ; end
91
+ class EDESTADDRREQ < SystemCallError; Errno = 89 ; end
92
+ class EMSGSIZE < SystemCallError; Errno = 90 ; end
93
+ class EPROTOTYPE < SystemCallError; Errno = 91 ; end
94
+ class ENOPROTOOPT < SystemCallError; Errno = 92 ; end
95
+ class EPROTONOSUPPORT < SystemCallError; Errno = 93 ; end
96
+ class ESOCKTNOSUPPORT < SystemCallError; Errno = 94 ; end
97
+ class EOPNOTSUPP < SystemCallError; Errno = 95 ; end
98
+ class EPFNOSUPPORT < SystemCallError; Errno = 96 ; end
99
+ class EAFNOSUPPORT < SystemCallError; Errno = 97 ; end
100
+ class EADDRINUSE < SystemCallError; Errno = 98 ; end
101
+ class EADDRNOTAVAIL < SystemCallError; Errno = 99 ; end
102
+ class ENETDOWN < SystemCallError; Errno = 100; end
103
+ class ENETUNREACH < SystemCallError; Errno = 101; end
104
+ class ENETRESET < SystemCallError; Errno = 102; end
105
+ class ECONNABORTED < SystemCallError; Errno = 103; end
106
+ class ECONNRESET < SystemCallError; Errno = 104; end
107
+ class ENOBUFS < SystemCallError; Errno = 105; end
108
+ class EISCONN < SystemCallError; Errno = 106; end
109
+ class ENOTCONN < SystemCallError; Errno = 107; end
110
+ class ESHUTDOWN < SystemCallError; Errno = 108; end
111
+ class ETOOMANYREFS < SystemCallError; Errno = 109; end
112
+ class ETIMEDOUT < SystemCallError; Errno = 110; end
113
+ class ECONNREFUSED < SystemCallError; Errno = 111; end
114
+ class EHOSTDOWN < SystemCallError; Errno = 112; end
115
+ class EHOSTUNREACH < SystemCallError; Errno = 113; end
116
+ class EALREADY < SystemCallError; Errno = 114; end
117
+ class EINPROGRESS < SystemCallError; Errno = 115; end
118
+ class ESTALE < SystemCallError; Errno = 116; end
119
+ class EUCLEAN < SystemCallError; Errno = 117; end
120
+ class ENOTNAM < SystemCallError; Errno = 118; end
121
+ class ENAVAIL < SystemCallError; Errno = 119; end
122
+ class EISNAM < SystemCallError; Errno = 120; end
123
+ class EREMOTEIO < SystemCallError; Errno = 121; end
124
+ class EDQUOT < SystemCallError; Errno = 122; end
125
+ class ENOMEDIUM < SystemCallError; Errno = 123; end
126
+ class EMEDIUMTYPE < SystemCallError; Errno = 124; end
127
+ class ECANCELED < SystemCallError; Errno = 125; end
128
+ class ENOKEY < SystemCallError; Errno = 126; end
129
+ class EKEYEXPIRED < SystemCallError; Errno = 127; end
130
+ class EKEYREVOKED < SystemCallError; Errno = 128; end
131
+ class EKEYREJECTED < SystemCallError; Errno = 129; end
132
+ class EOWNERDEAD < SystemCallError; Errno = 130; end
133
+ class ENOTRECOVERABLE < SystemCallError; Errno = 131; end
134
+ class ERFKILL < SystemCallError; Errno = 132; end
135
+ class EHWPOISON < SystemCallError; Errno = 133; end
136
+ end
137
+ end
@@ -5,29 +5,174 @@ end
5
5
 
6
6
  require "opal-file/version"
7
7
 
8
+ require "opal-file_test"
9
+ require "opal-file/stat"
10
+
8
11
  if RUBY_ENGINE == "opal"
9
12
  require "native"
10
13
 
11
14
  class File
12
15
  class << self
13
- def read(filename, options)
14
- _fs.JS.readFileSync(filename, { encoding: "utf8" }.to_n)
16
+ def method_missing(name, *args)
17
+ FileTest.public_send(name, *args)
15
18
  end
16
19
 
17
- def write(filename, content)
18
- _fs.JS.writeFileSync(filename, content.to_s)
20
+ def atime(filename)
21
+ File::Stat.new(filename).atime
22
+ end
23
+
24
+ def birthtime(filename)
25
+ File::Stat.new(filename).birthtime
26
+ end
27
+
28
+ def chmod(mode, *filename)
29
+ handle_errno do
30
+ filename.each do |f|
31
+ fs.JS.chmodSync(f, mode)
32
+ end
33
+ end
34
+ end
35
+
36
+ def chown(owner, group, *filename)
37
+ handle_errno do
38
+ filename.each do |f|
39
+ fs.JS.chownSync(f, owner, group)
40
+ end
41
+ end
19
42
  end
20
43
 
21
44
  def unlink(*filename)
22
- filename.each do |f|
23
- _fs.JS.unlinkSync(f.to_s)
45
+ handle_errno do
46
+ filename.each do |f|
47
+ fs.JS.unlinkSync(f.to_s)
48
+ end
49
+ end
50
+ end
51
+
52
+ alias delete unlink
53
+
54
+ # overwrites Opal corelib/file method
55
+ def directory?(filename)
56
+ FileTest.directory?(filename)
57
+ end
58
+
59
+ # overwrites Opal corelib/file method
60
+ def exist?(filename)
61
+ FileTest.exist?(filename)
62
+ end
63
+
64
+ # overwrites Opal corelib/file method
65
+ def exists?(filename)
66
+ FileTest.exists?(filename)
67
+ end
68
+
69
+ def fnmatch?(pattern, path, flags = 0)
70
+ raise NotImplementedError
71
+ end
72
+
73
+ alias fnmatch fnmatch?
74
+
75
+ def ftype(filename)
76
+ File.lstat(filename).ftype
77
+ end
78
+
79
+ def lchmod(mode, *filename)
80
+ handle_errno do
81
+ filename.each do |f|
82
+ fs.JS.lchmodSync(f, mode)
83
+ end
24
84
  end
25
85
  end
26
86
 
87
+ def lchown(owner, group, *filename)
88
+ handle_errno do
89
+ filename.each do |f|
90
+ fs.JS.lchownSync(f, owner, group)
91
+ end
92
+ end
93
+ end
94
+
95
+ def link(old, new)
96
+ handle_errno do
97
+ fs.JS.linkSync(old, new)
98
+ end
99
+ end
100
+
101
+ def lstat(filename)
102
+ File::Stat.from_lstat(filename)
103
+ end
104
+
105
+ def mkfifo(file_name, mode = 0666)
106
+ raise NotImplementedError
107
+ end
108
+
109
+ def path(filename)
110
+ # TODO: fd
111
+ filename
112
+ end
113
+
114
+ def readlink(filename)
115
+ handle_errno do
116
+ fs.JS.readlinkSync(filename)
117
+ end
118
+ end
119
+
120
+ # TODO: not raise
121
+ alias realdirpath realpath
122
+
123
+ def rename(from, to)
124
+ handle_errno do
125
+ fs.JS.renameSync(from, to)
126
+ end
127
+ end
128
+
129
+ def stat(filename)
130
+ File::Stat.new(filename)
131
+ end
132
+
133
+ def symlink(old, new)
134
+ handle_errno do
135
+ fs.JS.symlinkSync(old, new)
136
+ end
137
+ end
138
+
139
+ def truncate(path, length)
140
+ handle_errno do
141
+ fs.JS.truncateSync(path, length)
142
+ end
143
+ end
144
+
145
+ def umask(umask = nil)
146
+ raise NotImplementedError
147
+ end
148
+
149
+ def utime(atime, mtime, *filename)
150
+ handle_errno do
151
+ filename.each do |f|
152
+ fs.JS.utimesSync(f, atime.to_i, mtime.to_i) # TODO: s ? ms ?
153
+ end
154
+ end
155
+ end
156
+
157
+ # IO
158
+
159
+ def read(filename, options)
160
+ fs.JS.readFileSync(filename, { encoding: "utf8" }.to_n)
161
+ end
162
+
163
+ def write(filename, content)
164
+ fs.JS.writeFileSync(filename, content.to_s)
165
+ end
166
+
27
167
  private
28
168
 
29
- def _fs
30
- @_fs ||= `require("fs")`
169
+ def fs
170
+ @fs ||= `require("fs")`
171
+ end
172
+
173
+ def handle_errno
174
+ e = nil
175
+ `try { return #{yield} } catch (#{e}) { #{raise Class.const_get("Errno::#{e.JS[:code]}")} }`
31
176
  end
32
177
  end
33
178
  end
@@ -0,0 +1,214 @@
1
+ require "opal-errno"
2
+
3
+ if RUBY_ENGINE == "opal"
4
+ require "corelib/error"
5
+ require "native"
6
+
7
+ class File
8
+ class Stat
9
+ class << self
10
+ def from_lstat(path)
11
+ File::Stat.new(File::Stat.lstat_sync(path))
12
+ end
13
+
14
+ def stat_sync(file)
15
+ e = nil
16
+ `try { return require("fs").statSync(file) } catch(#{e}) { #{raise Class.const_get("Errno::#{e.JS[:code]}")} }`
17
+ end
18
+
19
+ def lstat_sync(file)
20
+ e = nil
21
+ `try { return require("fs").lstatSync(file) } catch (#{e}) { #{raise Class.const_get("Errno::#{e.JS[:code]}")} }`
22
+ end
23
+ end
24
+
25
+ def initialize(path)
26
+ if `typeof path === "string"`
27
+ @stat = File::Stat.stat_sync(path)
28
+ raise Class.const_get("Errno::#{@stat}") if `typeof this.stat === "string"`
29
+ else
30
+ @stat = path
31
+ end
32
+ end
33
+
34
+ def atime
35
+ ms_to_time @stat.JS[:atimeMs]
36
+ end
37
+
38
+ def birthtime
39
+ ms_to_time @stat.JS[:birthtimeMs]
40
+ end
41
+
42
+ def blksize
43
+ @stat.JS[:blksize]
44
+ end
45
+
46
+ def blockdev?
47
+ @stat.JS.isBlockDevice()
48
+ end
49
+
50
+ def blocks
51
+ @stat.JS[:blocks]
52
+ end
53
+
54
+ def chardev?
55
+ @stat.JS.isCharacterDevice()
56
+ end
57
+
58
+ def ctime
59
+ ms_to_time @stat.JS[:ctimeMs]
60
+ end
61
+
62
+ def dev
63
+ @stat.JS[:dev]
64
+ end
65
+
66
+ def dev_major
67
+ raise NotImplementedError
68
+ end
69
+
70
+ def dev_minor
71
+ raise NotImplementedError
72
+ end
73
+
74
+ def directory?
75
+ @stat.JS.isDirectory()
76
+ end
77
+
78
+ def executable?
79
+ raise NotImplementedError
80
+ end
81
+
82
+ def executable_real?
83
+ raise NotImplementedError
84
+ end
85
+
86
+ def file?
87
+ @stat.JS.isFile()
88
+ end
89
+
90
+ def ftype
91
+ if file? then "file"
92
+ elsif directory? then "directory"
93
+ elsif chardev? then "characterSpecial"
94
+ elsif blockdev? then "blockSpecial"
95
+ elsif pipe? then "fifo"
96
+ elsif symlink? then "link"
97
+ elsif socket? then "socket"
98
+ else "unknown"
99
+ end
100
+ end
101
+
102
+ def gid
103
+ @stat.JS[:gid]
104
+ end
105
+
106
+ def grpowned?
107
+ raise NotImplementedError
108
+ end
109
+
110
+ def ino
111
+ @stat.JS[:ino]
112
+ end
113
+
114
+ def mode
115
+ @stat.JS[:mode]
116
+ end
117
+
118
+ def mtime
119
+ ms_to_time @stat.JS[:mtimeMs]
120
+ end
121
+
122
+ def nlink
123
+ @stat.JS[:nlink]
124
+ end
125
+
126
+ def owned?
127
+ raise NotImplementedError
128
+ end
129
+
130
+ def pipe?
131
+ @stat.JS.isFIFO()
132
+ end
133
+
134
+ def rdev
135
+ @stat.JS[:rdev]
136
+ end
137
+
138
+ def rdev_major
139
+ raise NotImplementedError
140
+ end
141
+
142
+ def rdev_minor
143
+ raise NotImplementedError
144
+ end
145
+
146
+ def readable?
147
+ raise NotImplementedError
148
+ end
149
+
150
+ def readable_real?
151
+ raise NotImplementedError
152
+ end
153
+
154
+ def setgid?
155
+ raise NotImplementedError
156
+ end
157
+
158
+ def setuid?
159
+ raise NotImplementedError
160
+ end
161
+
162
+ def size
163
+ @stat.JS[:size]
164
+ end
165
+
166
+ def size?
167
+ _size = size
168
+ _size == 0 ? nil : _size
169
+ end
170
+
171
+ def socket?
172
+ @stat.JS.isSocket()
173
+ end
174
+
175
+ def sticky?
176
+ mode & 0001000;
177
+ end
178
+
179
+ def symlink?
180
+ @stat.JS.isSymbolicLink()
181
+ end
182
+
183
+ def uid
184
+ @stat.JS[:uid]
185
+ end
186
+
187
+ def world_readable?
188
+ raise NotImplementedError
189
+ end
190
+
191
+ def world_writable?
192
+ raise NotImplementedError
193
+ end
194
+
195
+ def writable?
196
+ raise NotImplementedError
197
+ end
198
+
199
+ def writable_real?
200
+ raise NotImplementedError
201
+ end
202
+
203
+ def zero?
204
+ size == 0
205
+ end
206
+
207
+ private
208
+
209
+ def ms_to_time(ms)
210
+ Time.at((ms / 1000).to_i, (ms % 1000) * 1000)
211
+ end
212
+ end
213
+ end
214
+ end
@@ -1,3 +1,3 @@
1
- module OpalFile
2
- VERSION = "0.1.0"
3
- end
1
+ module OpalFile
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,39 @@
1
+ require "opal-file/stat"
2
+
3
+ if RUBY_ENGINE == "opal"
4
+ require "native"
5
+
6
+ module FileTest
7
+ class << self
8
+ def method_missing(name, file)
9
+ File.lstat(file).public_send(name)
10
+ rescue SystemCallError
11
+ false
12
+ end
13
+
14
+ def empty?(file)
15
+ zero?(file)
16
+ end
17
+
18
+ def exist?(file)
19
+ `require("fs")`.existsSync(file)
20
+ end
21
+
22
+ alias exists? exist?
23
+
24
+ def identical?(file1, file2)
25
+ raise NotImplementedError
26
+ end
27
+
28
+ def size(file)
29
+ File.lstat(file).size
30
+ end
31
+
32
+ def size?(file)
33
+ size(file)
34
+ rescue SystemCallError
35
+ nil
36
+ end
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka
@@ -65,8 +65,11 @@ files:
65
65
  - Rakefile
66
66
  - bin/console
67
67
  - bin/setup
68
+ - lib/opal-errno.rb
68
69
  - lib/opal-file.rb
70
+ - lib/opal-file/stat.rb
69
71
  - lib/opal-file/version.rb
72
+ - lib/opal-file_test.rb
70
73
  - lib/opal/file.rb
71
74
  - opal-file.gemspec
72
75
  homepage: https://github.com/Narazaka/opal-file