| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 |
| 6 #include <sys/mount.h> | 7 #include <sys/mount.h> |
| 7 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 8 #include <sys/types.h> | 9 #include <sys/types.h> |
| 9 #include <dirent.h> | 10 #include <dirent.h> |
| 10 #include <errno.h> | 11 #include <errno.h> |
| 11 #include <fcntl.h> | 12 #include <fcntl.h> |
| 12 #include <stdio.h> | 13 #include <stdio.h> |
| 13 #include <stdlib.h> | 14 #include <stdlib.h> |
| 14 #include <string.h> | 15 #include <string.h> |
| 15 #include <unistd.h> | 16 #include <unistd.h> |
| 17 |
| 16 #include <algorithm> | 18 #include <algorithm> |
| 19 |
| 20 #include "base/file_path.h" |
| 21 #include "base/file_util.h" |
| 22 #include "base/string_util.h" |
| 17 #include "chromeos/obsolete_logging.h" | 23 #include "chromeos/obsolete_logging.h" |
| 18 #include "update_engine/file_writer.h" | 24 #include "update_engine/file_writer.h" |
| 19 #include "update_engine/omaha_request_params.h" | 25 #include "update_engine/omaha_request_params.h" |
| 20 #include "update_engine/subprocess.h" | 26 #include "update_engine/subprocess.h" |
| 21 | 27 |
| 22 using std::min; | 28 using std::min; |
| 23 using std::string; | 29 using std::string; |
| 24 using std::vector; | 30 using std::vector; |
| 25 | 31 |
| 26 namespace chromeos_update_engine { | 32 namespace chromeos_update_engine { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 dir_entry_p->d_name)); | 206 dir_entry_p->d_name)); |
| 201 } | 207 } |
| 202 TEST_AND_RETURN_FALSE(err == 0); | 208 TEST_AND_RETURN_FALSE(err == 0); |
| 203 } | 209 } |
| 204 // unlink dir | 210 // unlink dir |
| 205 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT)); | 211 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT)); |
| 206 return true; | 212 return true; |
| 207 } | 213 } |
| 208 | 214 |
| 209 string RootDevice(const string& partition_device) { | 215 string RootDevice(const string& partition_device) { |
| 210 CHECK(!partition_device.empty()); | 216 FilePath device_path(partition_device); |
| 217 if (device_path.DirName().value() != "/dev") { |
| 218 return ""; |
| 219 } |
| 211 string::const_iterator it = --partition_device.end(); | 220 string::const_iterator it = --partition_device.end(); |
| 212 for (; it >= partition_device.begin(); --it) { | 221 for (; it >= partition_device.begin(); --it) { |
| 213 if (!isdigit(*it)) | 222 if (!isdigit(*it)) |
| 214 break; | 223 break; |
| 215 } | 224 } |
| 216 // Some devices contain a p before the partitions. For example: | 225 // Some devices contain a p before the partitions. For example: |
| 217 // /dev/mmc0p4 should be shortened to /dev/mmc0. | 226 // /dev/mmc0p4 should be shortened to /dev/mmc0. |
| 218 if (*it == 'p') | 227 if (*it == 'p') |
| 219 --it; | 228 --it; |
| 220 return string(partition_device.begin(), it + 1); | 229 return string(partition_device.begin(), it + 1); |
| 221 } | 230 } |
| 222 | 231 |
| 223 string PartitionNumber(const string& partition_device) { | 232 string PartitionNumber(const string& partition_device) { |
| 224 CHECK(!partition_device.empty()); | 233 CHECK(!partition_device.empty()); |
| 225 string::const_iterator it = --partition_device.end(); | 234 string::const_iterator it = --partition_device.end(); |
| 226 for (; it >= partition_device.begin(); --it) { | 235 for (; it >= partition_device.begin(); --it) { |
| 227 if (!isdigit(*it)) | 236 if (!isdigit(*it)) |
| 228 break; | 237 break; |
| 229 } | 238 } |
| 230 return string(it + 1, partition_device.end()); | 239 return string(it + 1, partition_device.end()); |
| 231 } | 240 } |
| 232 | 241 |
| 242 string SysfsBlockDevice(const string& device) { |
| 243 FilePath device_path(device); |
| 244 if (device_path.DirName().value() != "/dev") { |
| 245 return ""; |
| 246 } |
| 247 return FilePath("/sys/block").Append(device_path.BaseName()).value(); |
| 248 } |
| 249 |
| 250 bool IsRemovableDevice(const std::string& device) { |
| 251 string sysfs_block = SysfsBlockDevice(device); |
| 252 string removable; |
| 253 if (sysfs_block.empty() || |
| 254 !file_util::ReadFileToString(FilePath(sysfs_block).Append("removable"), |
| 255 &removable)) { |
| 256 return false; |
| 257 } |
| 258 TrimWhitespaceASCII(removable, TRIM_ALL, &removable); |
| 259 return removable == "1"; |
| 260 } |
| 261 |
| 233 std::string ErrnoNumberAsString(int err) { | 262 std::string ErrnoNumberAsString(int err) { |
| 234 char buf[100]; | 263 char buf[100]; |
| 235 buf[0] = '\0'; | 264 buf[0] = '\0'; |
| 236 return strerror_r(err, buf, sizeof(buf)); | 265 return strerror_r(err, buf, sizeof(buf)); |
| 237 } | 266 } |
| 238 | 267 |
| 239 std::string NormalizePath(const std::string& path, bool strip_trailing_slash) { | 268 std::string NormalizePath(const std::string& path, bool strip_trailing_slash) { |
| 240 string ret; | 269 string ret; |
| 241 bool last_insert_was_slash = false; | 270 bool last_insert_was_slash = false; |
| 242 for (string::const_iterator it = path.begin(); it != path.end(); ++it) { | 271 for (string::const_iterator it = path.begin(); it != path.end(); ++it) { |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 Subprocess::SynchronousExec(command, &rc); | 442 Subprocess::SynchronousExec(command, &rc); |
| 414 TEST_AND_RETURN_FALSE(rc == 0); | 443 TEST_AND_RETURN_FALSE(rc == 0); |
| 415 return true; | 444 return true; |
| 416 } | 445 } |
| 417 | 446 |
| 418 const char* const kStatefulPartition = "/mnt/stateful_partition"; | 447 const char* const kStatefulPartition = "/mnt/stateful_partition"; |
| 419 | 448 |
| 420 } // namespace utils | 449 } // namespace utils |
| 421 | 450 |
| 422 } // namespace chromeos_update_engine | 451 } // namespace chromeos_update_engine |
| OLD | NEW |