All of lore.kernel.org
 help / color / mirror / Atom feed
* File open/create attibutes.
@ 2002-05-14 15:44 Richard B. Johnson
  2002-05-14 16:20 ` Andreas Schwab
  2002-05-14 16:43 ` Alan Cox
  0 siblings, 2 replies; 3+ messages in thread
From: Richard B. Johnson @ 2002-05-14 15:44 UTC (permalink / raw)
  To: Linux kernel

Hello,

If a file exists with attributes, 0644, and it is opened with truncate
and create with different attributes, it doesn't get those attributes.
It's only if the file doesn't exist at all that it gets created with
the new attributes.

I think this is a bug. According to my reading, O_CREAT opens the file
with the specified attributes. If the file exists, its data remains
unless O_TRUNC is set.

The following program clearly shows the problem:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(void);
char fname[]="/tmp/foo";
int main()
{
    int fd;
    (void)unlink(fname);
    if((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
    {
        fprintf(stderr, "Can't create file %s\n", fname);
        exit(EXIT_FAILURE);
    }
    (void)close(fd);
    (void)system("ls -la /tmp/foo");
    if((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0744)) == -1)
    {
        fprintf(stderr, "Can't create file %s\n", fname);
        exit(EXIT_FAILURE);
    }
    (void)close(fd);
    (void)system("ls -la /tmp/foo");
    (void)unlink(fname);
    if((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0744)) == -1)
    {
        fprintf(stderr, "Can't create file %s\n", fname);
        exit(EXIT_FAILURE);
    }
    (void)system("ls -la /tmp/foo");
    (void)unlink(fname);
    return 0;
}


Cheers,
Dick Johnson

Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).

                 Windows-2000/Professional isn't.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: File open/create attibutes.
  2002-05-14 15:44 File open/create attibutes Richard B. Johnson
@ 2002-05-14 16:20 ` Andreas Schwab
  2002-05-14 16:43 ` Alan Cox
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Schwab @ 2002-05-14 16:20 UTC (permalink / raw)
  To: root; +Cc: Linux kernel

"Richard B. Johnson" <root@chaos.analogic.com> writes:

|> Hello,
|> 
|> If a file exists with attributes, 0644, and it is opened with truncate
|> and create with different attributes, it doesn't get those attributes.
|> It's only if the file doesn't exist at all that it gets created with
|> the new attributes.
|> 
|> I think this is a bug.

POSIX disagrees:

O_CREAT         If the file exists, this flag has no effect except as
                noted under O_EXCL below.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: File open/create attibutes.
  2002-05-14 15:44 File open/create attibutes Richard B. Johnson
  2002-05-14 16:20 ` Andreas Schwab
@ 2002-05-14 16:43 ` Alan Cox
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Cox @ 2002-05-14 16:43 UTC (permalink / raw)
  To: root; +Cc: Linux kernel

> If a file exists with attributes, 0644, and it is opened with truncate
> and create with different attributes, it doesn't get those attributes.
> It's only if the file doesn't exist at all that it gets created with
> the new attributes.

This is correct behaviour. See IEEE Std 1003.1-2001. Its explicitly 
specified that "If the file exists and is a regular file, and the file
is successfully opened O_RDWR or O_WRONLY, its length shall be truncated
to 0, and the mode and owner shall be unchanged"

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-05-14 16:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-05-14 15:44 File open/create attibutes Richard B. Johnson
2002-05-14 16:20 ` Andreas Schwab
2002-05-14 16:43 ` Alan Cox

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.