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

File.isBinaryMode and more #9597

Open
dlangBugzillaToGithub opened this issue Feb 5, 2013 · 1 comment
Open

File.isBinaryMode and more #9597

dlangBugzillaToGithub opened this issue Feb 5, 2013 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-02-05T17:04:53Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=9455

Description

Some functions I write take a File reference and write some binary data on it. So in their pre-condition I'd like to assert the File to be not just open, but also opened in binary mode. I think currently there is no handy way to know if a std.stdio.File is opened in binary or text mode (or in write or read mode).

After a short discussion on IRC #D with jA_cOp, I think a way to implement this feature is:
- Add isBinaryMode/isWriteable/isReadable properties to File, and implement them as light wrappers around platform-specific functions that get that information.
- in most cases a std.stdio.File is created with a file name and a mode string, while the usage of std.stdio.File.wrapFile() is uncommon. So on systems where those platform-specific functions are not available a workaround is to keep this mode string given to the File.__ctor.
@dlangBugzillaToGithub
Copy link
Author

kevin.lamonte commented on 2013-07-09T11:38:23Z

(In reply to comment #0)
> - Add isBinaryMode/isWriteable/isReadable properties to File, and implement
> them as light wrappers around platform-specific functions that get that
> information.

I vote that there be five new functions:

* isReadable(inout(C)[] path) (or perhaps canRead()): for files, a std.stdio.open(..., "r") will succeed; for directories, std.file.dirEntries() will succeed.

* isWriteable(inout(C)[] path) (or perhaps canWrite()):  for files, a std.stdio.open(..., "w") will succeed; for directories, creating a new file in the directory will succeed (excluding things like no space on device).  Open question if this function should detect write-only media and return false if so.

* isExecutable(inout(C)[] path) (or perhaps canExecute()): for files, exec()/fork()/popen()/etc. (and wrappers) will succeed; always false for directories.

* isTraversable(inout(C)[] path): always false for files; for directories, a chdir() will succeed OR a file in the directory can be stat()d.  (This is the traditional POSIX view of execute bit on a directory.)

* std.stdio.File.isBinary(): returns true if the file was opened with "rb" or "wb".

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants