iPXE
nfs_uri.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Marin Hannache <ipxe@mareo.fr>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <libgen.h>
24 #include <ipxe/nfs_uri.h>
25 
26 /** @file
27  *
28  * Network File System protocol URI handling functions
29  *
30  */
31 
32 int nfs_uri_init ( struct nfs_uri *nfs_uri, const struct uri *uri ) {
33  if ( ! ( nfs_uri->mountpoint = strdup ( uri->path ) ) )
34  return -ENOMEM;
35 
37  if ( strchr ( uri->path, '/' ) != NULL )
39 
40  if ( nfs_uri->filename[0] == '\0' ) {
41  free ( nfs_uri->mountpoint );
42  return -EINVAL;
43  }
44 
45  if ( ! ( nfs_uri->path = strdup ( nfs_uri->filename ) ) ) {
46  free ( nfs_uri->mountpoint );
47  return -ENOMEM;
48  }
50 
51  return 0;
52 }
53 
54 char *nfs_uri_mountpoint ( const struct nfs_uri *uri ) {
55  if ( uri->mountpoint + 1 == uri->filename ||
56  uri->mountpoint == uri->filename )
57  return "/";
58 
59  return uri->mountpoint;
60 }
61 
63  char *sep;
64 
65  if ( uri->mountpoint + 1 == uri->filename ||
66  uri->mountpoint == uri->filename )
67  return -ENOENT;
68 
69  sep = strrchr ( uri->mountpoint, '/' );
70  uri->filename[-1] = '/';
71  uri->filename = sep + 1;
72  *sep = '\0';
73 
74  free ( uri->path );
75  if ( ! ( uri->path = strdup ( uri->filename ) ) ) {
76  uri->path = NULL;
77  return -ENOMEM;
78  }
79  uri->lookup_pos = uri->path;
80 
81  return 0;
82 }
83 
84 int nfs_uri_symlink ( struct nfs_uri *uri, const char *symlink ) {
85  size_t len;
86  char *new_path;
87 
88  if ( ! uri->path )
89  return -EINVAL;
90 
91  if ( *symlink == '/' )
92  {
93  if ( strncmp ( symlink, uri->mountpoint,
94  strlen ( uri->mountpoint ) ) != 0 )
95  return -EINVAL;
96 
97  len = strlen ( uri->lookup_pos ) + strlen ( symlink ) - \
98  strlen ( uri->mountpoint );
99  if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
100  return -ENOMEM;
101 
102  strcpy ( new_path, symlink + strlen ( uri->mountpoint ) );
103  strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
104 
105  } else {
106  len = strlen ( uri->lookup_pos ) + strlen ( symlink );
107  if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
108  return -ENOMEM;
109 
110 
111  strcpy ( new_path, symlink );
112  strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
113  }
114 
115  free ( uri->path );
116  uri->lookup_pos = uri->path = new_path;
117 
118  return 0;
119 }
120 
122  char *sep;
123  char *start;
124 
125  if ( ! uri->path )
126  return NULL;
127 
128  for ( sep = uri->lookup_pos ; *sep != '\0' && *sep != '/'; sep++ )
129  ;
130 
131  start = uri->lookup_pos;
132  uri->lookup_pos = sep;
133  if ( *sep != '\0' ) {
134  uri->lookup_pos++;
135  *sep = '\0';
136  if ( *start == '\0' )
137  return nfs_uri_next_path_component ( uri );
138  }
139 
140  return start;
141 }
142 
143 void nfs_uri_free ( struct nfs_uri *uri ) {
144  free ( uri->mountpoint );
145  free ( uri->path );
146  uri->mountpoint = NULL;
147  uri->path = NULL;
148 }
void nfs_uri_free(struct nfs_uri *uri)
Definition: nfs_uri.c:143
#define EINVAL
Invalid argument.
Definition: errno.h:428
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:289
char * dirname(char *path)
Return directory name from path.
Definition: basename.c:57
Error codes.
#define ENOENT
No such file or directory.
Definition: errno.h:514
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition: string.c:186
char * filename
Definition: nfs_uri.h:16
char * lookup_pos
Definition: nfs_uri.h:18
uint32_t start
Starting offset.
Definition: netvsc.h:12
#define ENOMEM
Not enough space.
Definition: errno.h:534
const char * path
Path (after URI decoding)
Definition: uri.h:80
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:326
char * basename(char *path)
Return base name from path.
Definition: basename.c:42
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
int nfs_uri_init(struct nfs_uri *nfs_uri, const struct uri *uri)
Definition: nfs_uri.c:32
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
char * strdup(const char *src)
Duplicate string.
Definition: string.c:380
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
char * nfs_uri_mountpoint(const struct nfs_uri *uri)
Definition: nfs_uri.c:54
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint32_t len
Length.
Definition: ena.h:14
Network File System protocol URI handling functions.
char * mountpoint
Definition: nfs_uri.h:15
A Uniform Resource Identifier.
Definition: uri.h:64
int nfs_uri_symlink(struct nfs_uri *uri, const char *symlink)
Definition: nfs_uri.c:84
int nfs_uri_next_mountpoint(struct nfs_uri *uri)
Definition: nfs_uri.c:62
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
char * nfs_uri_next_path_component(struct nfs_uri *uri)
Definition: nfs_uri.c:121
char * path
Definition: nfs_uri.h:17