lookup-at.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Lookup helper function for Hurd implementation of *at functions.
  2. Copyright (C) 2006-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <hurd.h>
  16. #include <hurd/lookup.h>
  17. #include <hurd/fd.h>
  18. #include <string.h>
  19. #include <fcntl.h>
  20. file_t
  21. __file_name_lookup_at (int fd, int at_flags,
  22. const char *file_name, int flags, mode_t mode)
  23. {
  24. error_t err;
  25. file_t result;
  26. int empty = at_flags & AT_EMPTY_PATH;
  27. int orig_flags;
  28. at_flags &= ~AT_EMPTY_PATH;
  29. err = __hurd_at_flags (&at_flags, &flags);
  30. if (err)
  31. return (__hurd_fail (err), MACH_PORT_NULL);
  32. if (empty != 0 && file_name[0] == '\0')
  33. {
  34. enum retry_type doretry;
  35. string_t retryname;
  36. err = HURD_DPORT_USE (fd, __dir_lookup (port, "", flags, mode,
  37. &doretry, retryname,
  38. &result));
  39. if (! err)
  40. err = __hurd_file_name_lookup_retry (&_hurd_ports_use, &__getdport,
  41. NULL, doretry, retryname,
  42. flags, mode, &result);
  43. return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
  44. }
  45. orig_flags = flags;
  46. if (flags & O_TMPFILE)
  47. flags = O_DIRECTORY;
  48. if (fd == AT_FDCWD || file_name[0] == '/')
  49. {
  50. err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
  51. file_name, flags, mode & ~_hurd_umask,
  52. &result);
  53. if (err)
  54. {
  55. __hurd_fail (err);
  56. return MACH_PORT_NULL;
  57. }
  58. }
  59. else
  60. {
  61. file_t startdir;
  62. /* We need to look the file up relative to the given directory (and
  63. not our cwd). For this to work, we supply our own wrapper for
  64. _hurd_ports_use, which replaces cwd with our startdir. */
  65. error_t use_init_port (int which, error_t (*operate) (mach_port_t))
  66. {
  67. return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
  68. : _hurd_ports_use (which, operate));
  69. }
  70. err = HURD_DPORT_USE (fd, (startdir = port,
  71. __hurd_file_name_lookup (&use_init_port,
  72. &__getdport, NULL,
  73. file_name,
  74. flags,
  75. mode & ~_hurd_umask,
  76. &result)));
  77. if (err)
  78. {
  79. __hurd_dfail (fd, err);
  80. return MACH_PORT_NULL;
  81. }
  82. }
  83. if (orig_flags & O_TMPFILE)
  84. {
  85. /* What we have looked up is not the file itself, but actually
  86. the directory to create the file in. Do that now. */
  87. file_t dir = result;
  88. err = __dir_mkfile (dir, orig_flags & ~(O_TMPFILE | O_DIRECTORY),
  89. mode, &result);
  90. __mach_port_deallocate (__mach_task_self (), dir);
  91. if (err)
  92. {
  93. __hurd_fail (err);
  94. return MACH_PORT_NULL;
  95. }
  96. }
  97. return result;
  98. }
  99. file_t
  100. __file_name_split_at (int fd, const char *file_name, char **name)
  101. {
  102. error_t err;
  103. file_t result;
  104. if (fd == AT_FDCWD || file_name[0] == '/')
  105. return __file_name_split (file_name, name);
  106. err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
  107. file_name, &result, name);
  108. file_t startdir;
  109. error_t use_init_port (int which, error_t (*operate) (mach_port_t))
  110. {
  111. return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
  112. : _hurd_ports_use (which, operate));
  113. }
  114. err = HURD_DPORT_USE (fd, (startdir = port,
  115. __hurd_file_name_split (&use_init_port,
  116. &__getdport, 0,
  117. file_name,
  118. &result, name)));
  119. return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
  120. }
  121. file_t
  122. __directory_name_split_at (int fd, const char *directory_name, char **name)
  123. {
  124. error_t err;
  125. file_t result;
  126. if (fd == AT_FDCWD || directory_name[0] == '/')
  127. return __directory_name_split (directory_name, name);
  128. file_t startdir;
  129. error_t use_init_port (int which, error_t (*operate) (mach_port_t))
  130. {
  131. return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
  132. : _hurd_ports_use (which, operate));
  133. }
  134. err = HURD_DPORT_USE (fd, (startdir = port,
  135. __hurd_directory_name_split (&use_init_port,
  136. &__getdport, 0,
  137. directory_name,
  138. &result, name)));
  139. return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
  140. }