Blob


1 #define _GNU_SOURCE /* for Linux O_DIRECT */
2 #include <u.h>
3 #define NOPLAN9DEFINES
4 #include <libc.h>
5 #include <sys/stat.h>
7 int
8 p9create(char *path, int mode, ulong perm)
9 {
10 int fd, cexec, umode, rclose;
12 cexec = mode&OCEXEC;
13 rclose = mode&ORCLOSE;
14 mode &= ~(ORCLOSE|OCEXEC);
16 /* XXX should get mode mask right? */
17 fd = -1;
18 if(perm&DMDIR){
19 if(mode != OREAD){
20 werrstr("bad mode in directory create");
21 goto out;
22 }
23 if(mkdir(path, perm&0777) < 0)
24 goto out;
25 fd = open(path, O_RDONLY);
26 }else{
27 umode = (mode&3)|O_CREAT|O_TRUNC;
28 mode &= ~(3|OTRUNC);
29 if(mode&ODIRECT){
30 umode |= O_DIRECT;
31 mode &= ~ODIRECT;
32 }
33 if(mode&OEXCL){
34 umode |= O_EXCL;
35 mode &= ~OEXCL;
36 }
37 if(mode){
38 werrstr("unsupported mode in create");
39 goto out;
40 }
41 fd = open(path, umode, perm);
42 }
43 out:
44 if(fd >= 0){
45 if(cexec)
46 fcntl(fd, F_SETFL, FD_CLOEXEC);
47 if(rclose)
48 remove(path);
49 }
50 return fd;
51 }