This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lfs.pir
694 lines (565 loc) · 14.6 KB
/
lfs.pir
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# Copyright (C) 2007-2009, Parrot Foundation.
# $Id$
=head1 Lua File System Library v1.4.0
=head2 Description
LuaFileSystem is a Lua library developed to complement the set of functions
related to file systems offered by the standard Lua distribution.
LuaFileSystem offers a portable way to access the underlying directory
structure and file attributes.
See original on L<http://luaforge.net/projects/luafilesystem/>
=head3 Functions
=over 4
=cut
.HLL 'lua'
.loadlib 'lua_group'
.namespace [ 'lfs' ]
.sub '__onload' :anon :load
# print "__onload lfs\n"
.const 'Sub' entry = 'luaopen_lfs'
set_hll_global 'luaopen_lfs', entry
.end
.sub 'luaopen_lfs'
# print "luaopen_lfs\n"
.local pmc _lua__GLOBAL
_lua__GLOBAL = get_hll_global '_G'
new $P1, 'LuaString'
.local pmc _lfs
new _lfs, 'LuaTable'
set $P1, 'lfs'
_lua__GLOBAL[$P1] = _lfs
$P2 = split "\n", <<'LIST'
attributes
chdir
currentdir
dir
lock
mkdir
rmdir
symlinkattributes
touch
unlock
LIST
lua_register($P1, _lfs, $P2)
new $P2, 'LuaString'
set $P2, "Copyright (C) 2007-2009, Parrot Foundation"
set $P1, "_COPYRIGHT"
_lfs[$P1] = $P2
set $P2, "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution"
set $P1, "_DESCRIPTION"
_lfs[$P1] = $P2
set $P2, "LuaFileSystem 1.4.0"
set $P1, "_VERSION"
_lfs[$P1] = $P2
.return (_lfs)
.end
.sub 'check_file' :anon
.param int narg
.param pmc fh
.param string funcname
.local pmc res
res = lua_checkudata(narg, fh, 'FileHandle')
unless null res goto L1
lua_error(funcname, ": closed file")
L1:
.return (res)
.end
=item C<lfs.attributes (filepath [, aname])>
Returns a table with the file attributes corresponding to C<filepath> (or
C<nil> followed by an error message in case of error). If the second optional
argument is given, then only the value of the named attribute is returned
(this use is equivalent to C<lfs.attributes(filepath).aname>, but the table
is not created and only one attribute is retrieved from the O.S.). The
attributes are described as follows; attribute C<mode> is a string, all the
others are numbers, and the time related attributes use the same time
reference of C<os.time>:
=over 4
=item B<"dev">
on Unix systems, this represents the device that the inode resides on.
On Windows systems, represents the drive number of the disk containing the file
=item B<"ino">
on Unix systems, this represents the inode number.
On Windows systems this has no meaning
=item B<"mode">
string representing the associated protection mode (the values could be
C<file>, C<directory>, C<link>, C<socket>, C<named pipe>, C<char device>,
C<block device> or C<other>)
=item B<"nlink">
number of hard links to the file
=item B<"uid">
user-id of owner (Unix only, always 0 on Windows)
=item B<"gid">
group-id of owner (Unix only, always 0 on Windows)
=item B<"rdev">
on Unix systems, represents the device type, for special file inodes.
On Windows systems represents the same as dev
=item B<"access">
time of last access
=item B<"modification">
time of last data modification
=item B<"changes">
time of last file status change
=item B<"size">
file size, in bytes
=item B<"blocks">
block allocated for file; (Unix only)
=item B<"blksize">
optimal file system I/O blocksize; (Unix only)
=back
=cut
.macro FILE_INFO(func, filepath, aname)
.local pmc res
.local pmc members
$S1 = lua_checkstring(1, .filepath)
$S0 = $S1
new members, 'Hash'
.const 'Sub' st_mode = 'st_mode'
members['mode'] = st_mode
.const 'Sub' st_dev = 'st_dev'
members['dev'] = st_dev
.const 'Sub' st_ino = 'st_ino'
members['ino'] = st_ino
.const 'Sub' st_nlink = 'st_nlink'
members['nlink'] = st_nlink
.const 'Sub' st_uid = 'st_uid'
members['uid'] = st_uid
.const 'Sub' st_gid = 'st_gid'
members['gid'] = st_gid
.const 'Sub' st_rdev = 'st_rdev'
members['rdev'] = st_rdev
.const 'Sub' st_atime = 'st_atime'
members['access'] = st_atime
.const 'Sub' st_mtime = 'st_mtime'
members['modification'] = st_mtime
.const 'Sub' st_ctime = 'st_ctime'
members['change'] = st_ctime
.const 'Sub' st_size = 'st_size'
members['size'] = st_size
.const 'Sub' st_blocks = 'st_blocks'
members['blocks'] = st_blocks
.const 'Sub' st_blksize = 'st_blksize'
members['blksize'] = st_blksize
new $P0, 'OS'
push_eh _handler
$P1 = $P0. .func($S1)
pop_eh
if null .aname goto L1
$I0 = isa .aname, 'LuaString'
unless $I0 goto L2
$S2 = .aname
$P2 = members[$S2]
unless null $P2 goto L3
lua_checkoption(2, $S2, '')
L3:
res = $P2($P1)
.return (res)
L2:
$I0 = isa .aname, 'LuaTable'
unless $I0 goto L1
res = .aname
goto L4
L1:
new res, 'LuaTable'
L4:
.local pmc iter
new iter, 'Iterator', members
new $P2, 'LuaString'
L5:
unless iter goto L6
$S2 = shift iter
set $P2, $S2
$P3 = members[$S2]
$P4 = $P3($P1)
res[$P2] = $P4
goto L5
L6:
.return (res)
_handler:
.local pmc nil
.local pmc msg
new nil, 'LuaNil'
new msg, 'LuaString'
$S0 = concat "cannot obtain information from file `", $S0
$S0 = concat "'"
set msg, $S0
.return (nil, msg)
.endm
.sub 'attributes'
.param pmc filepath :optional
.param pmc aname :optional
.param pmc extra :slurpy
.FILE_INFO('stat', filepath, aname)
.end
.sub 'st_dev' :anon
.param pmc st
$I0 = st[0]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_ino' :anon
.param pmc st
$I0 = st[1]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.const int S_IFIFO = 0x1000 # named pipe (fifo)
.const int S_IFCHR = 0x2000 # character special
.const int S_IFDIR = 0x4000 # directory
.const int S_IFBLK = 0x6000 # block special
.const int S_IFREG = 0x8000 # regular
.const int S_IFLNK = 0xA000 # symbolic link
.const int S_IFSOCK = 0xC000 # socket
.const int S_IFMT = 0xF000 # type of file mask
.sub 'st_mode' :anon
.param pmc st
$I0 = st[2]
$I0 &= S_IFMT
new $P0, 'LuaString'
unless $I0 == S_IFREG goto L1
set $P0, 'file'
.return ($P0)
L1:
unless $I0 == S_IFDIR goto L2
set $P0, 'dir'
.return ($P0)
L2:
unless $I0 == S_IFLNK goto L3
set $P0, 'link'
.return ($P0)
L3:
unless $I0 == S_IFSOCK goto L4
set $P0, 'socket'
.return ($P0)
L4:
unless $I0 == S_IFIFO goto L5
set $P0, 'named pipe'
.return ($P0)
L5:
unless $I0 == S_IFCHR goto L6
set $P0, 'char device'
.return ($P0)
L6:
unless $I0 == S_IFBLK goto L7
set $P0, 'block device'
.return ($P0)
L7:
set $P0, 'other'
.return ($P0)
.end
.sub 'st_nlink' :anon
.param pmc st
$I0 = st[3]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_uid' :anon
.param pmc st
$I0 = st[4]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_gid' :anon
.param pmc st
$I0 = st[5]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_rdev' :anon
.param pmc st
$I0 = st[6]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_size' :anon
.param pmc st
$I0 = st[7]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_atime' :anon
.param pmc st
$I0 = st[8]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_mtime' :anon
.param pmc st
$I0 = st[9]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_ctime' :anon
.param pmc st
$I0 = st[10]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
.end
.sub 'st_blksize' :anon
.param pmc st
$I0 = exists st[11]
unless $I0 goto L1
$I0 = st[11]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
L1:
new $P0, 'LuaNil'
.return ($P0)
.end
.sub 'st_blocks' :anon
.param pmc st
$I0 = exists st[12]
unless $I0 goto L1
$I0 = st[12]
new $P0, 'LuaNumber'
set $P0, $I0
.return ($P0)
L1:
new $P0, 'LuaNil'
.return ($P0)
.end
=item C<lfs.chdir (path)>
Changes the current working directory to the given C<path>.
Returns C<true> in case of success or C<nil> plus an error string.
=cut
.sub 'chdir'
.param pmc path :optional
.param pmc extra :slurpy
.local pmc res
$S1 = lua_checkstring(1, path)
$S0 = $S1
new $P0, 'OS'
push_eh _handler
$P0.'chdir'($S1)
pop_eh
new res, 'LuaBoolean'
set res, 1
.return (res)
_handler:
.local pmc nil
.local pmc msg
.local pmc e
.local string s
.get_results (e)
s = e
new nil, 'LuaNil'
new msg, 'LuaString'
$S0 = concat "Unable to change working directory to '", $S0
$S0 = concat "'\n"
$S0 = concat s
$S0 = concat "\n"
set msg, $S0
.return (nil, msg)
.end
=item C<lfs.currentdir ()>
Returns a string with the current working directory or C<nil> plus an error
string.
=cut
.sub 'currentdir'
.param pmc extra :slurpy
.local pmc res
new $P0, 'OS'
push_eh _handler
$S0 = $P0.'cwd'()
pop_eh
new res, 'LuaString'
set res, $S0
.return (res)
_handler:
.local pmc nil
.local pmc msg
.local pmc e
.local string s
.get_results (e)
s = e
new nil, 'LuaNil'
new msg, 'LuaString'
set msg, s
.return (nil, msg)
.end
=item C<lfs.dir (path)>
Lua iterator over the entries of a given directory. Each time the iterator is
called it returns a string with an entry of the directory; C<nil> is returned
when there is no more entries. Raises an error if C<path> is not a directory.
=cut
.sub 'dir'
.param pmc path :optional
.param pmc extra :slurpy
.local pmc res
$S1 = lua_checkstring(1, path)
$S0 = $S1
new $P0, 'OS'
push_eh _handler
$P1 = $P0.'readdir'($S1)
pop_eh
.lex 'upvar_dir', $P1
.const 'Sub' dir_aux = 'dir_aux'
res = newclosure dir_aux
.return (res)
_handler:
.local pmc e
.local string msg
.get_results (e)
msg = e
$S0 = lua_x_error("cannot open ", $S0, ": ", msg)
e = $S0
rethrow e
.end
.sub 'dir_aux' :anon :lex :outer(dir)
.local pmc res
$P1 = find_lex 'upvar_dir'
unless $P1 goto L1
$S1 = shift $P1
new res, 'LuaString'
set res, $S1
.return (res)
L1:
new res, 'LuaNil'
.return (res)
.end
=item C<lfs.lock (filehandle, mode[, start[, length]])>
Locks a file or a part of it. This function works on I<open files>; the file
handle should be specified as the first argument. The string C<mode> could be
either C<r> (for a read/shared lock) or C<w> (for a write/exclusive lock).
The optional arguments C<start> and C<length> can be used to specify a
starting point and its length; both should be numbers.
Returns C<true> if the operation was successful; in case of error, it returns
C<nil> plus an error string.
NOT YET IMPLEMENTED.
=cut
.sub 'lock'
.param pmc filehandle :optional
.param pmc mode :optional
.param pmc start :optional
.param pmc length_ :optional
.param pmc extra :slurpy
$P1 = check_file(1, filehandle, 'lock')
$S2 = lua_checkstring(2, mode)
$I3 = lua_optint(3, start, 0)
$I4 = lua_optint(4, length_, 0)
not_implemented()
.end
=item C<lfs.mkdir (dirname)>
Creates a new directory. The argument is the name of the new directory.
Returns C<true> if the operation was successful; in case of error, it returns
C<nil> plus an error string.
=cut
.sub 'mkdir'
.param pmc dirname :optional
.param pmc extra :slurpy
.local pmc res
$S1 = lua_checkstring(1, dirname)
new $P0, 'OS'
push_eh _handler
$I1 = 0o775
$P0.'mkdir'($S1, $I1)
pop_eh
new res, 'LuaBoolean'
set res, 1
.return (res)
_handler:
.local pmc nil
.local pmc msg
.local pmc e
.local string s
.get_results (e)
s = e
new nil, 'LuaNil'
new msg, 'LuaString'
set msg, s
.return (nil, msg)
.end
=item C<lfs.rmdir (dirname)>
Removes an existing directory. The argument is the name of the directory.
Returns C<true> if the operation was successful; in case of error, it returns
C<nil> plus an error string.
=cut
.sub 'rmdir'
.param pmc dirname :optional
.param pmc extra :slurpy
.local pmc res
$S1 = lua_checkstring(1, dirname)
new $P0, 'OS'
push_eh _handler
$P0.'rm'($S1)
pop_eh
new res, 'LuaBoolean'
set res, 1
.return (res)
_handler:
.local pmc nil
.local pmc msg
.local pmc e
.local string s
.get_results (e)
s = e
new nil, 'LuaNil'
new msg, 'LuaString'
set msg, s
.return (nil, msg)
.end
=item C<lfs.symlinkattributes (filepath [, aname])>
Identical to C<lfs.attributes> except that it obtains information about
the link itself (not the file it refers to).
=cut
.sub 'symlinkattributes'
.param pmc filepath :optional
.param pmc aname :optional
.param pmc extra :slurpy
.FILE_INFO('lstat', filepath, aname)
.end
=item C<lfs.touch (filepath [, atime [, mtime]])>
Set access and modification times of a file. This function is a bind to
C<utime> function. The first argument is the filename, the second argument
(C<atime>) is the access time, and the third argument (C<mtime>) is the
modification time. Both times are provided in seconds (which should be
generated with Lua standard function C<os.date>). If the modification time is
omitted, the access time provided is used; if both times are omitted, the
current time is used.
Returns C<true> if the operation was successful; in case of error, it returns
C<nil> plus an error string.
NOT YET IMPLEMENTED.
=cut
.sub 'touch'
.param pmc filepath :optional
.param pmc atime :optional
.param pmc mtime :optional
.param pmc extra :slurpy
$S1 = lua_checkstring(1, filepath)
not_implemented()
.end
=item C<lfs.unlock (filehandle[, start[, length]])>
Unlocks a file or a part of it. This function works on I<open files>; the file
handle should be specified as the first argument. The optional arguments
C<start> and C<length> can be used to specify a starting point and its length;
both should be numbers.
Returns C<true> if the operation was successful; in case of error, it returns
C<nil> plus an error string.
NOT YET IMPLEMENTED.
=cut
.sub 'unlock'
.param pmc filehandle :optional
.param pmc start :optional
.param pmc length_ :optional
.param pmc extra :slurpy
$P1 = check_file(1, filehandle, 'unlock')
$I2 = lua_optint(2, start, 0)
$I3 = lua_optint(3, length_, 0)
not_implemented()
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: