| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <pwd.h> | |
| 6 #include <string.h> | |
| 7 | |
| 8 #include "chrome/browser/sync/util/path_helpers.h" | |
| 9 | |
| 10 #if ((!defined(OS_LINUX)) && (!defined(OS_MACOSX))) | |
| 11 #error Compile this file on Mac OS X or Linux only. | |
| 12 #endif | |
| 13 | |
| 14 PathString ExpandTilde(const PathString& path) { | |
| 15 if (path.empty()) | |
| 16 return path; | |
| 17 if ('~' != path[0]) | |
| 18 return path; | |
| 19 PathString ret; | |
| 20 // TODO(sync): Consider using getpwuid_r. | |
| 21 ret.insert(0, getpwuid(getuid())->pw_dir); | |
| 22 ret.append(++path.begin(), path.end()); | |
| 23 return ret; | |
| 24 } | |
| 25 | |
| 26 namespace { | |
| 27 // TODO(sync): We really should use char[]. | |
| 28 string cache_dir_; | |
| 29 } | |
| 30 | |
| 31 void set_cache_dir(string cache_dir) { | |
| 32 CHECK(cache_dir_.empty()); | |
| 33 cache_dir_ = cache_dir; | |
| 34 } | |
| 35 | |
| 36 string get_cache_dir() { | |
| 37 CHECK(!cache_dir_.empty()); | |
| 38 return cache_dir_; | |
| 39 } | |
| 40 | |
| 41 // On Posix, PathStrings are UTF-8, not UTF-16 as they are on Windows. Thus, | |
| 42 // this function is different from the Windows version. | |
| 43 PathString TruncatePathString(const PathString& original, int length) { | |
| 44 if (original.size() <= length) | |
| 45 return original; | |
| 46 if (length <= 0) | |
| 47 return original; | |
| 48 PathString ret(original.begin(), original.begin() + length); | |
| 49 COMPILE_ASSERT(sizeof(PathChar) == sizeof(uint8), PathStringNotUTF8); | |
| 50 PathString::reverse_iterator last_char = ret.rbegin(); | |
| 51 | |
| 52 // Values taken from | |
| 53 // http://en.wikipedia.org/w/index.php?title=UTF-8&oldid=252875566 | |
| 54 if (0 == (*last_char & 0x80)) | |
| 55 return ret; | |
| 56 | |
| 57 for (; last_char != ret.rend(); ++last_char) { | |
| 58 if (0 == (*last_char & 0x80)) { | |
| 59 // Got malformed UTF-8; bail. | |
| 60 return ret; | |
| 61 } | |
| 62 if (0 == (*last_char & 0x40)) { | |
| 63 // Got another trailing byte. | |
| 64 continue; | |
| 65 } | |
| 66 break; | |
| 67 } | |
| 68 | |
| 69 if (ret.rend() == last_char) { | |
| 70 // We hit the beginning of the string. bail. | |
| 71 return ret; | |
| 72 } | |
| 73 | |
| 74 int last_codepoint_len = last_char - ret.rbegin() + 1; | |
| 75 | |
| 76 if (((0xC0 == (*last_char & 0xE0)) && (2 == last_codepoint_len)) || | |
| 77 ((0xE0 == (*last_char & 0xF0)) && (3 == last_codepoint_len)) || | |
| 78 ((0xF0 == (*last_char & 0xF8)) && (4 == last_codepoint_len))) { | |
| 79 // Valid utf-8. | |
| 80 return ret; | |
| 81 } | |
| 82 | |
| 83 // Invalid utf-8. chop off last "codepoint" and return. | |
| 84 ret.resize(ret.size() - last_codepoint_len); | |
| 85 return ret; | |
| 86 } | |
| 87 | |
| 88 // Convert /s to :s. | |
| 89 PathString MakePathComponentOSLegal(const PathString& component) { | |
| 90 if (PathString::npos == component.find("/")) | |
| 91 return PSTR(""); | |
| 92 PathString new_name; | |
| 93 new_name.reserve(component.size()); | |
| 94 StringReplace(component, "/", ":", true, &new_name); | |
| 95 return new_name; | |
| 96 } | |
| OLD | NEW |