1 be36ff68 2004-04-29 devnull #define _GNU_SOURCE /* for Linux O_DIRECT */
2 fd04aace 2003-11-23 devnull #include <u.h>
3 32f69c36 2003-12-11 devnull #define NOPLAN9DEFINES
4 e7504005 2004-06-11 devnull #include <sys/file.h>
5 fd04aace 2003-11-23 devnull #include <libc.h>
6 32f69c36 2003-12-11 devnull #include <sys/stat.h>
7 983990c8 2004-05-14 devnull #ifndef O_DIRECT
8 983990c8 2004-05-14 devnull #define O_DIRECT 0
12 8ad51794 2004-03-25 devnull p9create(char *path, int mode, ulong perm)
14 d9e16d25 2004-06-11 devnull int fd, cexec, umode, rclose, lock, rdwr;
16 d9e16d25 2004-06-11 devnull rdwr = mode&3;
17 e7504005 2004-06-11 devnull lock = mode&OLOCK;
18 32f69c36 2003-12-11 devnull cexec = mode&OCEXEC;
19 32f69c36 2003-12-11 devnull rclose = mode&ORCLOSE;
20 e7504005 2004-06-11 devnull mode &= ~(ORCLOSE|OCEXEC|OLOCK);
22 32f69c36 2003-12-11 devnull /* XXX should get mode mask right? */
24 32f69c36 2003-12-11 devnull if(perm&DMDIR){
25 32f69c36 2003-12-11 devnull if(mode != OREAD){
26 32f69c36 2003-12-11 devnull werrstr("bad mode in directory create");
27 32f69c36 2003-12-11 devnull goto out;
29 32f69c36 2003-12-11 devnull if(mkdir(path, perm&0777) < 0)
30 32f69c36 2003-12-11 devnull goto out;
31 32f69c36 2003-12-11 devnull fd = open(path, O_RDONLY);
33 32f69c36 2003-12-11 devnull umode = (mode&3)|O_CREAT|O_TRUNC;
34 32f69c36 2003-12-11 devnull mode &= ~(3|OTRUNC);
35 be36ff68 2004-04-29 devnull if(mode&ODIRECT){
36 be36ff68 2004-04-29 devnull umode |= O_DIRECT;
37 be36ff68 2004-04-29 devnull mode &= ~ODIRECT;
39 32f69c36 2003-12-11 devnull if(mode&OEXCL){
40 32f69c36 2003-12-11 devnull umode |= O_EXCL;
41 32f69c36 2003-12-11 devnull mode &= ~OEXCL;
43 32f69c36 2003-12-11 devnull if(mode){
44 32f69c36 2003-12-11 devnull werrstr("unsupported mode in create");
45 32f69c36 2003-12-11 devnull goto out;
47 32f69c36 2003-12-11 devnull fd = open(path, umode, perm);
50 32f69c36 2003-12-11 devnull if(fd >= 0){
51 e7504005 2004-06-11 devnull if(lock){
52 d9e16d25 2004-06-11 devnull if(flock(fd, (rdwr==OREAD) ? LOCK_SH : LOCK_EX) < 0){
53 e7504005 2004-06-11 devnull close(fd);
54 e7504005 2004-06-11 devnull return -1;
57 32f69c36 2003-12-11 devnull if(cexec)
58 32f69c36 2003-12-11 devnull fcntl(fd, F_SETFL, FD_CLOEXEC);
59 32f69c36 2003-12-11 devnull if(rclose)
60 32f69c36 2003-12-11 devnull remove(path);
62 32f69c36 2003-12-11 devnull return fd;