-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module (chicken file posix)
632 lines (438 loc) · 19.7 KB
/
Module (chicken file posix)
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
[[tags: manual]]
[[toc:]]
== Module (chicken file posix)
This module provides various operations on files and directories that
are more POSIX-oriented than the generic higher-level operations from
[[Module (chicken file)]].
Note that the following definitions are not all available on non-UNIX
systems like Windows. See below for Windows specific notes.
All errors related to failing file-operations will signal a condition
of kind {{(exn i/o file)}}.
=== Constants
==== File-control Commands
<constant>fcntl/dupfd</constant><br>
<constant>fcntl/getfd</constant><br>
<constant>fcntl/setfd</constant><br>
<constant>fcntl/getfl</constant><br>
<constant>fcntl/setfl</constant>
Operations used with {{file-control}}.
'''NOTE''': On native Windows builds (all except cygwin), these are
all defined as zero. The {{file-control}} procedure to use these with
is also unimplemented and will raise an error when called.
==== File positions
<constant>seek/cur</constant><br>
<constant>seek/set</constant><br>
<constant>seek/end</constant><br>
File positions for {{set-file-position!}}.
==== Standard I/O file-descriptors
<constant>fileno/stdin</constant><br>
<constant>fileno/stdout</constant><br>
<constant>fileno/stderr</constant>
Standard I/O file descriptor numbers, used with procedures
such as {{open-input-file*}} which take file descriptors.
==== Open flags
<constant>open/rdonly</constant><br>
<constant>open/wronly</constant><br>
<constant>open/rdwr</constant><br>
<constant>open/read</constant><br>
<constant>open/write</constant><br>
<constant>open/creat</constant><br>
<constant>open/append</constant><br>
<constant>open/excl</constant><br>
<constant>open/noctty</constant><br>
<constant>open/nonblock</constant><br>
<constant>open/trunc</constant><br>
<constant>open/sync</constant><br>
<constant>open/fsync</constant><br>
<constant>open/binary</constant><br>
<constant>open/text</constant>
Open flags used with the {{file-open}} procedure. {{open/read}} is a
convenience synonym for {{open/rdonly}}, as is {{open/write}}
for {{open/wronly}}.
'''NOTE''': On native Windows builds (all except cygwin),
{{open/noctty}}, {{open/nonblock}}, {{open/fsync}} and {{open/sync}}
are defined as zero because the corresponding flag doesn't exist.
This means you can safely add these to any set of flags when opening a
file or pipe, but it simply won't have an effect.
==== Open flags for create-pipe
<constant>open/noinherit</constant>
This variable is a mode value for {{create-pipe}}. Useful when
spawning a child process on Windows. On UNIX it is defined as zero,
so you can safely pass it there, but it will have no effect.
==== Permission bits
<constant>perm/irusr</constant><br>
<constant>perm/iwusr</constant><br>
<constant>perm/ixusr</constant><br>
<constant>perm/irgrp</constant><br>
<constant>perm/iwgrp</constant><br>
<constant>perm/ixgrp</constant><br>
<constant>perm/iroth</constant><br>
<constant>perm/iwoth</constant><br>
<constant>perm/ixoth</constant><br>
<constant>perm/irwxu</constant><br>
<constant>perm/irwxg</constant><br>
<constant>perm/irwxo</constant><br>
<constant>perm/isvtx</constant><br>
<constant>perm/isuid</constant><br>
<constant>perm/isgid</constant>
Permission bits used with, for example, {{file-open}}.
'''NOTE''': On native Windows builds (all except cygwin),
{{perm/isvtx}}, {{perm/isuid}} and {{perm/isgid}} are defined as zero
because the corresponding permission doesn't exist. This means you
can safely add these to any set of flags when opening a file or pipe,
but it simply won't have an effect.
=== Information about files
==== directory?
<procedure>(directory? FILE)</procedure>
Returns {{#t}} if {{FILE}} designates a directory. Otherwise, it returns {{#f}}.
{{FILE}} may be a pathname, a file-descriptor or a port object.
==== file-type
<procedure>(file-type FILE [LINK [ERROR]])</procedure>
Returns the file-type for {{FILE}}, which should be a filename, a file-descriptor
or a port object. If {{LINK}} is given and true, symbolic-links are
not followed:
regular-file
directory
fifo
socket
symbolic-link
character-device
block-device
Note that not all types are supported on every platform.
If {{ERROR}} is given and false, then {{file-type}} returns {{#f}} if the file does not exist;
otherwise, it signals an error.
==== character-device?
==== block-device?
==== socket?
<procedure>(character-device? FILE)</procedure><br>
<procedure>(block-device? FILE)</procedure><br>
<procedure>(socket? FILE)</procedure>
These procedures return {{#t}} if {{FILE}} given is of the
appropriate type. {{FILE}} may be a filename, a file-descriptor or a port object.
Note that these operations follow symbolic links. If the file does
not exist, {{#f}} is returned.
==== regular-file?
<procedure>(regular-file? FILE)</procedure>
Returns true, if {{FILE}} names a regular file (not a directory, socket, etc.) This operation follows symbolic links; use either {{symbolic-link?}} or {{file-type}} if you need to test for symlinks. {{FILE}} may refer to a filename, file descriptor or ports object.
=== Fifos
==== create-fifo
<procedure>(create-fifo FILENAME [MODE])</procedure>
Creates a FIFO with the name {{FILENAME}} and the permission bits
{{MODE}}, which defaults to
<enscript highlight=scheme>
(+ perm/irwxu perm/irwxg perm/irwxo)
</enscript>
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== fifo?
<procedure>(fifo? FILE)</procedure>
Returns {{#t}} if {{FILE}} names a FIFO. {{FILE}} may be a filename,
a port or a file-descriptor.
=== Retrieving file attributes
==== file-access-time
==== file-change-time
==== file-modification-time
<procedure>(file-access-time FILE)</procedure><br>
<procedure>(file-change-time FILE)</procedure><br>
<procedure>(file-modification-time FILE)</procedure>
Returns time (in seconds) of the last access, inode change or content
modification of {{FILE}}, respectively. {{FILE}} may be a filename, a
file-descriptor or a file-backed port. If the file does not exist, an
error is signaled.
==== set-file-times!
<procedure>(set-file-times! FILE [MTIME [ATIME]])</procedure>
Sets the time of last modification {{MTIME}} and/or time of last
access {{ATIME}} (in seconds) for {{FILE}}. {{FILE}} may be a
filename, a file-descriptor or a file-backed port. If the file does
not exist, an error is signaled.
If neither {{MTIME}} nor {{ATIME}} is supplied, the current time is
used. If only {{MTIME}} is supplied, {{ATIME}} will be set to the
same value. If an argument is {{#f}}, it will not be changed.
Consequently, if only {{MTIME}} is passed and it is {{#f}}, {{ATIME}}
is assumed to be {{#f}} as well and neither will be changed.
==== file-stat
<procedure>(file-stat FILE [LINK])</procedure>
Returns a 13-element vector with the following contents:
<table>
<tr><th>index</th>
<th>value</th>
<th>field</th>
<th>notes</th></tr>
<tr><td>0</td>
<td>inode number</td>
<td>{{st_ino}}</td>
<td></td></tr>
<tr><td>1</td>
<td>mode</td>
<td>{{st_mode}}</td>
<td>bitfield combining file permissions and file type</td></tr>
<tr><td>2</td>
<td>number of hard links</td>
<td>{{st_nlink}}</td>
<td></td></tr>
<tr><td>3</td>
<td>UID of owner</td>
<td>{{st_uid}}</td>
<td>as with {{file-owner}}</td></tr>
<tr><td>4</td>
<td>GID of owner</td>
<td>{{st_gid}}</td>
<td></td></tr>
<tr><td>5</td>
<td>size</td>
<td>{{st_size}}</td>
<td>as with {{file-size}}</td></tr>
<tr><td>6</td>
<td>access time</td>
<td>{{st_atime}}</td>
<td>as with {{file-access-time}}</td></tr>
<tr><td>7</td>
<td>change time</td>
<td>{{st_ctime}}</td>
<td>as with {{file-change-time}}</td></tr>
<tr><td>8</td>
<td>modification time</td>
<td>{{st_mtime}}</td>
<td>as with {{file-modification-time}}</td></tr>
<tr><td>9</td>
<td>parent device ID </td>
<td>{{st_dev}}</td>
<td>ID of device on which this file resides</td></tr>
<tr><td>10</td>
<td>device ID</td>
<td>{{st_rdev}}</td>
<td>device ID for special files (i.e. the raw major/minor number)</td></tr>
<tr><td>11</td>
<td>block size</td>
<td>{{st_blksize}}</td>
<td></td></tr>
<tr><td>12</td>
<td>number of blocks allocated</td>
<td>{{st_blocks}}</td>
<td></td></tr>
</table>
On Windows systems, the last 4 values are undefined.
By default, symbolic links are followed and
the status of the referenced file is returned;
however, if the optional argument {{LINK}} is given and
not {{#f}}, the status of the link itself is returned.
{{FILE}} may be a filename, port or file-descriptor.
Note that for very large files, the {{file-size}} value may be an
inexact integer.
==== file-position
<procedure>(file-position FILE)</procedure>
Returns the current file position of {{FILE}}, which should be a
port or a file-descriptor.
==== file-size
<procedure>(file-size FILE)</procedure>
Returns the size of the file designated by {{FILE}}. {{FILE}}
may be a filename, a file-descriptor or a port object. If the file does not exist,
an error is signaled. Note that for very large files, {{file-size}} may
return an inexact integer.
==== file-owner
<procedure>(file-owner FILE)</procedure>
Returns the user-id of {{FILE}} (an exact integer). {{FILE}} may be a
filename, a file-descriptor or a port object.
==== set-file-owner!
<procedure>(set-file-owner! FILE UID)</procedure>
<procedure>(set! (file-owner FILE) UID)</procedure>
Changes the ownership of {{FILE}} to user-id {{UID}} (which should be
an exact integer) using the {{chown()}} system call. {{FILE}} may be
a filename, a file-descriptor or a port object.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== file-group
<procedure>(file-group FILE)</procedure>
Returns the group-id of {{FILE}}. {{FILE}} may be a filename, a
file-descriptor or a port object.
==== set-file-group!
<procedure>(set-file-group! FILE GID)</procedure>
<procedure>(set! (file-group FILE) GID)</procedure>
Changes the group ownership of {{FILE}} to group-id {{GID}} (which
should be an exact integer) using the {{chgrp()}} system call.
{{FILE}} may be a filename, a file-descriptor or a port object.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== file-permissions
<procedure>(file-permissions FILE)</procedure>
Returns the permission bits for {{FILE}}. You can test this value
by performing bitwise operations on the result and the {{perm/...}}
values. {{FILE}} may be a filename, a file-descriptor or a port object.
==== set-file-permissions!
<procedure>(set-file-permissions! FILE MODE)</procedure>
<procedure>(set! (file-permissions FILE) MODE)</procedure>
Changes the current permission bits for {{FILE}} to {{MODE}} using the
{{chmod()}} system call. The {{perm/...}} variables contain the
various permission bits and can be combinded with the {{bitwise-ior}}
procedure. {{FILE}} may be a filename, a file-descriptor or a port
object, {{MODE}} should be a fixnum.
==== file-truncate
<procedure>(file-truncate FILE OFFSET)</procedure>
Truncates the file {{FILE}} to the length {{OFFSET}},
which should be an integer. If the file-size is smaller or equal to
{{OFFSET}} then nothing is done. {{FILE}} should be a filename,
a file-descriptor or a port object.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== set-file-position!
<procedure>(set-file-position! FILE POSITION [WHENCE])</procedure><br>
<procedure>(set! (file-position FILE) POSITION)</procedure>
Sets the current read/write position of {{FILE}} to
{{POSITION}}, which should be an exact integer. {{FILE}}
should be a port or a file-descriptor. {{WHENCE}} specifies
how the position is to interpreted and should be one of the values
{{seek/set, seek/cur}} and {{seek/end}}. It defaults to
{{seek/set}}.
Exceptions: {{(exn bounds)}}, {{(exn i/o file)}}
==== file-creation-mode
<procedure>(file-creation-mode [MODE])</procedure>
Returns the initial file permissions used for newly created files
(as with {{umask(2)}}). If {{MODE}} is supplied, the mode is
changed to this value. You can set the mode by executing
(set! (file-creation-mode) MODE)
or
(file-creation-mode MODE)
where {{MODE}} is a bitwise combination of one or more of
the {{perm/...}} flags.
=== Hard and symbolic links
==== file-link
<procedure>(file-link OLDNAME NEWNAME)</procedure>
Creates a hard link from {{OLDNAME}} to {{NEWNAME}} (both strings).
==== symbolic-link?
<procedure>(symbolic-link? FILE)</procedure>
Returns true, if {{FILE}} names a symbolic link. If no such file exists, {{#f}}
is returned. This operation does not follow symbolic links itself.
{{FILE}} could be a filename, file descriptor or port object.
==== create-symbolic-link
<procedure>(create-symbolic-link OLDNAME NEWNAME)</procedure>
Creates a symbolic link with the filename {{NEWNAME}} that points
to the file named {{OLDNAME}}.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== read-symbolic-link
<procedure>(read-symbolic-link FILENAME [CANONICALIZE])</procedure>
Returns the filename to which the symbolic link {{FILENAME}} points.
If {{CANONICALIZE}} is given and true, then symbolic links are
resolved repeatedly until the result is not a link.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
=== File descriptors and low-level I/O
==== duplicate-fileno
<procedure>(duplicate-fileno OLD [NEW])</procedure>
If {{NEW}} is given, then the file-descriptor {{NEW}} is opened
to access the file with the file-descriptor {{OLD}}. Otherwise a
fresh file-descriptor accessing the same file as {{OLD}} is returned.
==== file-close
<procedure>(file-close FILENO)</procedure>
Closes the input/output file with the file-descriptor {{FILENO}}.
==== file-open
<procedure>(file-open FILENAME FLAGS [MODE])</procedure>
Opens the file specified with the string {{FILENAME}} and open-flags
{{FLAGS}} using the C function {{open(2)}}. On success a
file-descriptor for the opened file is returned.
{{FLAGS}} is a bitmask of {{open/...}}
values '''or'''ed together using {{bitwise-ior}} (or simply added
together). You must provide exactly one of the access flags {{open/rdonly}}, {{open/wronly}}, or {{open/rdwr}}. Additionally, you may provide zero or more creation flags ({{open/creat}}, {{open/excl}}, {{open/trunc}}, and {{open/noctty}}) and status flags (the remaining {{open/...}} values). For example, to open a possibly new output file for appending:
(file-open "/tmp/hen.txt" (+ open/wronly open/append open/creat))
The optional {{MODE}} should be a bitmask composed of one
or more permission values like {{perm/irusr}} and is only relevant
when a new file is created. The default mode is
{{perm/irwxu | perm/irgrp | perm/iroth}}.
==== file-mkstemp
<procedure>(file-mkstemp TEMPLATE-FILENAME)</procedure>
Create a file based on the given {{TEMPLATE-FILENAME}}, in which
the six last characters must be ''XXXXXX''. These will be replaced
with a string that makes the filename unique. The file descriptor of
the created file and the generated filename is returned. See the
{{mkstemp(3)}} manual page for details on how this function
works. The template string given is not modified.
Example usage:
<enscript highlight=scheme>
(let-values (((fd temp-path) (file-mkstemp "/tmp/mytemporary.XXXXXX")))
(let ((temp-port (open-output-file* fd)))
(format temp-port "This file is ~A.~%" temp-path)
(close-output-port temp-port)))
</enscript>
==== file-read
<procedure>(file-read FILENO SIZE [BUFFER])</procedure>
Reads {{SIZE}} bytes from the file with the file-descriptor
{{FILENO}}. If a string or bytevector is passed in the optional
argument {{BUFFER}}, then this string will be destructively modified
to contain the read data. This procedure returns a list with two values:
the buffer containing the data and the number of bytes read.
==== file-select
<procedure>(file-select READFDLIST WRITEFDLIST [TIMEOUT])</procedure>
Waits until any of the file-descriptors given in the lists
{{READFDLIST}} and {{WRITEFDLIST}} is ready for input or
output, respectively. If the optional argument {{TIMEOUT}} is
given and not false, then it should specify the number of seconds after
which the wait is to be aborted (the value may be a floating point
number). This procedure returns two values:
the lists of file-descriptors ready for input and output, respectively.
{{READFDLIST}} and '''WRITEFDLIST''' may also by file-descriptors
instead of lists. In this case the returned values are booleans
indicating whether input/output is ready by {{#t}} or {{#f}}
otherwise. You can also pass {{#f}} as {{READFDLIST}} or
{{WRITEFDLIST}} argument, which is equivalent to {{()}}.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== file-write
<procedure>(file-write FILENO BUFFER [SIZE])</procedure>
Writes the contents of the string or bytevector {{BUFFER}} into
the file with the file-descriptor {{FILENO}}. If the optional
argument {{SIZE}} is given, then only the specified number of bytes
are written.
==== file-control
<procedure>(file-control FILENO COMMAND [ARGUMENT])</procedure>
Performs the fcntl operation {{COMMAND}} with the given
{{FILENO}} and optional {{ARGUMENT}}. The return value is
meaningful depending on the {{COMMAND}}.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== open-input-file*
==== open-output-file*
<procedure>(open-input-file* FILENO [OPENMODE])</procedure><br>
<procedure>(open-output-file* FILENO [OPENMODE])</procedure>
Opens file for the file-descriptor {{FILENO}} for input or output
and returns a port. {{FILENO}} should be a positive exact integer.
{{OPENMODE}} specifies an additional mode for opening the file
(currently only the keyword {{#:append}} is supported, which opens
an output-file for appending).
==== port->fileno
<procedure>(port->fileno PORT)</procedure>
If {{PORT}} is a file- or tcp-port, then a file-descriptor is returned for
this port. Otherwise an error is signaled.
=== Record locking
These procedures are all unsupported on native Windows builds (all
except cygwin).
==== file-lock
<procedure>(file-lock PORT [START [LEN]])</procedure>
Locks the file associated with {{PORT}} for reading or
writing (according to whether {{PORT}} is an input- or
output-port). {{START}} specifies the starting position in the
file to be locked and defaults to 0. {{LEN}} specifies the length
of the portion to be locked and defaults to {{#t}}, which means
the complete file. {{file-lock}} returns a ''lock''-object.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== file-lock/blocking
<procedure>(file-lock/blocking PORT [START [LEN]])</procedure>
Similar to {{file-lock}}, but if a lock is held on the file,
the current process blocks (including all threads) until the lock is released.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== file-test-lock
<procedure>(file-test-lock PORT [START [LEN]])</procedure>
Tests whether the file associated with {{PORT}} is locked for reading
or writing (according to whether {{PORT}} is an input- or output-port)
and returns either {{#f}} or the process-id of the locking process.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
==== file-unlock
<procedure>(file-unlock LOCK)</procedure>
Unlocks the previously locked portion of a file given in {{LOCK}}.
'''NOTE''': On native Windows builds (all except cygwin), this
procedure is unimplemented and will raise an error.
---
Previous: [[Module (chicken file)]]
Next: [[Module (chicken fixnum)]]