plan9port/src/lib9/create.c

71 lines
1.3 KiB
C
Raw Normal View History

#define _GNU_SOURCE /* for Linux O_DIRECT */
2003-11-23 18:12:54 +00:00
#include <u.h>
#define NOPLAN9DEFINES
2004-06-11 14:38:56 +00:00
#include <sys/file.h>
#include <unistd.h>
#include <fcntl.h>
2003-11-23 18:12:54 +00:00
#include <libc.h>
#include <sys/stat.h>
#ifndef O_DIRECT
#define O_DIRECT 0
#endif
2003-11-23 18:12:54 +00:00
int
2004-03-25 23:03:57 +00:00
p9create(char *path, int mode, ulong perm)
2003-11-23 18:12:54 +00:00
{
2004-06-11 18:57:32 +00:00
int fd, cexec, umode, rclose, lock, rdwr;
struct flock fl;
2004-06-11 18:57:32 +00:00
rdwr = mode&3;
2004-06-11 14:38:56 +00:00
lock = mode&OLOCK;
cexec = mode&OCEXEC;
rclose = mode&ORCLOSE;
2004-06-11 14:38:56 +00:00
mode &= ~(ORCLOSE|OCEXEC|OLOCK);
/* XXX should get mode mask right? */
fd = -1;
if(perm&DMDIR){
if(mode != OREAD){
werrstr("bad mode in directory create");
goto out;
}
if(mkdir(path, perm&0777) < 0)
goto out;
fd = open(path, O_RDONLY);
}else{
umode = (mode&3)|O_CREAT|O_TRUNC;
mode &= ~(3|OTRUNC);
if(mode&ODIRECT){
umode |= O_DIRECT;
mode &= ~ODIRECT;
}
if(mode&OEXCL){
umode |= O_EXCL;
mode &= ~OEXCL;
}
if(mode){
werrstr("unsupported mode in create");
goto out;
}
fd = open(path, umode, perm);
}
out:
if(fd >= 0){
2004-06-11 14:38:56 +00:00
if(lock){
fl.l_type = (rdwr==OREAD) ? F_RDLCK : F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if(fcntl(fd, F_SETLK, &fl) < 0){
2004-06-11 14:38:56 +00:00
close(fd);
return -1;
}
}
if(cexec)
fcntl(fd, F_SETFL, FD_CLOEXEC);
if(rclose)
remove(path);
}
return fd;
2003-11-23 18:12:54 +00:00
}