FileStream_POSIX.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // FileStream_POSIX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/FileStream_POSIX.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: FileStream
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/FileStream.h"
  16. #include "Poco/File.h"
  17. #include "Poco/Exception.h"
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. namespace Poco {
  23. FileStreamBuf::FileStreamBuf():
  24. BufferedBidirectionalStreamBuf(BUFFER_SIZE, std::ios::in | std::ios::out),
  25. _fd(-1),
  26. _pos(0)
  27. {
  28. }
  29. FileStreamBuf::~FileStreamBuf()
  30. {
  31. close();
  32. }
  33. void FileStreamBuf::open(const std::string& path, std::ios::openmode mode)
  34. {
  35. poco_assert (_fd == -1);
  36. _pos = 0;
  37. _path = path;
  38. setMode(mode);
  39. resetBuffers();
  40. int flags(0);
  41. if (mode & std::ios::trunc)
  42. flags |= O_TRUNC;
  43. if (mode & std::ios::app)
  44. flags |= O_APPEND;
  45. if (mode & std::ios::out)
  46. flags |= O_CREAT;
  47. if ((mode & std::ios::in) && (mode & std::ios::out))
  48. flags |= O_RDWR;
  49. else if (mode & std::ios::in)
  50. flags |= O_RDONLY;
  51. else
  52. flags |= O_WRONLY;
  53. _fd = ::open(path.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  54. if (_fd == -1)
  55. File::handleLastError(_path);
  56. if ((mode & std::ios::app) || (mode & std::ios::ate))
  57. seekoff(0, std::ios::end, mode);
  58. }
  59. int FileStreamBuf::readFromDevice(char* buffer, std::streamsize length)
  60. {
  61. if (_fd == -1) return -1;
  62. if (getMode() & std::ios::out)
  63. sync();
  64. int n = read(_fd, buffer, length);
  65. if (n == -1)
  66. File::handleLastError(_path);
  67. _pos += n;
  68. return n;
  69. }
  70. int FileStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
  71. {
  72. if (_fd == -1) return -1;
  73. #if defined(POCO_VXWORKS)
  74. int n = write(_fd, const_cast<char*>(buffer), length);
  75. #else
  76. int n = write(_fd, buffer, length);
  77. #endif
  78. if (n == -1)
  79. File::handleLastError(_path);
  80. _pos += n;
  81. return n;
  82. }
  83. bool FileStreamBuf::close()
  84. {
  85. bool success = true;
  86. if (_fd != -1)
  87. {
  88. try
  89. {
  90. sync();
  91. }
  92. catch (...)
  93. {
  94. success = false;
  95. }
  96. ::close(_fd);
  97. _fd = -1;
  98. }
  99. return success;
  100. }
  101. std::streampos FileStreamBuf::seekoff(std::streamoff off, std::ios::seekdir dir, std::ios::openmode mode)
  102. {
  103. if (_fd == -1 || !(getMode() & mode))
  104. return -1;
  105. if (getMode() & std::ios::out)
  106. sync();
  107. std::streamoff adj;
  108. if (mode & std::ios::in)
  109. adj = static_cast<std::streamoff>(egptr() - gptr());
  110. else
  111. adj = 0;
  112. resetBuffers();
  113. int whence = SEEK_SET;
  114. if (dir == std::ios::cur)
  115. {
  116. whence = SEEK_CUR;
  117. off -= adj;
  118. }
  119. else if (dir == std::ios::end)
  120. {
  121. whence = SEEK_END;
  122. }
  123. _pos = lseek(_fd, off, whence);
  124. return _pos;
  125. }
  126. std::streampos FileStreamBuf::seekpos(std::streampos pos, std::ios::openmode mode)
  127. {
  128. if (_fd == -1 || !(getMode() & mode))
  129. return -1;
  130. if (getMode() & std::ios::out)
  131. sync();
  132. resetBuffers();
  133. _pos = lseek(_fd, pos, SEEK_SET);
  134. return _pos;
  135. }
  136. } // namespace Poco