summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c
diff options
context:
space:
mode:
authorYang Zhang <yang.z.zhang@intel.com>2015-08-28 09:58:54 +0800
committerYang Zhang <yang.z.zhang@intel.com>2015-09-01 12:44:00 +0800
commite44e3482bdb4d0ebde2d8b41830ac2cdb07948fb (patch)
tree66b09f592c55df2878107a468a91d21506104d3f /qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c
parent9ca8dbcc65cfc63d6f5ef3312a33184e1d726e00 (diff)
Add qemu 2.4.0
Change-Id: Ic99cbad4b61f8b127b7dc74d04576c0bcbaaf4f5 Signed-off-by: Yang Zhang <yang.z.zhang@intel.com>
Diffstat (limited to 'qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c')
-rw-r--r--qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c b/qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c
new file mode 100644
index 000000000..c4c3f21e9
--- /dev/null
+++ b/qemu/roms/ipxe/src/net/oncrpc/nfs_uri.c
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2014 Marin Hannache <ipxe@mareo.fr>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <libgen.h>
+#include <ipxe/nfs_uri.h>
+
+/** @file
+ *
+ * Network File System protocol URI handling functions
+ *
+ */
+
+int nfs_uri_init ( struct nfs_uri *nfs_uri, const struct uri *uri ) {
+ if ( ! ( nfs_uri->mountpoint = strdup ( uri->path ) ) )
+ return -ENOMEM;
+
+ nfs_uri->filename = basename ( nfs_uri->mountpoint );
+ if ( strchr ( uri->path, '/' ) != NULL )
+ nfs_uri->mountpoint = dirname ( nfs_uri->mountpoint );
+
+ if ( nfs_uri->filename[0] == '\0' ) {
+ free ( nfs_uri->mountpoint );
+ return -EINVAL;
+ }
+
+ if ( ! ( nfs_uri->path = strdup ( nfs_uri->filename ) ) ) {
+ free ( nfs_uri->mountpoint );
+ return -ENOMEM;
+ }
+ nfs_uri->lookup_pos = nfs_uri->path;
+
+ return 0;
+}
+
+char *nfs_uri_mountpoint ( const struct nfs_uri *uri ) {
+ if ( uri->mountpoint + 1 == uri->filename ||
+ uri->mountpoint == uri->filename )
+ return "/";
+
+ return uri->mountpoint;
+}
+
+int nfs_uri_next_mountpoint ( struct nfs_uri *uri ) {
+ char *sep;
+
+ if ( uri->mountpoint + 1 == uri->filename ||
+ uri->mountpoint == uri->filename )
+ return -ENOENT;
+
+ sep = strrchr ( uri->mountpoint, '/' );
+ uri->filename[-1] = '/';
+ uri->filename = sep + 1;
+ *sep = '\0';
+
+ free ( uri->path );
+ if ( ! ( uri->path = strdup ( uri->filename ) ) ) {
+ uri->path = NULL;
+ return -ENOMEM;
+ }
+ uri->lookup_pos = uri->path;
+
+ return 0;
+}
+
+int nfs_uri_symlink ( struct nfs_uri *uri, const char *symlink ) {
+ size_t len;
+ char *new_path;
+
+ if ( ! uri->path )
+ return -EINVAL;
+
+ if ( *symlink == '/' )
+ {
+ if ( strncmp ( symlink, uri->mountpoint,
+ strlen ( uri->mountpoint ) ) != 0 )
+ return -EINVAL;
+
+ len = strlen ( uri->lookup_pos ) + strlen ( symlink ) - \
+ strlen ( uri->mountpoint );
+ if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
+ return -ENOMEM;
+
+ strcpy ( new_path, symlink + strlen ( uri->mountpoint ) );
+ strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
+
+ } else {
+ len = strlen ( uri->lookup_pos ) + strlen ( symlink );
+ if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
+ return -ENOMEM;
+
+
+ strcpy ( new_path, symlink );
+ strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
+ }
+
+ free ( uri->path );
+ uri->lookup_pos = uri->path = new_path;
+
+ return 0;
+}
+
+char *nfs_uri_next_path_component ( struct nfs_uri *uri ) {
+ char *sep;
+ char *start;
+
+ if ( ! uri->path )
+ return NULL;
+
+ for ( sep = uri->lookup_pos ; *sep != '\0' && *sep != '/'; sep++ )
+ ;
+
+ start = uri->lookup_pos;
+ uri->lookup_pos = sep;
+ if ( *sep != '\0' ) {
+ uri->lookup_pos++;
+ *sep = '\0';
+ if ( *start == '\0' )
+ return nfs_uri_next_path_component ( uri );
+ }
+
+ return start;
+}
+
+void nfs_uri_free ( struct nfs_uri *uri ) {
+ free ( uri->mountpoint );
+ free ( uri->path );
+ uri->mountpoint = NULL;
+ uri->path = NULL;
+}