Blame


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