Rename std::fs::raw to std::fs::bits

This ensures we use the same naming pattern for modules used to provide
the building blocks of other modules (std::fs::file for example).
parent 46dc7ddf
# Types and methods for manipulating the filesystem.
import std::fs::bits::(self, CREATED_AT, MODIFIED_AT, ACCESSED_AT)
import std::fs::path::(Path, ToPath)
import std::fs::raw::(self, CREATED_AT, MODIFIED_AT, ACCESSED_AT)
import std::io::(Error as IOError)
import std::time::SystemTime
......@@ -15,35 +15,35 @@ import std::time::SystemTime
#
# try! fs.size('/dev/null') # => 0
def size(path: ToPath) !! IOError -> Integer {
try raw.size(path.to_path.to_string)
try bits.size(path.to_path.to_string)
}
# Returns the creation time of a path.
def created_at(path: ToPath) !! IOError -> SystemTime {
try raw.created_at(path.to_path.to_string)
try bits.created_at(path.to_path.to_string)
}
# Returns the modification time of a path.
def modified_at(path: ToPath) !! IOError -> SystemTime {
try raw.modified_at(path.to_path.to_string)
try bits.modified_at(path.to_path.to_string)
}
# Returns the access time of a path.
def accessed_at(path: ToPath) !! IOError -> SystemTime {
try raw.accessed_at(path.to_path.to_string)
try bits.accessed_at(path.to_path.to_string)
}
# Returns `True` if the path points to a file.
def file?(path: ToPath) -> Boolean {
raw.file?(path.to_path.to_string)
bits.file?(path.to_path.to_string)
}
# Returns `True` if the path points to a directory.
def directory?(path: ToPath) -> Boolean {
raw.directory?(path.to_path.to_string)
bits.directory?(path.to_path.to_string)
}
# Returns `True` if the path points to an existing file or directory.
def exists?(path: ToPath) -> Boolean {
raw.exists?(path.to_path.to_string)
bits.exists?(path.to_path.to_string)
}
......@@ -9,8 +9,8 @@
# will be given a `ReadOnlyFile` object while opening a file in write-only mode
# produces a `WriteOnlyFile` object.
import std::conversion::ToString
import std::fs::bits::(self, READ_ONLY)
import std::fs::path::(Path, ToPath)
import std::fs::raw::(self, READ_ONLY)
import std::io::(Close, Error as IOError, Read, Seek, Size, Write)
import std::process
......@@ -43,7 +43,7 @@ object ReadOnlyFile {
static def new(path: ToPath) !! IOError -> Self {
let conv_path = path.to_path
let instance =
try raw.open(type: self, path: conv_path.to_string, mode: READ_ONLY)
try bits.open(type: self, path: conv_path.to_string, mode: READ_ONLY)
instance.init(conv_path)
instance
......@@ -56,19 +56,19 @@ object ReadOnlyFile {
impl Read for ReadOnlyFile {
def read_bytes(bytes: ByteArray, size: ?Integer = Nil) !! IOError -> Integer {
try raw.read_bytes(file: self, bytes: bytes, size: size)
try bits.read_bytes(file: self, bytes: bytes, size: size)
}
}
impl Seek for ReadOnlyFile {
def seek(position: Integer) !! IOError -> Integer {
try raw.seek(file: self, position: position)
try bits.seek(file: self, position: position)
}
}
impl Size for ReadOnlyFile {
def size !! IOError -> Integer {
try raw.size(@path.to_string)
try bits.size(@path.to_string)
}
}
......@@ -99,10 +99,10 @@ object WriteOnlyFile {
#
# let handle = try! WriteOnlyFile.new('/dev/null')
static def new(path: ToPath, append = False) !! IOError -> Self {
let mode = raw.mode_for_write(append)
let mode = bits.mode_for_write(append)
let conv_path = path.to_path
let instance =
try raw.open(type: self, path: conv_path.to_string, mode: mode)
try bits.open(type: self, path: conv_path.to_string, mode: mode)
instance.init(conv_path)
instance
......@@ -115,27 +115,27 @@ object WriteOnlyFile {
impl Write for WriteOnlyFile {
def write_bytes(bytes: ByteArray) !! IOError -> Integer {
try raw.write_bytes(file: self, bytes: bytes)
try bits.write_bytes(file: self, bytes: bytes)
}
def write_string(data: ToString) !! IOError -> Integer {
try raw.write_string(file: self, data: data.to_string)
try bits.write_string(file: self, data: data.to_string)
}
def flush !! IOError -> Nil {
try raw.flush(self)
try bits.flush(self)
}
}
impl Seek for WriteOnlyFile {
def seek(position: Integer) !! IOError -> Integer {
try raw.seek(file: self, position: position)
try bits.seek(file: self, position: position)
}
}
impl Size for WriteOnlyFile {
def size !! IOError -> Integer {
try raw.size(@path.to_string)
try bits.size(@path.to_string)
}
}
......@@ -172,10 +172,10 @@ object ReadWriteFile {
#
# let handle = try! ReadWriteFile.new('/dev/null')
static def new(path: ToPath, append = False) !! IOError -> Self {
let mode = raw.mode_for_read_write(append)
let mode = bits.mode_for_read_write(append)
let conv_path = path.to_path
let instance =
try raw.open(type: self, path: conv_path.to_string, mode: mode)
try bits.open(type: self, path: conv_path.to_string, mode: mode)
instance.init(conv_path)
instance
......@@ -188,33 +188,33 @@ object ReadWriteFile {
impl Read for ReadWriteFile {
def read_bytes(bytes: ByteArray, size: ?Integer = Nil) !! IOError -> Integer {
try raw.read_bytes(file: self, bytes: bytes, size: size)
try bits.read_bytes(file: self, bytes: bytes, size: size)
}
}
impl Write for ReadWriteFile {
def write_bytes(bytes: ByteArray) !! IOError -> Integer {
try raw.write_bytes(file: self, bytes: bytes)
try bits.write_bytes(file: self, bytes: bytes)
}
def write_string(data: ToString) !! IOError -> Integer {
try raw.write_string(file: self, data: data.to_string)
try bits.write_string(file: self, data: data.to_string)
}
def flush !! IOError -> Nil {
try raw.flush(self)
try bits.flush(self)
}
}
impl Seek for ReadWriteFile {
def seek(position: Integer) !! IOError -> Integer {
try raw.seek(file: self, position: position)
try bits.seek(file: self, position: position)
}
}
impl Size for ReadWriteFile {
def size !! IOError -> Integer {
try raw.size(@path.to_string)
try bits.size(@path.to_string)
}
}
......@@ -249,7 +249,7 @@ impl Remove for ReadWriteFile {
# try! handle.write('hello')
# try! file.remove('/tmp/test.txt') # => Nil
def remove(path: ToPath) !! IOError -> Nil {
try raw.remove(path.to_path.to_string)
try bits.remove(path.to_path.to_string)
}
# Copies a file from the source destination to the target destination,
......@@ -266,7 +266,7 @@ def remove(path: ToPath) !! IOError -> Nil {
# try! handle.write('hello')
# try! file.copy(from: '/tmp/test.txt', to: '/tmp/test2.txt')
def copy(from: ToPath, to: ToPath) !! IOError -> Integer {
try raw.copy(from: from.to_path.to_string, to: to.to_path.to_string)
try bits.copy(from: from.to_path.to_string, to: to.to_path.to_string)
}
# Opens a file that can only be used for reading data.
......
......@@ -39,13 +39,13 @@
# file.read_only('/dev/urandom')
# file.read_only(Path.new('/dev/urandom'))
import std::conversion::ToString
import std::fs::raw
import std::fs::bits::(self as fs_bits)
import std::fs::path::bits::(self, SEPARATOR as _SEPARATOR)
import std::io::(Error as IOError, Size)
import std::operators::Equal
import std::os
import std::string_buffer::StringBuffer
import std::time::SystemTime
import std::fs::path::bits::(self, SEPARATOR as _SEPARATOR)
# The primary separator of path components.
let SEPARATOR = _SEPARATOR
......@@ -79,17 +79,17 @@ object Path {
# Returns `True` if the path points to a file.
def file? -> Boolean {
raw.file?(@path)
fs_bits.file?(@path)
}
# Returns `True` if the path points to a directory.
def directory? -> Boolean {
raw.directory?(@path)
fs_bits.directory?(@path)
}
# Returns `True` if the path points to an existing file or directory.
def exists? -> Boolean {
raw.exists?(@path)
fs_bits.exists?(@path)
}
# Returns the creation time of `self`.
......@@ -104,7 +104,7 @@ object Path {
#
# try! path.created_at # => SystemTime { ... }
def created_at !! IOError -> SystemTime {
try raw.created_at(@path)
try fs_bits.created_at(@path)
}
# Returns the modification time of `self`.
......@@ -119,7 +119,7 @@ object Path {
#
# try! path.modified_at # => SystemTime { ... }
def modified_at !! IOError -> SystemTime {
try raw.modified_at(@path)
try fs_bits.modified_at(@path)
}
# Returns the access time of `self`.
......@@ -134,7 +134,7 @@ object Path {
#
# try! path.accessed_at # => SystemTime { ... }
def accessed_at !! IOError -> SystemTime {
try raw.accessed_at(@path)
try fs_bits.accessed_at(@path)
}
# Returns `True` if this `Path` is an absolute path.
......@@ -279,7 +279,7 @@ impl Size for Path {
#
# try! path.size # => 0
def size !! IOError -> Integer {
try raw.size(@path)
try fs_bits.size(@path)
}
}
......
......@@ -12,13 +12,13 @@ import test::std::ffi::test_types
import test::std::fs::test_dir
import test::std::fs::test_file
import test::std::fs::test_path
import test::std::fs::test_raw
import test::std::fs::test_bits::(self as _)
import test::std::net::test_ip
import test::std::net::test_socket
import test::std::net::test_unix
import test::std::test::test_assert
import test::std::test::test_config
import test::std::test::test_error::(self as test_test_error)
import test::std::test::test_error::(self as _)
import test::std::test::test_formatters
import test::std::test::test_runner
import test::std::test::test_test
......
import std::conversion::ToString
import std::fs::path::Path
import std::fs::raw::(READ_APPEND, READ_ONLY, READ_WRITE)
import std::fs::raw::(self, MODIFIED_AT, APPEND_ONLY, WRITE_ONLY)
import std::fs::bits::(
self, APPEND_ONLY, MODIFIED_AT, READ_APPEND, READ_ONLY, READ_WRITE, WRITE_ONLY
)
import std::process
import std::test
import std::test::assert
......@@ -12,13 +13,13 @@ import test::fixtures::(self, INVALID_TIME, INVALID_DIRECTORY, INVALID_FILE)
object DummyFile {}
def remove(path: ToString) -> Nil {
try raw.remove(path.to_string) else Nil
try bits.remove(path.to_string) else Nil
}
test.group('std::fs::raw.open') do (g) {
test.group('std::fs::bits.open') do (g) {
g.test('Opening a valid file') {
assert.no_panic {
try! raw.open(
try! bits.open(
type: DummyFile,
path: VALID_FILE.to_string,
mode: READ_ONLY
......@@ -28,7 +29,7 @@ test.group('std::fs::raw.open') do (g) {
g.test('Opening an invalid file') {
assert.panic {
try! raw.open(
try! bits.open(
type: DummyFile,
path: INVALID_FILE.to_string,
mode: READ_ONLY
......@@ -37,27 +38,27 @@ test.group('std::fs::raw.open') do (g) {
}
}
test.group('std::fs::raw.mode_for_write') do (g) {
test.group('std::fs::bits.mode_for_write') do (g) {
g.test('Obtaining the mode for opening a file as write-only') {
assert.equal(raw.mode_for_write, WRITE_ONLY)
assert.equal(bits.mode_for_write, WRITE_ONLY)
}
g.test('Obtaining the mode for opening a file as append-only') {
assert.equal(raw.mode_for_write(append: True), APPEND_ONLY)
assert.equal(bits.mode_for_write(append: True), APPEND_ONLY)
}
}
test.group('std::fs::raw.mode_for_read_write') do (g) {
test.group('std::fs::bits.mode_for_read_write') do (g) {
g.test('Obtaining the mode for opening a file as read-write') {
assert.equal(raw.mode_for_read_write, READ_WRITE)
assert.equal(bits.mode_for_read_write, READ_WRITE)
}
g.test('Obtaining the mode for opening a file as read-append') {
assert.equal(raw.mode_for_read_write(append: True), READ_APPEND)
assert.equal(bits.mode_for_read_write(append: True), READ_APPEND)
}
}
test.group('std::fs::raw.copy') do (g) {
test.group('std::fs::bits.copy') do (g) {
g.test('Copying a valid file') {
let orig = VALID_FILE
let copy = fixtures.temporary_file_path
......@@ -66,109 +67,109 @@ test.group('std::fs::raw.copy') do (g) {
remove(copy)
}
try! raw.copy(from: orig.to_string, to: copy.to_string)
try! bits.copy(from: orig.to_string, to: copy.to_string)
assert.true(raw.file?(copy.to_string))
assert.true(bits.file?(copy.to_string))
}
g.test('Copying an invalid file') {
let orig = INVALID_FILE
let copy = fixtures.temporary_file_path
let size = try raw.copy(from: orig.to_string, to: copy.to_string) else -1
let size = try bits.copy(from: orig.to_string, to: copy.to_string) else -1
assert.equal(size, -1)
}
}
test.group('std::fs::raw.size') do (g) {
test.group('std::fs::bits.size') do (g) {
g.test('Obtaining the size of a valid file') {
let size = try raw.size(VALID_FILE.to_string) else -1
let size = try bits.size(VALID_FILE.to_string) else -1
assert.greater(size, 0)
}
g.test('Obtaining the size of an invalid file') {
let size = try raw.size(INVALID_FILE.to_string) else -1
let size = try bits.size(INVALID_FILE.to_string) else -1
assert.equal(size, -1)
}
}
test.group('std::fs::raw.remove') do (g) {
test.group('std::fs::bits.remove') do (g) {
g.test('Removing a valid file') {
let path = fixtures.temporary_file_path.to_string
try! raw.open(type: DummyFile, path: path, mode: WRITE_ONLY)
try! raw.remove(path)
try! bits.open(type: DummyFile, path: path, mode: WRITE_ONLY)
try! bits.remove(path)
assert.false(raw.file?(path))
assert.false(bits.file?(path))
}
g.test('Removing an invalid file') {
assert.panic {
try! raw.remove(INVALID_FILE.to_string)
try! bits.remove(INVALID_FILE.to_string)
}
}
}
test.group('std::fs::raw.file?') do (g) {
test.group('std::fs::bits.file?') do (g) {
g.test('Checking if a valid file is a file') {
assert.true(raw.file?(VALID_FILE.to_string))
assert.true(bits.file?(VALID_FILE.to_string))
}
g.test('Checking if an invalid file is a file') {
assert.false(raw.file?(INVALID_FILE.to_string))
assert.false(bits.file?(INVALID_FILE.to_string))
}
g.test('Checking if a valid directory is a file') {
assert.false(raw.file?(VALID_DIRECTORY.to_string))
assert.false(bits.file?(VALID_DIRECTORY.to_string))
}
g.test('Checking if an invalid directory is a file') {
assert.false(raw.file?(INVALID_DIRECTORY.to_string))
assert.false(bits.file?(INVALID_DIRECTORY.to_string))
}
}
test.group('std::fs::raw.directory?') do (g) {
test.group('std::fs::bits.directory?') do (g) {
g.test('Checking if a valid file is a directory') {
assert.false(raw.directory?(VALID_FILE.to_string))
assert.false(bits.directory?(VALID_FILE.to_string))
}
g.test('Checking if an invalid file is a directory') {
assert.false(raw.directory?(INVALID_FILE.to_string))
assert.false(bits.directory?(INVALID_FILE.to_string))
}
g.test('Checking if a valid directory is a directory') {
assert.true(raw.directory?(VALID_DIRECTORY.to_string))
assert.true(bits.directory?(VALID_DIRECTORY.to_string))
}
g.test('Checking if an invalid directory is a directory') {
assert.false(raw.directory?(INVALID_DIRECTORY.to_string))
assert.false(bits.directory?(INVALID_DIRECTORY.to_string))
}
}
test.group('std::fs::raw.exists?') do (g) {
test.group('std::fs::bits.exists?') do (g) {
g.test('Checking if a valid file exists') {
assert.true(raw.exists?(VALID_FILE.to_string))
assert.true(bits.exists?(VALID_FILE.to_string))
}
g.test('Checking if an invalid file exists') {
assert.false(raw.exists?(INVALID_FILE.to_string))
assert.false(bits.exists?(INVALID_FILE.to_string))
}
g.test('Checking if a valid directory exists') {
assert.true(raw.exists?(VALID_DIRECTORY.to_string))
assert.true(bits.exists?(VALID_DIRECTORY.to_string))
}
g.test('Checking if an invalid directory exists') {
assert.false(raw.exists?(INVALID_DIRECTORY.to_string))
assert.false(bits.exists?(INVALID_DIRECTORY.to_string))
}
}
test.group('std::fs::raw.file_time') do (g) {
test.group('std::fs::bits.file_time') do (g) {
g.test('Obtaining the modification time of a valid file') {
let time = try {
raw.file_time(path: VALID_FILE.to_string, kind: MODIFIED_AT)
bits.file_time(path: VALID_FILE.to_string, kind: MODIFIED_AT)
} else {
INVALID_TIME
}
......@@ -178,15 +179,15 @@ test.group('std::fs::raw.file_time') do (g) {
}
features.creation_time?.if_true {
test.group('std::fs::raw.created_at') do (g) {
test.group('std::fs::bits.created_at') do (g) {
g.test('Obtaining the creation time of a valid file') {
let time = try raw.created_at(VALID_FILE.to_string) else INVALID_TIME
let time = try bits.created_at(VALID_FILE.to_string) else INVALID_TIME
assert.not_equal(time, INVALID_TIME)
}
g.test('Obtaining the creation time of an invalid file') {
let time = try raw.created_at(INVALID_FILE.to_string) else INVALID_TIME
let time = try bits.created_at(INVALID_FILE.to_string) else INVALID_TIME
assert.equal(time, INVALID_TIME)
}
......@@ -194,15 +195,15 @@ features.creation_time?.if_true {
}
features.modification_time?.if_true {
test.group('std::fs::raw.modified_at') do (g) {
test.group('std::fs::bits.modified_at') do (g) {
g.test('Obtaining the modification time of a valid file') {
let time = try raw.modified_at(VALID_FILE.to_string) else INVALID_TIME
let time = try bits.modified_at(VALID_FILE.to_string) else INVALID_TIME
assert.not_equal(time, INVALID_TIME)
}
g.test('Obtaining the modification time of an invalid file') {
let time = try raw.modified_at(INVALID_FILE.to_string) else INVALID_TIME
let time = try bits.modified_at(INVALID_FILE.to_string) else INVALID_TIME
assert.equal(time, INVALID_TIME)
}
......@@ -210,31 +211,31 @@ features.modification_time?.if_true {
}
features.access_time?.if_true {
test.group('std::fs::raw.accessed_at') do (g) {
test.group('std::fs::bits.accessed_at') do (g) {
g.test('Obtaining the access time of a valid file') {
let time = try raw.accessed_at(VALID_FILE.to_string) else INVALID_TIME
let time = try bits.accessed_at(VALID_FILE.to_string) else INVALID_TIME
assert.not_equal(time, INVALID_TIME)
}
g.test('Obtaining the access time of an invalid file') {
let time = try raw.accessed_at(INVALID_FILE.to_string) else INVALID_TIME
let time = try bits.accessed_at(INVALID_FILE.to_string) else INVALID_TIME
assert.equal(time, INVALID_TIME)
}
}
}
test.group('std::fs::raw.read_bytes') do (g) {
test.group('std::fs::bits.read_bytes') do (g) {
g.test('Reading a valid number of bytes from a file') {
let file = try! raw.open(
let file = try! bits.open(
type: DummyFile,
path: VALID_FILE.to_string,
mode: READ_ONLY
)
let bytes = ByteArray.new
let read = try! raw.read_bytes(file: file, bytes: bytes, size: 2)
let read = try! bits.read_bytes(file: file, bytes: bytes, size: 2)
assert.equal(bytes.length, 2)
assert.equal(read, 2)
......@@ -242,19 +243,19 @@ test.group('std::fs::raw.read_bytes') do (g) {
g.test('Reading an invalid number of bytes from a file') {
assert.panic {
let file = try! raw.open(
let file = try! bits.open(
type: DummyFile,
path: VALID_FILE.to_string,
mode: READ_ONLY
)
let bytes = ByteArray.new
let read = try! raw.read_bytes(file: file, bytes: bytes, size: -1)
let read = try! bits.read_bytes(file: file, bytes: bytes, size: -1)
}
}
}
test.group('std::fs::raw.write_string') do (g) {
test.group('std::fs::bits.write_string') do (g) {
g.test('Writing a String to a file') {
let path = fixtures.temporary_file_path.to_string
......@@ -262,26 +263,26 @@ test.group('std::fs::raw.write_string') do (g) {
remove(path)
}
let file = try! raw.open(type: DummyFile, path: path, mode: WRITE_ONLY)
let written = try! raw.write_string(file: file, data: 'abc')
let file = try! bits.open(type: DummyFile, path: path, mode: WRITE_ONLY)
let written = try! bits.write_string(file: file, data: 'abc')
assert.equal(written, 3)
}
g.test('Writing a String to a file that is opened in read-only mode') {
let file = try! raw.open(
let file = try! bits.open(
type: DummyFile,
path: VALID_FILE.to_string,
mode: READ_ONLY
)
let written = try raw.write_string(file: file, data: 'abc') else -1
let written = try bits.write_string(file: file, data: 'abc') else -1
assert.equal(written, -1)
}
}
test.group('std::fs::raw.write_bytes') do (g) {
test.group('std::fs::bits.write_bytes') do (g) {
g.test('Writing a ByteArray to a file') {
let path = fixtures.temporary_file_path.to_string
......@@ -289,21 +290,21 @@ test.group('std::fs::raw.write_bytes') do (g) {
remove(path)
}
let file = try! raw.open(type: DummyFile, path: path, mode: WRITE_ONLY)
let written = try! raw.write_bytes(file: file, bytes: 'abc'.to_byte_array)
let file = try! bits.open(type: DummyFile, path: path, mode: WRITE_ONLY)
let written = try! bits.write_bytes(file: file, bytes: 'abc'.to_byte_array)
assert.equal(written, 3)
}
g.test('Writing a ByteArray to a file that is opened in read-only mode') {