OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "update_engine/utils.h" | 5 #include "update_engine/utils.h" |
6 #include <sys/mount.h> | 6 #include <sys/mount.h> |
7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <dirent.h> | 9 #include <dirent.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 17 matching lines...) Expand all Loading... |
28 bool WriteFile(const char* path, const char* data, int data_len) { | 28 bool WriteFile(const char* path, const char* data, int data_len) { |
29 DirectFileWriter writer; | 29 DirectFileWriter writer; |
30 TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, | 30 TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path, |
31 O_WRONLY | O_CREAT | O_TRUNC, | 31 O_WRONLY | O_CREAT | O_TRUNC, |
32 0666)); | 32 0666)); |
33 ScopedFileWriterCloser closer(&writer); | 33 ScopedFileWriterCloser closer(&writer); |
34 TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len)); | 34 TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len)); |
35 return true; | 35 return true; |
36 } | 36 } |
37 | 37 |
38 bool WriteAll(int fd, const void *buf, size_t count) { | 38 bool WriteAll(int fd, const void* buf, size_t count) { |
39 const char* c_buf = static_cast<const char*>(buf); | 39 const char* c_buf = static_cast<const char*>(buf); |
40 ssize_t bytes_written = 0; | 40 ssize_t bytes_written = 0; |
41 while (bytes_written < static_cast<ssize_t>(count)) { | 41 while (bytes_written < static_cast<ssize_t>(count)) { |
42 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); | 42 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
43 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 43 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
44 bytes_written += rc; | 44 bytes_written += rc; |
45 } | 45 } |
46 return true; | 46 return true; |
47 } | 47 } |
48 | 48 |
| 49 bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 50 const char* c_buf = static_cast<const char*>(buf); |
| 51 ssize_t bytes_written = 0; |
| 52 while (bytes_written < static_cast<ssize_t>(count)) { |
| 53 ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written, |
| 54 offset + bytes_written); |
| 55 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 56 bytes_written += rc; |
| 57 } |
| 58 return true; |
| 59 } |
| 60 |
| 61 bool PReadAll(int fd, void* buf, size_t count, off_t offset, |
| 62 ssize_t* out_bytes_read) { |
| 63 char* c_buf = static_cast<char*>(buf); |
| 64 ssize_t bytes_read = 0; |
| 65 while (bytes_read < static_cast<ssize_t>(count)) { |
| 66 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 67 offset + bytes_read); |
| 68 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 69 if (rc == 0) { |
| 70 break; |
| 71 } |
| 72 bytes_read += rc; |
| 73 } |
| 74 *out_bytes_read = bytes_read; |
| 75 return true; |
| 76 |
| 77 } |
| 78 |
49 bool ReadFile(const std::string& path, std::vector<char>* out) { | 79 bool ReadFile(const std::string& path, std::vector<char>* out) { |
50 CHECK(out); | 80 CHECK(out); |
51 FILE* fp = fopen(path.c_str(), "r"); | 81 FILE* fp = fopen(path.c_str(), "r"); |
52 if (!fp) | 82 if (!fp) |
53 return false; | 83 return false; |
54 const size_t kChunkSize = 1024; | 84 const size_t kChunkSize = 1024; |
55 size_t read_size; | 85 size_t read_size; |
56 do { | 86 do { |
57 char buf[kChunkSize]; | 87 char buf[kChunkSize]; |
58 read_size = fread(buf, 1, kChunkSize, fp); | 88 read_size = fread(buf, 1, kChunkSize, fp); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 *filename = &buf[0]; | 256 *filename = &buf[0]; |
227 } | 257 } |
228 if (fd) { | 258 if (fd) { |
229 *fd = mkstemp_fd; | 259 *fd = mkstemp_fd; |
230 } else { | 260 } else { |
231 close(mkstemp_fd); | 261 close(mkstemp_fd); |
232 } | 262 } |
233 return true; | 263 return true; |
234 } | 264 } |
235 | 265 |
| 266 bool MakeTempDirectory(const std::string& dirname_template, |
| 267 std::string* dirname) { |
| 268 DCHECK(dirname); |
| 269 vector<char> buf(dirname_template.size() + 1); |
| 270 memcpy(&buf[0], dirname_template.data(), dirname_template.size()); |
| 271 buf[dirname_template.size()] = '\0'; |
| 272 |
| 273 char* return_code = mkdtemp(&buf[0]); |
| 274 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL); |
| 275 *dirname = &buf[0]; |
| 276 return true; |
| 277 } |
| 278 |
236 bool StringHasSuffix(const std::string& str, const std::string& suffix) { | 279 bool StringHasSuffix(const std::string& str, const std::string& suffix) { |
237 if (suffix.size() > str.size()) | 280 if (suffix.size() > str.size()) |
238 return false; | 281 return false; |
239 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); | 282 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); |
240 } | 283 } |
241 | 284 |
242 bool StringHasPrefix(const std::string& str, const std::string& prefix) { | 285 bool StringHasPrefix(const std::string& str, const std::string& prefix) { |
243 if (prefix.size() > str.size()) | 286 if (prefix.size() > str.size()) |
244 return false; | 287 return false; |
245 return 0 == str.compare(0, prefix.size(), prefix); | 288 return 0 == str.compare(0, prefix.size(), prefix); |
(...skipping 20 matching lines...) Expand all Loading... |
266 if (c == ' ') | 309 if (c == ' ') |
267 break; | 310 break; |
268 ret += c; | 311 ret += c; |
269 pos++; | 312 pos++; |
270 } | 313 } |
271 return ret; | 314 return ret; |
272 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems | 315 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems |
273 } | 316 } |
274 | 317 |
275 bool MountFilesystem(const string& device, | 318 bool MountFilesystem(const string& device, |
276 const string& mountpoint) { | 319 const string& mountpoint, |
277 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", 0, NULL); | 320 unsigned long mountflags) { |
| 321 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL); |
278 if (rc < 0) { | 322 if (rc < 0) { |
279 string msg = ErrnoNumberAsString(errno); | 323 string msg = ErrnoNumberAsString(errno); |
280 LOG(ERROR) << "Unable to mount destination device: " << msg << ". " | 324 LOG(ERROR) << "Unable to mount destination device: " << msg << ". " |
281 << device << " on " << mountpoint; | 325 << device << " on " << mountpoint; |
282 return false; | 326 return false; |
283 } | 327 } |
284 return true; | 328 return true; |
285 } | 329 } |
286 | 330 |
287 bool UnmountFilesystem(const string& mountpoint) { | 331 bool UnmountFilesystem(const string& mountpoint) { |
288 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0); | 332 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0); |
289 return true; | 333 return true; |
290 } | 334 } |
291 | 335 |
292 const char* const kStatefulPartition = "/mnt/stateful_partition"; | 336 const char* const kStatefulPartition = "/mnt/stateful_partition"; |
293 | 337 |
294 } // namespace utils | 338 } // namespace utils |
295 | 339 |
296 } // namespace chromeos_update_engine | 340 } // namespace chromeos_update_engine |
297 | 341 |
OLD | NEW |