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

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

Definition at line 32 of file nfs_uri.c.

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().

                                                                    {
        if ( ! ( nfs_uri->mountpoint = strdup ( uri->path ) ) )
                return -ENOMEM;

        nfs_uri->filename = basename ( nfs_uri->mountpoint );
        if ( strchr ( uri->path, '/' ) != NULL )
                nfs_uri->mountpoint = dirname ( nfs_uri->mountpoint );

        if ( nfs_uri->filename[0] == '\0' ) {
                free ( nfs_uri->mountpoint );
                return -EINVAL;
        }

        if ( ! ( nfs_uri->path = strdup ( nfs_uri->filename ) ) ) {
                free ( nfs_uri->mountpoint );
                return -ENOMEM;
        }
        nfs_uri->lookup_pos = nfs_uri->path;

        return 0;
}
char* nfs_uri_mountpoint ( const struct nfs_uri uri)

Definition at line 54 of file nfs_uri.c.

References nfs_uri::filename, and nfs_uri::mountpoint.

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

                                                       {
        if ( uri->mountpoint + 1 == uri->filename ||
             uri->mountpoint     == uri->filename )
                return "/";

        return uri->mountpoint;
}
int nfs_uri_next_mountpoint ( struct nfs_uri uri)

Definition at line 62 of file nfs_uri.c.

References ENOENT, ENOMEM, nfs_uri::filename, free, nfs_uri::lookup_pos, nfs_uri::mountpoint, NULL, nfs_uri::path, strdup(), and strrchr().

Referenced by nfs_mount_deliver().

                                                    {
        char *sep;

        if ( uri->mountpoint + 1 == uri->filename ||
             uri->mountpoint     == uri->filename )
                return -ENOENT;

        sep = strrchr ( uri->mountpoint, '/' );
        uri->filename[-1] = '/';
        uri->filename     = sep + 1;
        *sep = '\0';

        free ( uri->path );
        if ( ! ( uri->path = strdup ( uri->filename ) ) ) {
                uri->path = NULL;
                return -ENOMEM;
        }
        uri->lookup_pos = uri->path;

        return 0;
}
int nfs_uri_symlink ( struct nfs_uri uri,
const char *  symlink 
)

Definition at line 84 of file nfs_uri.c.

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

Referenced by nfs_deliver().

                                                                 {
        size_t len;
        char *new_path;

        if ( ! uri->path )
                return -EINVAL;

        if ( *symlink == '/' )
        {
                if ( strncmp ( symlink, uri->mountpoint,
                               strlen ( uri->mountpoint ) ) != 0 )
                        return -EINVAL;

                len = strlen ( uri->lookup_pos ) + strlen ( symlink ) - \
                      strlen ( uri->mountpoint );
                if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
                        return -ENOMEM;

                strcpy ( new_path, symlink + strlen ( uri->mountpoint ) );
                strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );

        } else {
                len = strlen ( uri->lookup_pos ) + strlen ( symlink );
                if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
                        return -ENOMEM;


                strcpy ( new_path, symlink );
                strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
        }

        free ( uri->path );
        uri->lookup_pos = uri->path = new_path;

        return 0;
}
char* nfs_uri_next_path_component ( struct nfs_uri uri)

Definition at line 121 of file nfs_uri.c.

References nfs_uri::lookup_pos, nfs_uri_next_path_component(), NULL, nfs_uri::path, and start.

Referenced by nfs_step(), and nfs_uri_next_path_component().

                                                          {
        char *sep;
        char *start;

        if ( ! uri->path )
                return NULL;

        for ( sep = uri->lookup_pos ; *sep != '\0' && *sep != '/'; sep++ )
                ;

        start = uri->lookup_pos;
        uri->lookup_pos = sep;
        if ( *sep != '\0' ) {
                uri->lookup_pos++;
                *sep = '\0';
                if ( *start == '\0' )
                        return nfs_uri_next_path_component ( uri );
        }

        return start;
}
void nfs_uri_free ( struct nfs_uri uri)

Definition at line 143 of file nfs_uri.c.

References free, nfs_uri::mountpoint, NULL, and nfs_uri::path.

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

                                          {
        free ( uri->mountpoint );
        free ( uri->path );
        uri->mountpoint = NULL;
        uri->path       = NULL;
}