File.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef __serenity__
  27. # include <Kernel/Syscall.h>
  28. #endif
  29. #include <LibCore/File.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <sys/stat.h>
  34. #include <unistd.h>
  35. namespace Core {
  36. Result<NonnullRefPtr<File>, String> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
  37. {
  38. auto file = File::construct(filename);
  39. if (!file->open_impl(mode, permissions))
  40. return String(file->error_string());
  41. return file;
  42. }
  43. File::File(const StringView& filename, Object* parent)
  44. : IODevice(parent)
  45. , m_filename(filename)
  46. {
  47. }
  48. File::~File()
  49. {
  50. if (m_should_close_file_descriptor == ShouldCloseFileDescription::Yes && mode() != NotOpen)
  51. close();
  52. }
  53. bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescription should_close)
  54. {
  55. set_fd(fd);
  56. set_mode(mode);
  57. m_should_close_file_descriptor = should_close;
  58. return true;
  59. }
  60. bool File::open(IODevice::OpenMode mode)
  61. {
  62. return open_impl(mode, 0666);
  63. }
  64. bool File::open_impl(IODevice::OpenMode mode, mode_t permissions)
  65. {
  66. ASSERT(!m_filename.is_null());
  67. int flags = 0;
  68. if ((mode & IODevice::ReadWrite) == IODevice::ReadWrite) {
  69. flags |= O_RDWR | O_CREAT;
  70. } else if (mode & IODevice::ReadOnly) {
  71. flags |= O_RDONLY;
  72. } else if (mode & IODevice::WriteOnly) {
  73. flags |= O_WRONLY | O_CREAT;
  74. bool should_truncate = !((mode & IODevice::Append) || (mode & IODevice::MustBeNew));
  75. if (should_truncate)
  76. flags |= O_TRUNC;
  77. }
  78. if (mode & IODevice::Append)
  79. flags |= O_APPEND;
  80. if (mode & IODevice::Truncate)
  81. flags |= O_TRUNC;
  82. if (mode & IODevice::MustBeNew)
  83. flags |= O_EXCL;
  84. int fd = ::open(m_filename.characters(), flags, permissions);
  85. if (fd < 0) {
  86. set_error(errno);
  87. return false;
  88. }
  89. set_fd(fd);
  90. set_mode(mode);
  91. return true;
  92. }
  93. bool File::is_directory() const
  94. {
  95. struct stat stat;
  96. if (fstat(fd(), &stat) < 0)
  97. return false;
  98. return S_ISDIR(stat.st_mode);
  99. }
  100. bool File::is_directory(const String& filename)
  101. {
  102. struct stat st;
  103. if (stat(filename.characters(), &st) < 0)
  104. return false;
  105. return S_ISDIR(st.st_mode);
  106. }
  107. bool File::exists(const String& filename)
  108. {
  109. struct stat st;
  110. return stat(filename.characters(), &st) == 0;
  111. }
  112. String File::real_path_for(const String& filename)
  113. {
  114. if (filename.is_null())
  115. return {};
  116. auto* path = realpath(filename.characters(), nullptr);
  117. String real_path(path);
  118. free(path);
  119. return real_path;
  120. }
  121. #ifdef __serenity__
  122. String File::read_link(const StringView& link_path)
  123. {
  124. // First, try using a 64-byte buffer, that ought to be enough for anybody.
  125. char small_buffer[64];
  126. Syscall::SC_readlink_params small_params {
  127. { link_path.characters_without_null_termination(), link_path.length() },
  128. { small_buffer, sizeof(small_buffer) }
  129. };
  130. int rc = syscall(SC_readlink, &small_params);
  131. if (rc < 0) {
  132. errno = -rc;
  133. return {};
  134. }
  135. size_t size = rc;
  136. // If the call was successful, the syscall (unlike the LibC wrapper)
  137. // returns the full size of the link. Let's see if our small buffer
  138. // was enough to read the whole link.
  139. if (size <= sizeof(small_buffer))
  140. return { small_buffer, size };
  141. // Nope, but at least now we know the right size.
  142. char* large_buffer_ptr;
  143. auto large_buffer = StringImpl::create_uninitialized(size, large_buffer_ptr);
  144. Syscall::SC_readlink_params large_params {
  145. { link_path.characters_without_null_termination(), link_path.length() },
  146. { large_buffer_ptr, (size_t)size }
  147. };
  148. rc = syscall(SC_readlink, &large_params);
  149. if (rc < 0) {
  150. errno = -rc;
  151. return {};
  152. }
  153. size_t new_size = rc;
  154. if (new_size == size)
  155. return { *large_buffer };
  156. // If we're here, the symlink has changed while we were looking at it.
  157. // If it became shorter, our buffer is valid, we just have to trim it a bit.
  158. if (new_size < size)
  159. return { large_buffer_ptr, new_size };
  160. // Otherwise, here's not much we can do, unless we want to loop endlessly
  161. // in this case. Let's leave it up to the caller whether to loop.
  162. errno = -EAGAIN;
  163. return {};
  164. }
  165. #else
  166. // This is a sad version for other systems. It has to always make a copy of the
  167. // link path, and to always make two syscalls to get the right size first.
  168. String File::read_link(const StringView& link_path)
  169. {
  170. String link_path_str = link_path;
  171. struct stat statbuf;
  172. int rc = lstat(link_path_str.characters(), &statbuf);
  173. if (rc < 0)
  174. return {};
  175. char* buffer_ptr;
  176. auto buffer = StringImpl::create_uninitialized(statbuf.st_size, buffer_ptr);
  177. rc = readlink(link_path_str.characters(), buffer_ptr, statbuf.st_size);
  178. if (rc < 0)
  179. return {};
  180. // (See above.)
  181. if (rc == statbuf.st_size)
  182. return { *buffer };
  183. return { buffer_ptr, (size_t)rc };
  184. }
  185. #endif
  186. }