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 FILE_SECBOOT ( FORBIDDEN );
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <libgen.h>
26 #include <ipxe/nfs_uri.h>
27 
28 /** @file
29  *
30  * Network File System protocol URI handling functions
31  *
32  */
33 
34 int nfs_uri_init ( struct nfs_uri *nfs_uri, const struct uri *uri ) {
35  if ( ! ( nfs_uri->mountpoint = strdup ( uri->path ) ) )
36  return -ENOMEM;
37 
39  if ( strchr ( uri->path, '/' ) != NULL )
41 
42  if ( nfs_uri->filename[0] == '\0' ) {
43  free ( nfs_uri->mountpoint );
44  return -EINVAL;
45  }
46 
47  if ( ! ( nfs_uri->path = strdup ( nfs_uri->filename ) ) ) {
48  free ( nfs_uri->mountpoint );
49  return -ENOMEM;
50  }
52 
53  return 0;
54 }
55 
56 char *nfs_uri_mountpoint ( const struct nfs_uri *uri ) {
57  if ( uri->mountpoint + 1 == uri->filename ||
58  uri->mountpoint == uri->filename )
59  return "/";
60 
61  return uri->mountpoint;
62 }
63 
65  char *sep;
66 
67  if ( uri->mountpoint + 1 == uri->filename ||
68  uri->mountpoint == uri->filename )
69  return -ENOENT;
70 
71  sep = strrchr ( uri->mountpoint, '/' );
72  uri->filename[-1] = '/';
73  uri->filename = sep + 1;
74  *sep = '\0';
75 
76  free ( uri->path );
77  if ( ! ( uri->path = strdup ( uri->filename ) ) ) {
78  uri->path = NULL;
79  return -ENOMEM;
80  }
81  uri->lookup_pos = uri->path;
82 
83  return 0;
84 }
85 
86 int nfs_uri_symlink ( struct nfs_uri *uri, const char *symlink ) {
87  size_t len;
88  char *new_path;
89 
90  if ( ! uri->path )
91  return -EINVAL;
92 
93  if ( *symlink == '/' )
94  {
95  if ( strncmp ( symlink, uri->mountpoint,
96  strlen ( uri->mountpoint ) ) != 0 )
97  return -EINVAL;
98 
99  len = strlen ( uri->lookup_pos ) + strlen ( symlink ) - \
100  strlen ( uri->mountpoint );
101  if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
102  return -ENOMEM;
103 
104  strcpy ( new_path, symlink + strlen ( uri->mountpoint ) );
105  strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
106 
107  } else {
108  len = strlen ( uri->lookup_pos ) + strlen ( symlink );
109  if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
110  return -ENOMEM;
111 
112 
113  strcpy ( new_path, symlink );
114  strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
115  }
116 
117  free ( uri->path );
118  uri->lookup_pos = uri->path = new_path;
119 
120  return 0;
121 }
122 
124  char *sep;
125  char *start;
126 
127  if ( ! uri->path )
128  return NULL;
129 
130  for ( sep = uri->lookup_pos ; *sep != '\0' && *sep != '/'; sep++ )
131  ;
132 
133  start = uri->lookup_pos;
134  uri->lookup_pos = sep;
135  if ( *sep != '\0' ) {
136  uri->lookup_pos++;
137  *sep = '\0';
138  if ( *start == '\0' )
139  return nfs_uri_next_path_component ( uri );
140  }
141 
142  return start;
143 }
144 
145 void nfs_uri_free ( struct nfs_uri *uri ) {
146  free ( uri->mountpoint );
147  free ( uri->path );
148  uri->mountpoint = NULL;
149  uri->path = NULL;
150 }
void nfs_uri_free(struct nfs_uri *uri)
Definition: nfs_uri.c:145
#define EINVAL
Invalid argument.
Definition: errno.h:429
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:290
char * dirname(char *path)
Return directory name from path.
Definition: basename.c:58
Error codes.
#define ENOENT
No such file or directory.
Definition: errno.h:515
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition: string.c:187
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:535
ring len
Length.
Definition: dwmac.h:231
const char * path
Path (after URI decoding)
Definition: uri.h:81
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:347
FILE_SECBOOT(FORBIDDEN)
char * basename(char *path)
Return base name from path.
Definition: basename.c:43
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
int nfs_uri_init(struct nfs_uri *nfs_uri, const struct uri *uri)
Definition: nfs_uri.c:34
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:272
char * strdup(const char *src)
Duplicate string.
Definition: string.c:394
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
char * nfs_uri_mountpoint(const struct nfs_uri *uri)
Definition: nfs_uri.c:56
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:621
Network File System protocol URI handling functions.
char * mountpoint
Definition: nfs_uri.h:15
A Uniform Resource Identifier.
Definition: uri.h:65
int nfs_uri_symlink(struct nfs_uri *uri, const char *symlink)
Definition: nfs_uri.c:86
int nfs_uri_next_mountpoint(struct nfs_uri *uri)
Definition: nfs_uri.c:64
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
char * nfs_uri_next_path_component(struct nfs_uri *uri)
Definition: nfs_uri.c:123
char * path
Definition: nfs_uri.h:17