-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cc
More file actions
138 lines (123 loc) · 3 KB
/
Copy pathfile.cc
File metadata and controls
138 lines (123 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "bsdiff/file.h"
#include <errno.h>
#include <fcntl.h>
#ifdef __linux__
#include <linux/fs.h>
#endif // __linux__
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// TEMP_FAILURE_RETRY is defined by some versions of <unistd.h>.
#ifndef TEMP_FAILURE_RETRY
#if defined(__ANDROID__)
#include <utils/Compat.h>
#else
#define TEMP_FAILURE_RETRY(exp) \
({ \
decltype(exp) _rc; \
do { \
_rc = (exp); \
} while (_rc == -1 && errno == EINTR); \
_rc; \
})
#endif
#endif // TEMP_FAILURE_RETRY
#include <algorithm>
namespace bsdiff {
std::unique_ptr<File> File::FOpen(const char* pathname, int flags) {
int fd = TEMP_FAILURE_RETRY(open(pathname, flags, 0644));
if (fd < 0)
return std::unique_ptr<File>();
return std::unique_ptr<File>(new File(fd));
}
File::~File() {
Close();
}
bool File::Read(void* buf, size_t count, size_t* bytes_read) {
if (fd_ < 0) {
errno = EBADF;
return false;
}
ssize_t rc = TEMP_FAILURE_RETRY(read(fd_, buf, count));
if (rc == -1)
return false;
*bytes_read = static_cast<size_t>(rc);
return true;
}
bool File::Write(const void* buf, size_t count, size_t* bytes_written) {
if (fd_ < 0) {
errno = EBADF;
return false;
}
ssize_t rc = TEMP_FAILURE_RETRY(write(fd_, buf, count));
if (rc == -1)
return false;
*bytes_written = static_cast<size_t>(rc);
return true;
}
bool File::Seek(off_t pos) {
if (fd_ < 0) {
errno = EBADF;
return false;
}
off_t newpos = lseek(fd_, pos, SEEK_SET);
if (newpos < 0)
return false;
if (newpos != pos) {
errno = EINVAL;
return false;
}
return true;
}
bool File::Close() {
if (fd_ < 0) {
errno = EBADF;
return false;
}
bool success = close(fd_) == 0;
if (!success && errno == EINTR)
success = true;
fd_ = -1;
return success;
}
bool File::GetSize(uint64_t* size) {
struct stat stbuf;
if (fstat(fd_, &stbuf) == -1)
return false;
if (S_ISREG(stbuf.st_mode)) {
*size = stbuf.st_size;
return true;
}
if (S_ISBLK(stbuf.st_mode)) {
#if defined(BLKGETSIZE64)
return ioctl(fd_, BLKGETSIZE64, size);
#elif defined(DKIOCGETBLOCKCOUNT)
uint64_t sectors = 0;
if (ioctl(fd_, DKIOCGETBLOCKCOUNT, §ors) == 0) {
*size = sectors << 9;
return true;
}
return false;
#else
// Fall back to doing seeks to know the EOF.
off_t pos = lseek(fd_, 0, SEEK_CUR);
if (pos == -1)
return false;
off_t end_pos = lseek(fd_, 0, SEEK_END);
if (end_pos == -1)
return false;
*size = end_pos;
lseek(fd_, 0, SEEK_END);
return true;
#endif
}
return false;
}
File::File(int fd)
: fd_(fd) {}
} // namespace bsdiff