[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: file:write() fails silently for read-only files
- From: HyperHacker <hyperhacker@...>
- Date: Tue, 6 Sep 2011 01:16:14 -0600
On Tue, Sep 6, 2011 at 01:14, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great HyperHacker once stated:
>> On Tue, Sep 6, 2011 at 00:51, Josh Simmons <simmons.44@gmail.com> wrote:
>> > On Tue, Sep 6, 2011 at 4:39 PM, HyperHacker <hyperhacker@gmail.com> wrote:
>> >> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
>> >>> f=io.open('foo.foo', 'r')
>> >>> f:write('ffd')
>> >>> f:close()
>> >>>
>> >>
>> >> Call me crazy, but I feel like f:write() should throw an error when
>> >> trying to write to a read-only file, instead of just doing nothing.
>> >>
>> >> --
>> >> Sent from my toaster.
>> >>
>> >>
>> >
>> > Check the return value of write.
>> >
>> >
>>
>> I get "bad file descriptor" which seems rather misleading. No return
>> value is documented in the manual though.
>
> Re-read section 5.7:
>
> Unless otherwise stated, all I/O functions return nil on failure
> (plus an error message as a second result and a system-dependent
> error code as a third result) and some value different from nil on
> success.
>
> Also, under Unix, the write() system call (which is the system call that
> f:write() eventually calls) can return EBADF (which translates to "bad file
> descriptor") as described in the man page:
>
> ERRORS
>
> EBADF fd is not a valid file descriptor or is not open for
> writing.
>
> ...
>
> -spc
>
>
>
Ahh, serves me right for only looking at the specific function and not
the section header.
Just because Unix does it that way though doesn't necessarily mean
it's the best way. I think throwing an error makes more sense than
returning a system-dependent, odd-sounding error code/message.
--
Sent from my toaster.