2012-03-29 01:27:24 +02:00
|
|
|
/****************************************************************************
|
2012-06-12 19:36:48 +02:00
|
|
|
* fs/nfs/nfs_util.c
|
2012-03-29 01:27:24 +02:00
|
|
|
*
|
2020-05-25 20:45:57 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-03-29 01:27:24 +02:00
|
|
|
*
|
2020-05-25 20:45:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-03-29 01:27:24 +02:00
|
|
|
*
|
2020-05-25 20:45:57 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-03-29 01:27:24 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
2012-04-17 01:01:21 +02:00
|
|
|
#include <sys/time.h>
|
2012-03-29 01:27:24 +02:00
|
|
|
|
2020-11-25 11:53:58 +01:00
|
|
|
#include <inttypes.h>
|
2012-03-29 01:27:24 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2017-02-13 17:07:43 +01:00
|
|
|
#include <time.h>
|
2012-03-29 01:27:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-06-09 21:29:49 +02:00
|
|
|
#include "rpc.h"
|
|
|
|
#include "nfs.h"
|
2012-04-17 01:01:21 +02:00
|
|
|
#include "nfs_proto.h"
|
|
|
|
#include "nfs_mount.h"
|
|
|
|
#include "nfs_node.h"
|
2012-06-09 21:29:49 +02:00
|
|
|
#include "xdr_subs.h"
|
2012-03-29 01:27:24 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-06-09 21:29:49 +02:00
|
|
|
static inline int nfs_pathsegment(FAR const char **path, FAR char *buffer,
|
|
|
|
FAR char *terminator)
|
|
|
|
{
|
|
|
|
FAR const char *src = *path;
|
|
|
|
FAR char *dest = buffer;
|
|
|
|
int nbytes = 0;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
/* Loop until the name is successfully parsed or an error occurs */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
|
|
|
/* Get the next byte from the path */
|
|
|
|
|
|
|
|
ch = *src++;
|
|
|
|
|
|
|
|
/* Check if this the last byte in this segment name */
|
|
|
|
|
|
|
|
if (ch == '\0' || ch == '/')
|
|
|
|
{
|
2019-03-01 22:00:00 +01:00
|
|
|
/* This logic just supports "//" sequences in the path name */
|
2012-06-09 21:29:49 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if (ch == '\0' || nbytes > 0)
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
|
|
|
/* NULL terminate the parsed path segment */
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
|
|
|
|
/* Return next path and the terminating character */
|
|
|
|
|
|
|
|
*terminator = ch;
|
|
|
|
*path = src;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just skip over any leading '/' characters */
|
|
|
|
}
|
|
|
|
else if (nbytes >= NAME_MAX)
|
|
|
|
{
|
2020-11-25 11:53:58 +01:00
|
|
|
ferr("ERROR: File name segment is too long: %d\n", nbytes);
|
2020-03-01 21:53:45 +01:00
|
|
|
return -EFBIG;
|
2012-06-09 21:29:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save next character in the accumulated name */
|
|
|
|
|
|
|
|
*dest++ = ch;
|
2012-06-11 20:55:58 +02:00
|
|
|
nbytes++;
|
2012-06-09 21:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 02:16:04 +02:00
|
|
|
|
2012-03-29 01:27:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-06-28 00:03:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nfs_request
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2020-05-25 20:45:57 +02:00
|
|
|
* Perform the NFS request. On successful receipt, it verifies the NFS
|
|
|
|
* level of the returned values.
|
2012-06-28 00:03:32 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-01 21:53:45 +01:00
|
|
|
* Zero on success; a negative errno value on failure.
|
2012-06-28 00:03:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
int nfs_request(FAR struct nfsmount *nmp, int procnum,
|
2012-06-28 00:03:32 +02:00
|
|
|
FAR void *request, size_t reqlen,
|
|
|
|
FAR void *response, size_t resplen)
|
|
|
|
{
|
2020-03-01 21:53:45 +01:00
|
|
|
FAR struct rpcclnt *clnt = nmp->nm_rpcclnt;
|
2012-06-28 00:03:32 +02:00
|
|
|
struct nfs_reply_header replyh;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = rpcclnt_request(clnt, procnum, NFS_PROG, NFS_VER3,
|
|
|
|
request, reqlen, response, resplen);
|
|
|
|
if (error != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: rpcclnt_request failed: %d\n", error);
|
2021-06-28 06:56:21 +02:00
|
|
|
|
|
|
|
if (error != -ENOTCONN)
|
|
|
|
{
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reconnect */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("Reconnect due to timeout\n");
|
2021-06-28 06:56:21 +02:00
|
|
|
|
|
|
|
error = rpcclnt_connect(nmp->nm_rpcclnt);
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
{
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the request again */
|
|
|
|
|
|
|
|
error = rpcclnt_request(clnt, procnum, NFS_PROG, NFS_VER3,
|
|
|
|
request, reqlen, response, resplen);
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
{
|
|
|
|
return error;
|
|
|
|
}
|
2012-06-28 00:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&replyh, response, sizeof(struct nfs_reply_header));
|
|
|
|
|
|
|
|
if (replyh.nfs_status != 0)
|
|
|
|
{
|
2020-03-01 10:24:29 +01:00
|
|
|
/* NFS_ERRORS are the same as NuttX errno values */
|
2012-06-28 00:03:32 +02:00
|
|
|
|
2020-03-01 10:24:29 +01:00
|
|
|
return -fxdr_unsigned(uint32_t, replyh.nfs_status);
|
2012-06-28 00:03:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
if (replyh.rh.rpc_verfi.authtype != 0)
|
2012-06-28 00:03:32 +02:00
|
|
|
{
|
2020-03-01 21:53:45 +01:00
|
|
|
error = -EOPNOTSUPP;
|
|
|
|
ferr("ERROR: NFS authtype %d from server\n",
|
2020-05-25 20:45:57 +02:00
|
|
|
fxdr_unsigned(int, replyh.rh.rpc_verfi.authtype));
|
2012-06-28 00:03:32 +02:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("NFS_SUCCESS\n");
|
2012-06-28 00:03:32 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-06-10 02:13:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nfs_lookup
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2012-06-10 02:13:59 +02:00
|
|
|
* Given a directory file handle, and the path to file in the directory,
|
|
|
|
* return the file handle of the path and attributes of both the file and
|
|
|
|
* the directory containing the file.
|
|
|
|
*
|
|
|
|
* NOTE: The LOOKUP call differs from other RPC messages in that the
|
|
|
|
* call message is variable length, depending upon the size of the path
|
|
|
|
* name.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
int nfs_lookup(FAR struct nfsmount *nmp, FAR const char *filename,
|
2012-06-10 02:13:59 +02:00
|
|
|
FAR struct file_handle *fhandle,
|
|
|
|
FAR struct nfs_fattr *obj_attributes,
|
|
|
|
FAR struct nfs_fattr *dir_attributes)
|
|
|
|
{
|
2012-06-10 20:16:01 +02:00
|
|
|
FAR uint32_t *ptr;
|
2012-06-10 03:16:46 +02:00
|
|
|
uint32_t value;
|
2012-06-10 20:16:01 +02:00
|
|
|
int reqlen;
|
2012-06-10 02:13:59 +02:00
|
|
|
int namelen;
|
|
|
|
int error = 0;
|
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
DEBUGASSERT(nmp && filename && fhandle);
|
2012-06-10 02:13:59 +02:00
|
|
|
|
|
|
|
/* Get the length of the string to be sent */
|
|
|
|
|
|
|
|
namelen = strlen(filename);
|
|
|
|
if (namelen > NAME_MAX)
|
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: Length of the string is too long: %d\n", namelen);
|
2020-03-01 21:53:45 +01:00
|
|
|
return -E2BIG;
|
2012-06-10 02:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the request */
|
|
|
|
|
2012-06-14 17:45:38 +02:00
|
|
|
ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.lookup.lookup;
|
2012-06-10 20:16:01 +02:00
|
|
|
reqlen = 0;
|
|
|
|
|
|
|
|
/* Copy the variable length, directory file handle */
|
|
|
|
|
|
|
|
*ptr++ = txdr_unsigned(fhandle->length);
|
|
|
|
reqlen += sizeof(uint32_t);
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
memcpy(ptr, &fhandle->handle, fhandle->length);
|
2020-03-01 21:53:45 +01:00
|
|
|
reqlen += uint32_alignup(fhandle->length);
|
2012-06-10 20:16:01 +02:00
|
|
|
ptr += uint32_increment(fhandle->length);
|
2012-06-10 02:13:59 +02:00
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
/* Copy the variable-length file name */
|
|
|
|
|
|
|
|
*ptr++ = txdr_unsigned(namelen);
|
|
|
|
reqlen += sizeof(uint32_t);
|
|
|
|
|
|
|
|
memcpy(ptr, filename, namelen);
|
|
|
|
reqlen += uint32_alignup(namelen);
|
2012-06-10 02:13:59 +02:00
|
|
|
|
|
|
|
/* Request LOOKUP from the server */
|
|
|
|
|
2012-06-12 19:36:48 +02:00
|
|
|
nfs_statistics(NFSPROC_LOOKUP);
|
2012-06-10 02:13:59 +02:00
|
|
|
error = nfs_request(nmp, NFSPROC_LOOKUP,
|
2012-06-14 17:45:38 +02:00
|
|
|
(FAR void *)&nmp->nm_msgbuffer.lookup, reqlen,
|
|
|
|
(FAR void *)nmp->nm_iobuffer, nmp->nm_buflen);
|
|
|
|
|
2012-06-10 02:13:59 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nfs_request failed: %d\n", error);
|
2012-06-10 02:13:59 +02:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-06-10 03:16:46 +02:00
|
|
|
/* Return the data to the caller's buffers. NOTE: Here we ignore the
|
|
|
|
* the exact layout of the rpc_reply_lookup structure. File handles
|
|
|
|
* may differ in size whereas struct rpc_reply_lookup uses a fixed size.
|
|
|
|
*/
|
|
|
|
|
2020-05-25 20:45:57 +02:00
|
|
|
ptr = (FAR uint32_t *)
|
|
|
|
&((FAR struct rpc_reply_lookup *)nmp->nm_iobuffer)->lookup;
|
2012-06-10 03:16:46 +02:00
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
/* Get the length of the file handle */
|
2012-06-10 02:13:59 +02:00
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
value = *ptr++;
|
|
|
|
value = fxdr_unsigned(uint32_t, value);
|
2012-06-11 23:16:35 +02:00
|
|
|
if (value > NFSX_V3FHMAX)
|
2012-06-10 20:16:01 +02:00
|
|
|
{
|
2020-11-25 11:53:58 +01:00
|
|
|
ferr("ERROR: Bad file handle length: %" PRId32 "\n", value);
|
2020-03-01 21:53:45 +01:00
|
|
|
return -EIO;
|
2012-06-10 20:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the file handle */
|
|
|
|
|
|
|
|
fhandle->length = value;
|
|
|
|
memcpy(&fhandle->handle, ptr, value);
|
2012-06-10 03:16:46 +02:00
|
|
|
ptr += uint32_increment(value);
|
2012-06-10 02:13:59 +02:00
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
/* Check if there are object attributes and, if so, copy them to the user
|
|
|
|
* buffer
|
|
|
|
*/
|
2012-06-10 03:16:46 +02:00
|
|
|
|
|
|
|
value = *ptr++;
|
|
|
|
if (value)
|
2012-06-10 02:13:59 +02:00
|
|
|
{
|
2012-06-10 20:16:01 +02:00
|
|
|
if (obj_attributes)
|
|
|
|
{
|
|
|
|
memcpy(obj_attributes, ptr, sizeof(struct nfs_fattr));
|
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2012-06-10 03:16:46 +02:00
|
|
|
ptr += uint32_increment(sizeof(struct nfs_fattr));
|
2012-06-10 02:13:59 +02:00
|
|
|
}
|
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
/* Check if there are directory attributes and, if so, copy them to the
|
|
|
|
* user buffer
|
|
|
|
*/
|
2012-06-10 03:16:46 +02:00
|
|
|
|
|
|
|
value = *ptr++;
|
2012-06-10 20:16:01 +02:00
|
|
|
if (value && dir_attributes)
|
2012-06-10 02:13:59 +02:00
|
|
|
{
|
2012-06-10 03:16:46 +02:00
|
|
|
memcpy(dir_attributes, ptr, sizeof(struct nfs_fattr));
|
2012-06-10 02:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-06-09 21:29:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nfs_findnode
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2012-06-09 21:29:49 +02:00
|
|
|
* Given a path to something that may or may not be in the file system,
|
2012-06-11 16:39:19 +02:00
|
|
|
* return the handle of the directory entry of the requested object.
|
2012-06-09 21:29:49 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-01 21:53:45 +01:00
|
|
|
* Zero on success; a negative errno value on failure.
|
2012-06-09 21:29:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
int nfs_findnode(FAR struct nfsmount *nmp, FAR const char *relpath,
|
2019-10-27 18:48:14 +01:00
|
|
|
FAR struct file_handle *fhandle,
|
|
|
|
FAR struct nfs_fattr *obj_attributes,
|
2012-06-09 21:29:49 +02:00
|
|
|
FAR struct nfs_fattr *dir_attributes)
|
|
|
|
{
|
|
|
|
FAR const char *path = relpath;
|
2019-03-01 22:00:00 +01:00
|
|
|
char buffer[NAME_MAX + 1];
|
2012-06-09 21:29:49 +02:00
|
|
|
char terminator;
|
2012-06-11 16:39:19 +02:00
|
|
|
uint32_t tmp;
|
2012-06-09 21:29:49 +02:00
|
|
|
int error;
|
|
|
|
|
2012-06-10 20:16:01 +02:00
|
|
|
/* Start with the file handle of the root directory. */
|
2012-06-09 21:29:49 +02:00
|
|
|
|
|
|
|
fhandle->length = nmp->nm_fhsize;
|
2020-03-01 21:53:45 +01:00
|
|
|
memcpy(&fhandle->handle, nmp->nm_fh, nmp->nm_fhsize);
|
2012-06-09 21:29:49 +02:00
|
|
|
|
|
|
|
/* If no path was provided, then the root directory must be exactly what
|
|
|
|
* the caller is looking for.
|
|
|
|
*/
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
if (*path == '\0')
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
2012-06-10 20:16:01 +02:00
|
|
|
/* Return the root directory attributes */
|
|
|
|
|
|
|
|
if (obj_attributes)
|
|
|
|
{
|
|
|
|
memcpy(obj_attributes, &nmp->nm_fattr, sizeof(struct nfs_fattr));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir_attributes)
|
|
|
|
{
|
|
|
|
memcpy(dir_attributes, &nmp->nm_fattr, sizeof(struct nfs_fattr));
|
|
|
|
}
|
|
|
|
|
2012-06-09 21:29:49 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-05-25 20:45:57 +02:00
|
|
|
/* This is not the root directory. Loop until the directory entry
|
|
|
|
* corresponding to the path is found.
|
2012-06-09 21:29:49 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
|
|
|
/* Extract the next path segment name. */
|
|
|
|
|
|
|
|
error = nfs_pathsegment(&path, buffer, &terminator);
|
2012-06-11 20:55:58 +02:00
|
|
|
if (error != OK)
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
|
|
|
/* The filename segment contains is too long. */
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: nfs_pathsegment of \"%s\" failed after \"%s\": %d\n",
|
2012-06-09 21:29:49 +02:00
|
|
|
relpath, buffer, error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look-up this path segment */
|
|
|
|
|
2020-05-25 20:45:57 +02:00
|
|
|
error = nfs_lookup(nmp, buffer, fhandle, obj_attributes,
|
|
|
|
dir_attributes);
|
2012-06-11 20:55:58 +02:00
|
|
|
if (error != OK)
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: nfs_lookup of \"%s\" failed at \"%s\": %d\n",
|
2012-06-09 21:29:49 +02:00
|
|
|
relpath, buffer, error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the terminator character in the path was the end of the string
|
|
|
|
* then we have successfully found the directory entry that describes
|
|
|
|
* the path.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!terminator)
|
|
|
|
{
|
|
|
|
/* Return success meaning that the description the matching
|
2020-05-25 20:45:57 +02:00
|
|
|
* directory entry is in fhandle, obj_attributes, and
|
|
|
|
* dir_attributes.
|
2012-06-09 21:29:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. then we have found one of the intermediate directories on
|
|
|
|
* the way to the final path target. In this case, make sure
|
|
|
|
* the thing that we found is, indeed, a directory.
|
|
|
|
*/
|
|
|
|
|
2012-06-11 16:39:19 +02:00
|
|
|
tmp = fxdr_unsigned(uint32_t, obj_attributes->fa_type);
|
|
|
|
if (tmp != NFDIR)
|
|
|
|
{
|
|
|
|
/* Ooops.. we found something else */
|
|
|
|
|
2020-05-25 20:45:57 +02:00
|
|
|
ferr("ERROR: Intermediate segment \"%s\" of \'%s\" is not a "
|
|
|
|
"directory\n",
|
2020-03-01 21:53:45 +01:00
|
|
|
buffer, relpath);
|
|
|
|
return -ENOTDIR;
|
2012-06-11 16:39:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nfs_finddir
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2012-06-11 16:39:19 +02:00
|
|
|
* Given a path to something that may or may not be in the file system,
|
|
|
|
* return the handle of the entry of the directory containing the requested
|
2015-10-11 19:39:29 +02:00
|
|
|
* object.
|
2012-06-11 16:39:19 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-03-01 21:53:45 +01:00
|
|
|
* Zero on success; a negative errno value on failure.
|
2012-06-11 16:39:19 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
int nfs_finddir(FAR struct nfsmount *nmp, FAR const char *relpath,
|
2012-06-11 16:39:19 +02:00
|
|
|
FAR struct file_handle *fhandle,
|
2012-06-11 19:14:46 +02:00
|
|
|
FAR struct nfs_fattr *attributes, FAR char *filename)
|
2012-06-11 16:39:19 +02:00
|
|
|
{
|
|
|
|
FAR const char *path = relpath;
|
|
|
|
uint32_t tmp;
|
|
|
|
char terminator;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Verify that a path was provided */
|
|
|
|
|
2020-03-01 21:53:45 +01:00
|
|
|
if (*path == '\0')
|
2012-06-11 16:39:19 +02:00
|
|
|
{
|
2020-03-01 21:53:45 +01:00
|
|
|
return -ENOENT;
|
2012-06-11 16:39:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start with the file handle of the root directory. */
|
|
|
|
|
|
|
|
fhandle->length = nmp->nm_fhsize;
|
2020-03-01 21:53:45 +01:00
|
|
|
memcpy(&fhandle->handle, nmp->nm_fh, nmp->nm_fhsize);
|
2012-06-11 16:39:19 +02:00
|
|
|
memcpy(attributes, &nmp->nm_fattr, sizeof(struct nfs_fattr));
|
|
|
|
|
|
|
|
/* Loop until the directory entry containing the path is found. */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2012-06-11 16:39:19 +02:00
|
|
|
{
|
|
|
|
/* Extract the next path segment name. */
|
|
|
|
|
2012-06-11 19:14:46 +02:00
|
|
|
error = nfs_pathsegment(&path, filename, &terminator);
|
2012-06-11 20:55:58 +02:00
|
|
|
if (error != OK)
|
2012-06-11 16:39:19 +02:00
|
|
|
{
|
|
|
|
/* The filename segment contains is too long. */
|
|
|
|
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: nfs_pathsegment of \"%s\" failed after \"%s\": %d\n",
|
2012-06-11 19:14:46 +02:00
|
|
|
relpath, filename, error);
|
2012-06-11 16:39:19 +02:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the terminator character in the path was the end of the string
|
|
|
|
* then we have successfully found the directory that contains the name
|
|
|
|
* of interest.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!terminator)
|
|
|
|
{
|
|
|
|
/* Return success meaning that the description of the directory
|
|
|
|
* containing the object is in fhandle and attributes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look-up the next path segment */
|
|
|
|
|
2012-06-11 19:14:46 +02:00
|
|
|
error = nfs_lookup(nmp, filename, fhandle, attributes, NULL);
|
2012-06-11 20:55:58 +02:00
|
|
|
if (error != OK)
|
2012-06-11 16:39:19 +02:00
|
|
|
{
|
2016-06-12 01:14:02 +02:00
|
|
|
ferr("ERROR: fs_lookup of \"%s\" failed at \"%s\": %d\n",
|
2012-06-11 19:14:46 +02:00
|
|
|
relpath, filename, error);
|
2012-06-11 16:39:19 +02:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the thing that we found is, indeed, a directory. */
|
|
|
|
|
|
|
|
tmp = fxdr_unsigned(uint32_t, attributes->fa_type);
|
|
|
|
if (tmp != NFDIR)
|
2012-06-09 21:29:49 +02:00
|
|
|
{
|
|
|
|
/* Ooops.. we found something else */
|
|
|
|
|
2020-05-25 20:45:57 +02:00
|
|
|
ferr("ERROR: Intermediate segment \"%s\" of \'%s\" is not a "
|
|
|
|
"directory\n",
|
2020-03-01 21:53:45 +01:00
|
|
|
filename, relpath);
|
|
|
|
return -ENOTDIR;
|
2012-06-09 21:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 18:11:31 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nfs_attrupdate
|
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2012-06-12 18:11:31 +02:00
|
|
|
* Update file attributes on write or after the file is modified.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-06-12 18:11:31 +02:00
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nfs_attrupdate(FAR struct nfsnode *np, FAR struct nfs_fattr *attributes)
|
|
|
|
{
|
2020-05-25 20:45:57 +02:00
|
|
|
/* Save a few of the files attribute values in file structure (host
|
|
|
|
* order).
|
|
|
|
*/
|
2017-02-13 17:07:43 +01:00
|
|
|
|
|
|
|
np->n_type = fxdr_unsigned(uint8_t, attributes->fa_type);
|
|
|
|
np->n_mode = fxdr_unsigned(uint16_t, attributes->fa_mode);
|
2012-06-12 18:11:31 +02:00
|
|
|
np->n_size = fxdr_hyper(&attributes->fa_size);
|
2017-02-13 17:07:43 +01:00
|
|
|
|
2021-08-10 09:38:51 +02:00
|
|
|
fxdr_nfsv3time(&attributes->fa_atime, &np->n_atime);
|
|
|
|
fxdr_nfsv3time(&attributes->fa_mtime, &np->n_mtime);
|
|
|
|
fxdr_nfsv3time(&attributes->fa_ctime, &np->n_ctime);
|
2012-06-12 18:11:31 +02:00
|
|
|
}
|