Skip to content

Commit

Permalink
Don't store file paths in file types
Browse files Browse the repository at this point in the history
Storing the file path in file types isn't that useful, as the user can
use whatever path they used to create the file in the first place. In
addition, persisting the file path can lead to unexpected results if an
opened file is moved or renamed.

Changelog: changed
  • Loading branch information
yorickpeterse committed Jan 27, 2025
1 parent 09eeb48 commit 73e5f0f
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions std/src/std/fs/file.inko
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ import std.sys.unix.fs (self as sys) if unix
type pub ReadOnlyFile {
let @fd: Int32

# The path of the file.
let pub @path: Path

# Returns a new `ReadOnlyFile`.
#
# # Errors
Expand All @@ -46,7 +43,7 @@ type pub ReadOnlyFile {
#
# ReadOnlyFile.new('/dev/null'.to_path)
# ```
fn pub static new(path: Path) -> Result[ReadOnlyFile, Error] {
fn pub static new(path: ref Path) -> Result[ReadOnlyFile, Error] {
match
sys.open_file(
path.to_string,
Expand All @@ -56,7 +53,7 @@ type pub ReadOnlyFile {
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadOnlyFile(fd: fd, path: path))
case Ok(fd) -> Result.Ok(ReadOnlyFile(fd))
case Error(e) -> Result.Error(e)
}
}
Expand Down Expand Up @@ -111,9 +108,6 @@ impl Seek for ReadOnlyFile {
type pub WriteOnlyFile {
let @fd: Int32

# The path of the file.
let pub @path: Path

# Opens a file in write-only mode.
#
# # Examples
Expand All @@ -123,7 +117,7 @@ type pub WriteOnlyFile {
#
# WriteOnlyFile.new('/dev/null'.to_path)
# ```
fn pub static new(path: Path) -> Result[WriteOnlyFile, Error] {
fn pub static new(path: ref Path) -> Result[WriteOnlyFile, Error] {
match
sys.open_file(
path.to_string,
Expand All @@ -133,7 +127,7 @@ type pub WriteOnlyFile {
truncate: true,
)
{
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd: fd, path: path))
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd))
case Error(e) -> Result.Error(e)
}
}
Expand All @@ -157,7 +151,7 @@ type pub WriteOnlyFile {
truncate: false,
)
{
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd: fd, path: path))
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd))
case Error(e) -> Result.Error(e)
}
}
Expand Down Expand Up @@ -226,9 +220,6 @@ impl Seek for WriteOnlyFile {
type pub ReadWriteFile {
let @fd: Int32

# The path of the file.
let pub @path: Path

# Opens a file for both reading and writing:
#
# # Examples
Expand All @@ -238,7 +229,7 @@ type pub ReadWriteFile {
#
# ReadWriteFile.new('/dev/null'.to_path)
# ```
fn pub static new(path: Path) -> Result[ReadWriteFile, Error] {
fn pub static new(path: ref Path) -> Result[ReadWriteFile, Error] {
match
sys.open_file(
path.to_string,
Expand All @@ -248,7 +239,7 @@ type pub ReadWriteFile {
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadWriteFile(fd: fd, path: path))
case Ok(fd) -> Result.Ok(ReadWriteFile(fd))
case Error(e) -> Result.Error(e)
}
}
Expand All @@ -262,7 +253,7 @@ type pub ReadWriteFile {
#
# ReadWriteFile.append('/dev/null'.to_path)
# ```
fn pub static append(path: Path) -> Result[ReadWriteFile, Error] {
fn pub static append(path: ref Path) -> Result[ReadWriteFile, Error] {
match
sys.open_file(
path.to_string,
Expand All @@ -272,7 +263,7 @@ type pub ReadWriteFile {
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadWriteFile(fd: fd, path: path))
case Ok(fd) -> Result.Ok(ReadWriteFile(fd))
case Error(e) -> Result.Error(e)
}
}
Expand Down

0 comments on commit 73e5f0f

Please sign in to comment.