2 * Copyright (C) 2014 Marin Hannache <ipxe@mareo.fr>.
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.
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.
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
24 #include <ipxe/nfs_uri.h>
28 * Network File System protocol URI handling functions
32 int nfs_uri_init ( struct nfs_uri
*nfs_uri
, const struct uri
*uri
) {
33 if ( ! ( nfs_uri
->mountpoint
= strdup ( uri
->path
) ) )
36 nfs_uri
->filename
= basename ( nfs_uri
->mountpoint
);
37 if ( strchr ( uri
->path
, '/' ) != NULL
)
38 nfs_uri
->mountpoint
= dirname ( nfs_uri
->mountpoint
);
40 if ( nfs_uri
->filename
[0] == '\0' ) {
41 free ( nfs_uri
->mountpoint
);
45 if ( ! ( nfs_uri
->path
= strdup ( nfs_uri
->filename
) ) ) {
46 free ( nfs_uri
->mountpoint
);
49 nfs_uri
->lookup_pos
= nfs_uri
->path
;
54 char *nfs_uri_mountpoint ( const struct nfs_uri
*uri
) {
55 if ( uri
->mountpoint
+ 1 == uri
->filename
||
56 uri
->mountpoint
== uri
->filename
)
59 return uri
->mountpoint
;
62 int nfs_uri_next_mountpoint ( struct nfs_uri
*uri
) {
65 if ( uri
->mountpoint
+ 1 == uri
->filename
||
66 uri
->mountpoint
== uri
->filename
)
69 sep
= strrchr ( uri
->mountpoint
, '/' );
70 uri
->filename
[-1] = '/';
71 uri
->filename
= sep
+ 1;
75 if ( ! ( uri
->path
= strdup ( uri
->filename
) ) ) {
79 uri
->lookup_pos
= uri
->path
;
84 int nfs_uri_symlink ( struct nfs_uri
*uri
, const char *symlink
) {
91 if ( *symlink
== '/' )
93 if ( strncmp ( symlink
, uri
->mountpoint
,
94 strlen ( uri
->mountpoint
) ) != 0 )
97 len
= strlen ( uri
->lookup_pos
) + strlen ( symlink
) - \
98 strlen ( uri
->mountpoint
);
99 if ( ! ( new_path
= malloc ( len
* sizeof ( char ) ) ) )
102 strcpy ( new_path
, symlink
+ strlen ( uri
->mountpoint
) );
103 strcpy ( new_path
+ strlen ( new_path
), uri
->lookup_pos
);
106 len
= strlen ( uri
->lookup_pos
) + strlen ( symlink
);
107 if ( ! ( new_path
= malloc ( len
* sizeof ( char ) ) ) )
111 strcpy ( new_path
, symlink
);
112 strcpy ( new_path
+ strlen ( new_path
), uri
->lookup_pos
);
116 uri
->lookup_pos
= uri
->path
= new_path
;
121 char *nfs_uri_next_path_component ( struct nfs_uri
*uri
) {
128 for ( sep
= uri
->lookup_pos
; *sep
!= '\0' && *sep
!= '/'; sep
++ )
131 start
= uri
->lookup_pos
;
132 uri
->lookup_pos
= sep
;
133 if ( *sep
!= '\0' ) {
136 if ( *start
== '\0' )
137 return nfs_uri_next_path_component ( uri
);
143 void nfs_uri_free ( struct nfs_uri
*uri
) {
144 free ( uri
->mountpoint
);
146 uri
->mountpoint
= NULL
;