Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: src/platform/update_engine/utils.cc

Issue 891002: AU: Delta Diff Generator (Closed)
Patch Set: fixes for review Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/platform/update_engine/utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
39 const char* c_buf = static_cast<const char*>(buf);
40 ssize_t bytes_written = 0;
41 while (bytes_written < static_cast<ssize_t>(count)) {
42 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written);
43 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
44 bytes_written += rc;
45 }
46 return true;
47 }
48
38 bool ReadFile(const std::string& path, std::vector<char>* out) { 49 bool ReadFile(const std::string& path, std::vector<char>* out) {
39 CHECK(out); 50 CHECK(out);
40 FILE* fp = fopen(path.c_str(), "r"); 51 FILE* fp = fopen(path.c_str(), "r");
41 if (!fp) 52 if (!fp)
42 return false; 53 return false;
43 const size_t kChunkSize = 1024; 54 const size_t kChunkSize = 1024;
44 size_t read_size; 55 size_t read_size;
45 do { 56 do {
46 char buf[kChunkSize]; 57 char buf[kChunkSize];
47 read_size = fread(buf, 1, kChunkSize, fp); 58 read_size = fread(buf, 1, kChunkSize, fp);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 118
108 bool RecursiveUnlinkDir(const std::string& path) { 119 bool RecursiveUnlinkDir(const std::string& path) {
109 struct stat stbuf; 120 struct stat stbuf;
110 int r = lstat(path.c_str(), &stbuf); 121 int r = lstat(path.c_str(), &stbuf);
111 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT)); 122 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
112 if ((r < 0) && (errno == ENOENT)) 123 if ((r < 0) && (errno == ENOENT))
113 // path request is missing. that's fine. 124 // path request is missing. that's fine.
114 return true; 125 return true;
115 if (!S_ISDIR(stbuf.st_mode)) { 126 if (!S_ISDIR(stbuf.st_mode)) {
116 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) || 127 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
117 (errno == ENOENT)); 128 (errno == ENOENT));
118 // success or path disappeared before we could unlink. 129 // success or path disappeared before we could unlink.
119 return true; 130 return true;
120 } 131 }
121 { 132 {
122 // We have a dir, unlink all children, then delete dir 133 // We have a dir, unlink all children, then delete dir
123 DIR *dir = opendir(path.c_str()); 134 DIR *dir = opendir(path.c_str());
124 TEST_AND_RETURN_FALSE_ERRNO(dir); 135 TEST_AND_RETURN_FALSE_ERRNO(dir);
125 ScopedDirCloser dir_closer(&dir); 136 ScopedDirCloser dir_closer(&dir);
126 struct dirent dir_entry; 137 struct dirent dir_entry;
127 struct dirent *dir_entry_p; 138 struct dirent *dir_entry_p;
128 int err = 0; 139 int err = 0;
129 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) { 140 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
130 if (dir_entry_p == NULL) { 141 if (dir_entry_p == NULL) {
131 // end of stream reached 142 // end of stream reached
132 break; 143 break;
133 } 144 }
134 // Skip . and .. 145 // Skip . and ..
135 if (!strcmp(dir_entry_p->d_name, ".") || 146 if (!strcmp(dir_entry_p->d_name, ".") ||
136 !strcmp(dir_entry_p->d_name, "..")) 147 !strcmp(dir_entry_p->d_name, ".."))
137 continue; 148 continue;
138 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" + 149 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
139 dir_entry_p->d_name)); 150 dir_entry_p->d_name));
140 } 151 }
141 TEST_AND_RETURN_FALSE(err == 0); 152 TEST_AND_RETURN_FALSE(err == 0);
142 } 153 }
143 // unlink dir 154 // unlink dir
144 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT)); 155 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
145 return true; 156 return true;
146 } 157 }
147 158
148 std::string ErrnoNumberAsString(int err) { 159 std::string ErrnoNumberAsString(int err) {
149 char buf[100]; 160 char buf[100];
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 else 205 else
195 new_suffix.append(1, '0' + r - (26 * 2)); 206 new_suffix.append(1, '0' + r - (26 * 2));
196 } 207 }
197 CHECK_EQ(new_suffix.size(), suffix.size()); 208 CHECK_EQ(new_suffix.size(), suffix.size());
198 path.resize(path.size() - new_suffix.size()); 209 path.resize(path.size() - new_suffix.size());
199 path.append(new_suffix); 210 path.append(new_suffix);
200 } while (FileExists(path.c_str())); 211 } while (FileExists(path.c_str()));
201 return path; 212 return path;
202 } 213 }
203 214
215 bool MakeTempFile(const std::string& filename_template,
216 std::string* filename,
217 int* fd) {
218 DCHECK(filename || fd);
219 vector<char> buf(filename_template.size() + 1);
220 memcpy(&buf[0], filename_template.data(), filename_template.size());
221 buf[filename_template.size()] = '\0';
222
223 int mkstemp_fd = mkstemp(&buf[0]);
224 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
225 if (filename) {
226 *filename = &buf[0];
227 }
228 if (fd) {
229 *fd = mkstemp_fd;
230 } else {
231 close(mkstemp_fd);
232 }
233 return true;
234 }
235
204 bool StringHasSuffix(const std::string& str, const std::string& suffix) { 236 bool StringHasSuffix(const std::string& str, const std::string& suffix) {
205 if (suffix.size() > str.size()) 237 if (suffix.size() > str.size())
206 return false; 238 return false;
207 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); 239 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
208 } 240 }
209 241
210 bool StringHasPrefix(const std::string& str, const std::string& prefix) { 242 bool StringHasPrefix(const std::string& str, const std::string& prefix) {
211 if (prefix.size() > str.size()) 243 if (prefix.size() > str.size())
212 return false; 244 return false;
213 return 0 == str.compare(0, prefix.size(), prefix); 245 return 0 == str.compare(0, prefix.size(), prefix);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0); 288 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
257 return true; 289 return true;
258 } 290 }
259 291
260 const char* const kStatefulPartition = "/mnt/stateful_partition"; 292 const char* const kStatefulPartition = "/mnt/stateful_partition";
261 293
262 } // namespace utils 294 } // namespace utils
263 295
264 } // namespace chromeos_update_engine 296 } // namespace chromeos_update_engine
265 297
OLDNEW
« no previous file with comments | « src/platform/update_engine/utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698