blob: fad4b3a0bef141dbf3bea64dc97f7515d7598a41 [file] [log] [blame]
kumarashishg826308d2023-06-23 13:21:22 +00001// Copyright 2014 The PDFium Authors
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fxcrt/cfx_fileaccess_posix.h"
8
Haibo Huang49cc9302020-04-27 16:14:24 -07009#include <fcntl.h>
10#include <sys/stat.h>
11#include <unistd.h>
12
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070013#include <memory>
14
Haibo Huang49cc9302020-04-27 16:14:24 -070015#include "core/fxcrt/fx_stream.h"
kumarashishg826308d2023-06-23 13:21:22 +000016#include "third_party/base/numerics/safe_conversions.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070017
18#ifndef O_BINARY
19#define O_BINARY 0
20#endif // O_BINARY
21
22#ifndef O_LARGEFILE
23#define O_LARGEFILE 0
24#endif // O_LARGEFILE
25
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070026// static
Haibo Huang49cc9302020-04-27 16:14:24 -070027std::unique_ptr<FileAccessIface> FileAccessIface::Create() {
kumarashishg826308d2023-06-23 13:21:22 +000028 return std::make_unique<CFX_FileAccess_Posix>();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070029}
30
31CFX_FileAccess_Posix::CFX_FileAccess_Posix() : m_nFD(-1) {}
32
33CFX_FileAccess_Posix::~CFX_FileAccess_Posix() {
34 Close();
35}
36
kumarashishg826308d2023-06-23 13:21:22 +000037bool CFX_FileAccess_Posix::Open(ByteStringView fileName) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070038 if (m_nFD > -1)
39 return false;
40
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070041 // TODO(tsepez): check usage of c_str() below.
kumarashishg826308d2023-06-23 13:21:22 +000042 m_nFD =
43 open(fileName.unterminated_c_str(), O_BINARY | O_LARGEFILE | O_RDONLY);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070044 return m_nFD > -1;
45}
46
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070047void CFX_FileAccess_Posix::Close() {
48 if (m_nFD < 0) {
49 return;
50 }
51 close(m_nFD);
52 m_nFD = -1;
53}
54FX_FILESIZE CFX_FileAccess_Posix::GetSize() const {
55 if (m_nFD < 0) {
56 return 0;
57 }
58 struct stat s;
59 memset(&s, 0, sizeof(s));
60 fstat(m_nFD, &s);
kumarashishg826308d2023-06-23 13:21:22 +000061 return pdfium::base::checked_cast<FX_FILESIZE>(s.st_size);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070062}
63FX_FILESIZE CFX_FileAccess_Posix::GetPosition() const {
64 if (m_nFD < 0) {
65 return (FX_FILESIZE)-1;
66 }
67 return lseek(m_nFD, 0, SEEK_CUR);
68}
69FX_FILESIZE CFX_FileAccess_Posix::SetPosition(FX_FILESIZE pos) {
70 if (m_nFD < 0) {
71 return (FX_FILESIZE)-1;
72 }
73 return lseek(m_nFD, pos, SEEK_SET);
74}
75size_t CFX_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer) {
76 if (m_nFD < 0) {
77 return 0;
78 }
79 return read(m_nFD, pBuffer, szBuffer);
80}
81size_t CFX_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer) {
82 if (m_nFD < 0) {
83 return 0;
84 }
85 return write(m_nFD, pBuffer, szBuffer);
86}
87size_t CFX_FileAccess_Posix::ReadPos(void* pBuffer,
88 size_t szBuffer,
89 FX_FILESIZE pos) {
90 if (m_nFD < 0) {
91 return 0;
92 }
93 if (pos >= GetSize()) {
94 return 0;
95 }
96 if (SetPosition(pos) == (FX_FILESIZE)-1) {
97 return 0;
98 }
99 return Read(pBuffer, szBuffer);
100}
101size_t CFX_FileAccess_Posix::WritePos(const void* pBuffer,
102 size_t szBuffer,
103 FX_FILESIZE pos) {
104 if (m_nFD < 0) {
105 return 0;
106 }
107 if (SetPosition(pos) == (FX_FILESIZE)-1) {
108 return 0;
109 }
110 return Write(pBuffer, szBuffer);
111}
112
113bool CFX_FileAccess_Posix::Flush() {
114 if (m_nFD < 0)
115 return false;
116
117 return fsync(m_nFD) > -1;
118}
119
120bool CFX_FileAccess_Posix::Truncate(FX_FILESIZE szFile) {
121 if (m_nFD < 0)
122 return false;
123
124 return !ftruncate(m_nFD, szFile);
125}