2004-04-29 17:13:24 +00:00
|
|
|
#define _GNU_SOURCE /* for Linux O_DIRECT */
|
2003-11-23 18:12:54 +00:00
|
|
|
#include <u.h>
|
2003-12-11 17:48:38 +00:00
|
|
|
#define NOPLAN9DEFINES
|
2003-11-23 18:12:54 +00:00
|
|
|
#include <libc.h>
|
2003-12-11 17:48:38 +00:00
|
|
|
#include <sys/stat.h>
|
2004-05-14 20:19:53 +00:00
|
|
|
#ifndef O_DIRECT
|
|
|
|
#define O_DIRECT 0
|
|
|
|
#endif
|
2003-12-11 17:48:38 +00:00
|
|
|
|
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
|
|
|
{
|
2003-12-11 17:48:38 +00:00
|
|
|
int fd, cexec, umode, rclose;
|
|
|
|
|
|
|
|
cexec = mode&OCEXEC;
|
|
|
|
rclose = mode&ORCLOSE;
|
|
|
|
mode &= ~(ORCLOSE|OCEXEC);
|
|
|
|
|
|
|
|
/* 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);
|
2004-04-29 17:13:24 +00:00
|
|
|
if(mode&ODIRECT){
|
|
|
|
umode |= O_DIRECT;
|
|
|
|
mode &= ~ODIRECT;
|
|
|
|
}
|
2003-12-11 17:48:38 +00:00
|
|
|
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){
|
|
|
|
if(cexec)
|
|
|
|
fcntl(fd, F_SETFL, FD_CLOEXEC);
|
|
|
|
if(rclose)
|
|
|
|
remove(path);
|
|
|
|
}
|
|
|
|
return fd;
|
2003-11-23 18:12:54 +00:00
|
|
|
}
|