Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / oncrpc / nfs_uri.c
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
36         nfs_uri->filename = basename ( nfs_uri->mountpoint );
37         if ( strchr ( uri->path, '/' ) != NULL )
38                 nfs_uri->mountpoint = dirname ( nfs_uri->mountpoint );
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         }
49         nfs_uri->lookup_pos = nfs_uri->path;
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
62 int nfs_uri_next_mountpoint ( struct nfs_uri *uri ) {
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
121 char *nfs_uri_next_path_component ( struct nfs_uri *uri ) {
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 }