Debian Codegraph @

libnpclient/mkdir.c

raw
1 /*
2 * Copyright (C) 2006 by Latchesar Ionkov <[email protected]>
3 * Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #if HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 #include <pthread.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <libgen.h>
40
41 #include "9p.h"
42 #include "npfs.h"
43 #include "npclient.h"
44 #include "npcimpl.h"
45
46 int
47 npc_mkdir (Npcfid *fid, char *name, u32 mode)
48 {
49 Npfcall *tc = NULL, *rc = NULL;
50 int ret = -1;
51
52 if (!(tc = np_create_tmkdir(fid->fid, name, mode, getegid()))) {
53 np_uerror (ENOMEM);
54 goto done;
55 }
56 if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0)
57 goto done;
58 ret = 0;
59 done:
60 if (tc)
61 free(tc);
62 if (rc)
63 free(rc);
64 return ret;
65 }
66
67 int
68 npc_mkdir_bypath (Npcfid *root, char *path, u32 mode)
69 {
70 Npcfid *fid;
71 char *cpy, *dname, *fname;
72
73 /* walk to the parent */
74 if (!(cpy = strdup (path))) {
75 np_uerror (ENOMEM);
76 return -1;
77 }
78 dname = dirname (cpy);
79 fid = npc_walk (root, dname);
80 free (cpy);
81 if (!fid)
82 return -1;
83
84 /* create the child */
85 if (!(cpy = strdup (path))) {
86 (void)npc_clunk (fid);
87 np_uerror (ENOMEM);
88 return -1;
89 }
90 fname = basename (cpy);
91 if (npc_mkdir (fid, fname, mode) < 0) {
92 int saved_err = np_rerror ();
93 (void)npc_clunk (fid);
94 free (cpy);
95 np_uerror (saved_err);
96 return -1;
97 }
98 free (cpy);
99 if (npc_clunk (fid) < 0)
100 return -1;
101 return 0;
102 }
103