Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: Fix fsys.OpenFile confusion of flags and permissions #137

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

kke
Copy link
Contributor

@kke kke commented Nov 9, 2023

OpenFile used to accept FileMode twice, as in:

func (f *PosixFsys) OpenFile(path string, mode FileMode, perm FileMode)
  (*PosixFile, error)

The correct (or stdlib equivalent) signature is to accept an integer for the status flags and fs.FileMode for the permissions, as in:

func (f *PosixFsys) OpenFile(path string, flags int, perm fs.FileMode)
  (*PosixFile, error)

The status flags are:

File access:

  • O_RDONLY - open the file read-only. The value is 0x0, you can pass a zero if you just want to read a file (this is what fsys.Open does and it should be used instead for that purpose.).
  • O_WRONLY - open the file write-only.
  • O_RDWR - open the file read-write.
  • O_SYNC is implied, all operations are synchronous.

File creation:

  • O_APPEND - append data to the file when writing.
  • O_CREATE - create a new file if none exists.
  • O_TRUNC - truncate the file if it exists
  • O_EXCL - used in conjunction with O_CREATE, file must not already exist.

Usage:

// Explanation of the flags:
// - Create a new file if one doesn't exist
// - Truncate an existing file if one exist (keeps
//   existing permissions)
// - Open the file for writing
fsys.OpenFile("/tmp/example", rigfs.O_CREATE|rigfs.O_TRUNC|rigfs.O_WRONLY,
    fs.FileMode(0644))

The rigfs.FileMode was retired, you're now expected to use fs.FileMode from "io/fs".

There were some other omissions and logical errors in the previous implementation of the function.

@kke kke added bug Something isn't working enhancement New feature or request labels Nov 9, 2023
@kke kke changed the title Fix OpenFile confusion of flags and permissions (BREAKING API) Fix OpenFile confusion of flags and permissions (BREAKING) Nov 9, 2023
@kke kke force-pushed the flags-perms branch 5 times, most recently from 07f28cd to 1114f0b Compare November 10, 2023 07:26
@kke kke marked this pull request as ready for review November 10, 2023 07:34
OpenFile used to accept `fs.FileMode` twice, as in:

```go
func (f *PosixFsys) OpenFile(path string, mode FileMode, perm FileMode)
  (*PosixFile, error)
```

The correct (or stdlib equivalent) signature is to accept an integer for
the status flags and fs.FileMode for the permissions, as in:

```go
func (f *PosixFsys) OpenFile(path string, flags int, perm fs.FileMode)
  (*PosixFile, error)
```

The OpenFile status flags are:

File access:
- O_RDONLY - open the file read-only.
- O_WRONLY - open the file write-only.
- O_RDWR - open the file read-write.

File creation:
- O_APPEND - append data to the file when writing.
- O_CREATE - create a new file if none exists.
- O_TRUNC  - truncate the file if it exists
- O_EXCL   - used in conjunction with O_CREATE, file must not already exist.

There's also O_SYNC but it is not implemented in rig, it's always sync.

See https://pkg.go.dev/os#pkg-constants

*Usage:*

    // Explanation of the flags:
    // - Create a new file if one doesn't exist
    // - Truncate an existing file if one exist (keeps
    //   existing permissions)
    // - Open the file for writing
    fsys.OpenFile("/tmp/example", os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
        fs.FileMode(0644))

The `rigfs.FileMode` is now retired, you're now expected to use fs.FileMode
from `"io/fs"`.

Signed-off-by: Kimmo Lehto <[email protected]>
@kke kke changed the title Fix OpenFile confusion of flags and permissions (BREAKING) breaking: Fix fsys.OpenFile confusion of flags and permissions Nov 10, 2023
@kke kke requested a review from twz123 November 10, 2023 08:25
@kke kke merged commit 24e770e into main Nov 13, 2023
@kke kke deleted the flags-perms branch November 13, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant