File open/create attibutes.

Richard B. Johnson (root@chaos.analogic.com)
Tue, 14 May 2002 11:44:44 -0400 (EDT)


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.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/