feat: add file create, delete, writeBytes and FileFlags class to IO module

Implement File.create() and File.delete() static methods with async libuv operations, add writeBytes() instance method for writing at offsets, introduce FileFlags constants class mirroring POSIX open() flags, refactor File.open() to accept flag parameters via openWithFlags(), and update documentation and module registration accordingly.
This commit is contained in:
Bob Nystrom
2016-02-20 17:23:42 +00:00
parent 38f48fbce1
commit 60fc3ff59c
21 changed files with 463 additions and 52 deletions
+48
View File
@@ -0,0 +1,48 @@
^title FileFlags Class
Contains constants for the various file flags used to open or create a file.
These correspond directly to the flags that can be passed to the POSIX
[`open()`][open] syscall.
[open]: http://linux.die.net/man/2/open
They are integers and can be bitwise or'ed together to produce a composite
flag.
## Static Methods
### FileFlags.**readOnly**
The file can be read from but not written. Equivalent to `O_RDONLY`.
### FileFlags.**writeOnly**
The file can be written but not read from. Equivalent to `O_WRONLY`.
### FileFlags.**readWrite**
The file can be both read from and written to. Equivalent to `O_RDWR`.
### FileFlags.**sync**
Writes will block until the data has been physically written to the underling
hardware. This does *not* affect whether or the file API is synchronous. File
operations are always asynchronous in Wren and may allow other scheduled fibers
to run.
This is a lower-level flag that ensures that when a write completes, it has
been flushed all the way to disc.
### FileFlags.**create**
Creates a new file if a file at the given path does not already exist.
### FileFlags.**truncate**
If the file already exists and can be written to, its previous contents are
discarded.
### FileFlags.**exclusive**
Ensures that a new file must be created. If a file already exists at the given
path, this flag will cause the operation to fail.
+39 -3
View File
@@ -10,10 +10,25 @@ a file descriptor.
## Static Methods
### File.**create**(path, fn)
Opens the file at `path` for writing and passes it to `fn`. If there is already
a file at that path, it is truncated. After the function returns, the file is
automatically closed.
:::wren
File.create("numbers.txt") {|file|
file.writeBytes("one two three")
}
### File.**delete**(path)
Deletes the file at `path`.
### File.**open**(path, fn)
Opens the file at `path` and passes it to `fn`. After the function returns, the
file is automatically closed.
Opens the file at `path` for reading and passes it to `fn`. After the function
returns, the file is automatically closed.
:::wren
File.open("words.txt") {|file|
@@ -37,9 +52,20 @@ Returns the size in bytes of the contents of the file at `path`.
## Constructors
### File.**create**(path)
Opens the file at `path` for writing. If there is already a file at that path,
it is truncated.
:::wren
var file = File.create("colors.txt")
file.writeBytes("chartreuse lime teal")
file.close()
### File.**open**(path)
Opens the file at `path` for reading.
Opens the file at `path` for reading. You are responsible for closing it when
done with it.
## Methods
@@ -79,3 +105,13 @@ the file.
File.open("example.txt") {|file|
System.print(file.readBytes(6, 2)) //> am a f
}
### **writeBytes**(bytes)
Writes the raw bytes of the string `bytes` to the end of the file.
### **writeBytes**(bytes, offset)
Writes the raw bytes of the string `bytes` to the to the file, starting at
`offset`. Any overlapping bytes already in the file at the offset are
overwritten.
+2
View File
@@ -29,6 +29,7 @@
<ul>
<li><a href="directory.html">Directory</a></li>
<li><a href="file.html">File</a></li>
<li><a href="file-flags.html">FileFlags</a></li>
<li><a href="stat.html">Stat</a></li>
<li><a href="stdin.html">Stdin</a></li>
</ul>
@@ -48,6 +49,7 @@
<ul>
<li><a href="directory.html">Directory</a></li>
<li><a href="file.html">File</a></li>
<li><a href="file-flags.html">FileFlags</a></li>
</ul>
</td>
<td>