File.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <LibCore/File.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. namespace Core {
  33. Result<NonnullRefPtr<File>, String> File::open(const String& filename, IODevice::OpenMode mode, mode_t permissions)
  34. {
  35. auto file = File::construct(filename);
  36. if (!file->open_impl(mode, permissions))
  37. return String(file->error_string());
  38. return file;
  39. }
  40. File::File(const StringView& filename, Object* parent)
  41. : IODevice(parent)
  42. , m_filename(filename)
  43. {
  44. }
  45. File::~File()
  46. {
  47. if (m_should_close_file_descriptor == ShouldCloseFileDescription::Yes && mode() != NotOpen)
  48. close();
  49. }
  50. bool File::open(int fd, IODevice::OpenMode mode, ShouldCloseFileDescription should_close)
  51. {
  52. set_fd(fd);
  53. set_mode(mode);
  54. m_should_close_file_descriptor = should_close;
  55. return true;
  56. }
  57. bool File::open(IODevice::OpenMode mode)
  58. {
  59. return open_impl(mode, 0666);
  60. }
  61. bool File::open_impl(IODevice::OpenMode mode, mode_t permissions)
  62. {
  63. ASSERT(!m_filename.is_null());
  64. int flags = 0;
  65. if ((mode & IODevice::ReadWrite) == IODevice::ReadWrite) {
  66. flags |= O_RDWR | O_CREAT;
  67. } else if (mode & IODevice::ReadOnly) {
  68. flags |= O_RDONLY;
  69. } else if (mode & IODevice::WriteOnly) {
  70. flags |= O_WRONLY | O_CREAT;
  71. bool should_truncate = !((mode & IODevice::Append) || (mode & IODevice::MustBeNew));
  72. if (should_truncate)
  73. flags |= O_TRUNC;
  74. }
  75. if (mode & IODevice::Append)
  76. flags |= O_APPEND;
  77. if (mode & IODevice::Truncate)
  78. flags |= O_TRUNC;
  79. if (mode & IODevice::MustBeNew)
  80. flags |= O_EXCL;
  81. int fd = ::open(m_filename.characters(), flags, permissions);
  82. if (fd < 0) {
  83. set_error(errno);
  84. return false;
  85. }
  86. set_fd(fd);
  87. set_mode(mode);
  88. return true;
  89. }
  90. bool File::is_directory() const
  91. {
  92. struct stat stat;
  93. if (fstat(fd(), &stat) < 0)
  94. return false;
  95. return S_ISDIR(stat.st_mode);
  96. }
  97. bool File::is_directory(const String& filename)
  98. {
  99. struct stat st;
  100. if (stat(filename.characters(), &st) < 0)
  101. return false;
  102. return S_ISDIR(st.st_mode);
  103. }
  104. bool File::exists(const String& filename)
  105. {
  106. struct stat st;
  107. return stat(filename.characters(), &st) == 0;
  108. }
  109. String File::real_path_for(const String& filename)
  110. {
  111. if (filename.is_null())
  112. return {};
  113. auto* path = realpath(filename.characters(), nullptr);
  114. String real_path(path);
  115. free(path);
  116. return real_path;
  117. }
  118. }