You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nfs_uri.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <libgen.h>
  23. #include <ipxe/nfs_uri.h>
  24. /** @file
  25. *
  26. * Network File System protocol URI handling functions
  27. *
  28. */
  29. int nfs_uri_init ( struct nfs_uri *nfs_uri, const struct uri *uri ) {
  30. if ( ! ( nfs_uri->mountpoint = strdup ( uri->path ) ) )
  31. return -ENOMEM;
  32. nfs_uri->filename = basename ( nfs_uri->mountpoint );
  33. if ( strchr ( uri->path, '/' ) != NULL )
  34. nfs_uri->mountpoint = dirname ( nfs_uri->mountpoint );
  35. if ( nfs_uri->filename[0] == '\0' ) {
  36. free ( nfs_uri->mountpoint );
  37. return -EINVAL;
  38. }
  39. if ( ! ( nfs_uri->path = strdup ( nfs_uri->filename ) ) ) {
  40. free ( nfs_uri->mountpoint );
  41. return -ENOMEM;
  42. }
  43. nfs_uri->lookup_pos = nfs_uri->path;
  44. return 0;
  45. }
  46. char *nfs_uri_mountpoint ( const struct nfs_uri *uri ) {
  47. if ( uri->mountpoint + 1 == uri->filename ||
  48. uri->mountpoint == uri->filename )
  49. return "/";
  50. return uri->mountpoint;
  51. }
  52. int nfs_uri_next_mountpoint ( struct nfs_uri *uri ) {
  53. char *sep;
  54. if ( uri->mountpoint + 1 == uri->filename ||
  55. uri->mountpoint == uri->filename )
  56. return -ENOENT;
  57. sep = strrchr ( uri->mountpoint, '/' );
  58. uri->filename[-1] = '/';
  59. uri->filename = sep + 1;
  60. *sep = '\0';
  61. free ( uri->path );
  62. if ( ! ( uri->path = strdup ( uri->filename ) ) ) {
  63. uri->path = NULL;
  64. return -ENOMEM;
  65. }
  66. uri->lookup_pos = uri->path;
  67. return 0;
  68. }
  69. int nfs_uri_symlink ( struct nfs_uri *uri, const char *symlink ) {
  70. size_t len;
  71. char *new_path;
  72. if ( ! uri->path )
  73. return -EINVAL;
  74. if ( *symlink == '/' )
  75. {
  76. if ( strncmp ( symlink, uri->mountpoint,
  77. strlen ( uri->mountpoint ) ) != 0 )
  78. return -EINVAL;
  79. len = strlen ( uri->lookup_pos ) + strlen ( symlink ) - \
  80. strlen ( uri->mountpoint );
  81. if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
  82. return -ENOMEM;
  83. strcpy ( new_path, symlink + strlen ( uri->mountpoint ) );
  84. strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
  85. } else {
  86. len = strlen ( uri->lookup_pos ) + strlen ( symlink );
  87. if ( ! ( new_path = malloc ( len * sizeof ( char ) ) ) )
  88. return -ENOMEM;
  89. strcpy ( new_path, symlink );
  90. strcpy ( new_path + strlen ( new_path ), uri->lookup_pos );
  91. }
  92. free ( uri->path );
  93. uri->lookup_pos = uri->path = new_path;
  94. return 0;
  95. }
  96. char *nfs_uri_next_path_component ( struct nfs_uri *uri ) {
  97. char *sep;
  98. char *start;
  99. if ( ! uri->path )
  100. return NULL;
  101. for ( sep = uri->lookup_pos ; *sep != '\0' && *sep != '/'; sep++ )
  102. ;
  103. start = uri->lookup_pos;
  104. uri->lookup_pos = sep;
  105. if ( *sep != '\0' ) {
  106. uri->lookup_pos++;
  107. *sep = '\0';
  108. if ( *start == '\0' )
  109. return nfs_uri_next_path_component ( uri );
  110. }
  111. return start;
  112. }
  113. void nfs_uri_free ( struct nfs_uri *uri ) {
  114. free ( uri->mountpoint );
  115. free ( uri->path );
  116. uri->mountpoint = NULL;
  117. uri->path = NULL;
  118. }