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>
6 #ifndef O_DIRECT
7 #define O_DIRECT 0
8 #endif
10 int
11 p9create(char *path, int mode, ulong perm)
12 {
13 int fd, cexec, umode, rclose;
15 cexec = mode&OCEXEC;
16 rclose = mode&ORCLOSE;
17 mode &= ~(ORCLOSE|OCEXEC);
19 /* XXX should get mode mask right? */
20 fd = -1;
21 if(perm&DMDIR){
22 if(mode != OREAD){
23 werrstr("bad mode in directory create");
24 goto out;
25 }
26 if(mkdir(path, perm&0777) < 0)
27 goto out;
28 fd = open(path, O_RDONLY);
29 }else{
30 umode = (mode&3)|O_CREAT|O_TRUNC;
31 mode &= ~(3|OTRUNC);
32 if(mode&ODIRECT){
33 umode |= O_DIRECT;
34 mode &= ~ODIRECT;
35 }
36 if(mode&OEXCL){
37 umode |= O_EXCL;
38 mode &= ~OEXCL;
39 }
40 if(mode){
41 werrstr("unsupported mode in create");
42 goto out;
43 }
44 fd = open(path, umode, perm);
45 }
46 out:
47 if(fd >= 0){
48 if(cexec)
49 fcntl(fd, F_SETFL, FD_CLOEXEC);
50 if(rclose)
51 remove(path);
52 }
53 return fd;
54 }