iPXE
Functions
nfs_uri.c File Reference

Network File System protocol URI handling functions. More...

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <ipxe/nfs_uri.h>

Go to the source code of this file.

Functions

int nfs_uri_init (struct nfs_uri *nfs_uri, const struct uri *uri)
 
char * nfs_uri_mountpoint (const struct nfs_uri *uri)
 
int nfs_uri_next_mountpoint (struct nfs_uri *uri)
 
int nfs_uri_symlink (struct nfs_uri *uri, const char *symlink)
 
char * nfs_uri_next_path_component (struct nfs_uri *uri)
 
void nfs_uri_free (struct nfs_uri *uri)
 

Detailed Description

Network File System protocol URI handling functions.

Definition in file nfs_uri.c.

Function Documentation

◆ nfs_uri_init()

int nfs_uri_init ( struct nfs_uri nfs_uri,
const struct uri uri 
)

Definition at line 32 of file nfs_uri.c.

32  {
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 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
char * dirname(char *path)
Return directory name from path.
Definition: basename.c:57
char * filename
Definition: nfs_uri.h:16
char * lookup_pos
Definition: nfs_uri.h:18
#define ENOMEM
Not enough space.
Definition: errno.h:534
const char * path
Path.
Definition: uri.h:66
char * basename(char *path)
Return base name from path.
Definition: basename.c:42
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:241
char * strdup(const char *src)
Duplicate string.
Definition: string.c:350
char * mountpoint
Definition: nfs_uri.h:15
A Uniform Resource Identifier.
Definition: uri.h:50
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
char * path
Definition: nfs_uri.h:17

References basename(), dirname(), EINVAL, ENOMEM, nfs_uri::filename, free, nfs_uri::lookup_pos, nfs_uri::mountpoint, NULL, nfs_uri::path, uri::path, strchr(), and strdup().

Referenced by nfs_parse_uri().

◆ nfs_uri_mountpoint()

char* nfs_uri_mountpoint ( const struct nfs_uri uri)

Definition at line 54 of file nfs_uri.c.

54  {
55  if ( uri->mountpoint + 1 == uri->filename ||
56  uri->mountpoint == uri->filename )
57  return "/";
58 
59  return uri->mountpoint;
60 }
A Uniform Resource Identifier.
Definition: uri.h:50

Referenced by nfs_mount_deliver(), nfs_mount_step(), and nfs_parse_uri().

◆ nfs_uri_next_mountpoint()

int nfs_uri_next_mountpoint ( struct nfs_uri uri)

Definition at line 62 of file nfs_uri.c.

62  {
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 }
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:259
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define ENOMEM
Not enough space.
Definition: errno.h:534
const char * path
Path.
Definition: uri.h:66
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * strdup(const char *src)
Duplicate string.
Definition: string.c:350
A Uniform Resource Identifier.
Definition: uri.h:50
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References ENOENT, ENOMEM, free, NULL, uri::path, strdup(), and strrchr().

Referenced by nfs_mount_deliver().

◆ nfs_uri_symlink()

int nfs_uri_symlink ( struct nfs_uri uri,
const char *  symlink 
)

Definition at line 84 of file nfs_uri.c.

84  {
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 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition: string.c:170
#define ENOMEM
Not enough space.
Definition: errno.h:534
const char * path
Path.
Definition: uri.h:66
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:296
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint32_t len
Length.
Definition: ena.h:14
A Uniform Resource Identifier.
Definition: uri.h:50

References EINVAL, ENOMEM, free, len, malloc(), uri::path, strcpy(), strlen(), and strncmp().

Referenced by nfs_deliver().

◆ nfs_uri_next_path_component()

char* nfs_uri_next_path_component ( struct nfs_uri uri)

Definition at line 121 of file nfs_uri.c.

121  {
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 }
uint32_t start
Starting offset.
Definition: netvsc.h:12
const char * path
Path.
Definition: uri.h:66
A Uniform Resource Identifier.
Definition: uri.h:50
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
char * nfs_uri_next_path_component(struct nfs_uri *uri)
Definition: nfs_uri.c:121

References nfs_uri_next_path_component(), NULL, uri::path, and start.

Referenced by nfs_step(), and nfs_uri_next_path_component().

◆ nfs_uri_free()

void nfs_uri_free ( struct nfs_uri uri)

Definition at line 143 of file nfs_uri.c.

143  {
144  free ( uri->mountpoint );
145  free ( uri->path );
146  uri->mountpoint = NULL;
147  uri->path = NULL;
148 }
const char * path
Path.
Definition: uri.h:66
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
A Uniform Resource Identifier.
Definition: uri.h:50
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References free, NULL, and uri::path.

Referenced by nfs_free(), nfs_open(), and nfs_parse_uri().