| 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 <stdarg.h> |
| 33 |
#include <string.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 |
/* fid: parent directory on entry, new file on exit. |
| 47 |
*/ |
| 48 |
int |
| 49 |
npc_create (Npcfid *fid, char *name, u32 flags, u32 mode, gid_t gid) |
| 50 |
{ |
| 51 |
int maxio = fid->fsys->msize - P9_IOHDRSZ; |
| 52 |
Npfcall *tc = NULL, *rc = NULL; |
| 53 |
int ret = -1; |
| 54 |
|
| 55 |
if (!(tc = np_create_tlcreate(fid->fid, name, flags, mode, gid))) { |
| 56 |
np_uerror (ENOMEM); |
| 57 |
goto done; |
| 58 |
} |
| 59 |
if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0) |
| 60 |
goto done; |
| 61 |
fid->iounit = rc->u.rlcreate.iounit; |
| 62 |
if (!fid->iounit == 0 || fid->iounit > maxio) |
| 63 |
fid->iounit = maxio; |
| 64 |
ret = 0; |
| 65 |
done: |
| 66 |
if (tc) |
| 67 |
free(tc); |
| 68 |
if (rc) |
| 69 |
free(rc); |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
Npcfid * |
| 74 |
npc_create_bypath (Npcfid *root, char *path, u32 flags, u32 mode, gid_t gid) |
| 75 |
{ |
| 76 |
Npcfid *fid; |
| 77 |
char *cpy, *dname, *fname; |
| 78 |
|
| 79 |
/* walk to the parent */ |
| 80 |
if (!(cpy = strdup (path))) { |
| 81 |
np_uerror (ENOMEM); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
dname = dirname (cpy); |
| 85 |
fid = npc_walk (root, dname); |
| 86 |
free (cpy); |
| 87 |
if (!fid) |
| 88 |
return NULL; |
| 89 |
|
| 90 |
/* create the child */ |
| 91 |
if (!(cpy = strdup (path))) { |
| 92 |
(void)npc_clunk (fid); |
| 93 |
np_uerror (ENOMEM); |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
fname = basename (cpy); |
| 97 |
if (npc_create (fid, fname, flags, mode, gid) < 0) { |
| 98 |
int saved_err = np_rerror (); |
| 99 |
(void)npc_clunk (fid); |
| 100 |
free (cpy); |
| 101 |
np_uerror (saved_err); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
free (cpy); |
| 105 |
return fid; |
| 106 |
} |
| 107 |
|
| 108 |
int |
| 109 |
npc_open (Npcfid *fid, u32 flags) |
| 110 |
{ |
| 111 |
int maxio = fid->fsys->msize - P9_IOHDRSZ; |
| 112 |
Npfcall *tc = NULL, *rc = NULL; |
| 113 |
int ret = -1; |
| 114 |
|
| 115 |
if (!(tc = np_create_tlopen(fid->fid, flags))) { |
| 116 |
np_uerror (ENOMEM); |
| 117 |
goto done; |
| 118 |
} |
| 119 |
if (fid->fsys->rpc(fid->fsys, tc, &rc) < 0) |
| 120 |
goto done; |
| 121 |
fid->iounit = rc->u.rlopen.iounit; |
| 122 |
if (fid->iounit == 0 || fid->iounit > maxio) |
| 123 |
fid->iounit = maxio; |
| 124 |
fid->offset = 0; |
| 125 |
ret = 0; |
| 126 |
done: |
| 127 |
if (tc) |
| 128 |
free(tc); |
| 129 |
if (rc) |
| 130 |
free(rc); |
| 131 |
return ret; |
| 132 |
} |
| 133 |
|
| 134 |
Npcfid * |
| 135 |
npc_open_bypath (Npcfid *root, char *path, u32 flags) |
| 136 |
{ |
| 137 |
Npcfid *fid; |
| 138 |
|
| 139 |
if (!(fid = npc_walk (root, path))) |
| 140 |
return NULL; |
| 141 |
if (npc_open (fid, flags) < 0) { |
| 142 |
int saved_err = np_rerror (); |
| 143 |
(void)npc_clunk (fid); |
| 144 |
np_uerror (saved_err); |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
return fid; |
| 148 |
} |
| 149 |
|
| 150 |
u64 |
| 151 |
npc_lseek(Npcfid *fid, u64 offset, int whence) |
| 152 |
{ |
| 153 |
u64 file_size = 0; |
| 154 |
|
| 155 |
switch (whence) { |
| 156 |
case SEEK_SET: |
| 157 |
fid->offset = offset; |
| 158 |
break; |
| 159 |
case SEEK_CUR: |
| 160 |
fid->offset += offset; |
| 161 |
break; |
| 162 |
case SEEK_END: |
| 163 |
/* FIXME: get file_size */ |
| 164 |
fid->offset = file_size + offset; |
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
return fid->offset; |
| 169 |
} |