mode append not working in os.Open

757 views
Skip to first unread message

kar

unread,
Jul 21, 2010, 12:43:19 AM7/21/10
to golang-nuts
code below works,

f, err := os.Open(o.path, os.O_WRONLY|os.O_CREAT, 0666)
if err != nil { return false }
f.Write(buf)
f.Close()

but this adds nothing,

f, err := os.Open(o.path, os.O_APPEND|os.O_CREAT, 0666)
if err != nil { return false }
f.Write(buf)
f.Close()

is there a bug in this pkg that is should know or report?

Andrew Gerrand

unread,
Jul 21, 2010, 1:15:33 AM7/21/10
to kar, golang-nuts

Using os.O_WRONLY|os.O_APPEND|os.O_CREATE should work.

Not sure why this is the case. If it's the intended behaviour, the
comment should be amended:

O_RDONLY = syscall.O_RDONLY // open the file read-only.
O_WRONLY = syscall.O_WRONLY // open the file write-only.
O_RDWR = syscall.O_RDWR // open the file read-write.
O_APPEND = syscall.O_APPEND // open the file append-only.

perhaps, instead:

O_APPEND = syscall.O_APPEND // append data to the file when writing.

Andrew

kar

unread,
Jul 21, 2010, 1:16:46 AM7/21/10
to golang-nuts
thanks

On Jul 21, 1:15 pm, Andrew Gerrand <a...@golang.org> wrote:

peterGo

unread,
Jul 21, 2010, 5:53:00 AM7/21/10
to golang-nuts
Andrew,

On Jul 21, 1:15 am, Andrew Gerrand <a...@golang.org> wrote:

> Using os.O_WRONLY|os.O_APPEND|os.O_CREATE should work.

> Not sure why this is the case. If it's the intended behaviour, the
> comment should be amended:
>
> O_RDONLY = syscall.O_RDONLY // open the file read-only.
> O_WRONLY = syscall.O_WRONLY // open the file write-only.
> O_RDWR = syscall.O_RDWR // open the file read-write.
> O_APPEND = syscall.O_APPEND // open the file append-only.
>
> perhaps, instead:
>
> O_APPEND = syscall.O_APPEND // append data to the file when writing.

It's working as expected and the documentation is correct.

The argument flags must include one of the following access modes:
O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-
only, write-only, or read/write, respectively.
http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html

O_APPEND The file is opened in append mode.
http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html

Peter

On Jul 21, 1:15 am, Andrew Gerrand <a...@golang.org> wrote:

peterGo

unread,
Jul 21, 2010, 5:54:49 AM7/21/10
to golang-nuts
kar,

You need to specify exactly one of the file access modes: O_RDONLY,
O_WRONLY, O_RDWR.

Peter

Andrew Gerrand

unread,
Jul 21, 2010, 5:59:46 AM7/21/10
to peterGo, golang-nuts
On 21 July 2010 19:53, peterGo <go.pe...@gmail.com> wrote:
> Andrew,
>
> On Jul 21, 1:15 am, Andrew Gerrand <a...@golang.org> wrote:
>
>> Using os.O_WRONLY|os.O_APPEND|os.O_CREATE should work.
>
>> Not sure why this is the case. If it's the intended behaviour, the
>> comment should be amended:
>>
>>     O_RDONLY   = syscall.O_RDONLY   // open the file read-only.
>>     O_WRONLY   = syscall.O_WRONLY   // open the file write-only.
>>     O_RDWR     = syscall.O_RDWR     // open the file read-write.
>>     O_APPEND   = syscall.O_APPEND   // open the file append-only.
>>
>> perhaps, instead:
>>
>>     O_APPEND   = syscall.O_APPEND   // append data to the file when writing.
>
> It's working as expected and the documentation is correct.
>
> The argument flags must include one of the following access modes:
> O_RDONLY, O_WRONLY, or O_RDWR.  These request opening the file read-
> only, write-only, or read/write, respectively.
> http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html
>
> O_APPEND The file is opened in append mode.
> http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html

The comment is strictly correct, but it is misleading. It implies that
O_APPEND is part of a group with O_RDONLY, O_WRONLY, and O_RDWR. The
open(2) man page clearly delineates between "access modes" (O_RDONLY,
O_WRONLY, O_RDWR) and "file creation/status flags" (including
O_APPEND). You must always supply one of the former.

I committed a change earlier that alters the comment to disambiguate
the O_APPEND flag from the access modes.

Andrew

Reply all
Reply to author
Forward
0 new messages