diff options
author | nick@chromium.org <nick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-10 06:05:27 +0000 |
---|---|---|
committer | nick@chromium.org <nick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-10 06:05:27 +0000 |
commit | 5852edc1b6eab234b9e048c41dd0d664ae7fc747 (patch) | |
tree | 9e5d8eb4833b76cdb11e66fc3607689e0f5e0122 /chrome/browser/sync/util/path_helpers-posix.cc | |
parent | f6059e37f8b8ac335ce18a189a13e702974a1c7e (diff) | |
download | chromium_src-5852edc1b6eab234b9e048c41dd0d664ae7fc747.zip chromium_src-5852edc1b6eab234b9e048c41dd0d664ae7fc747.tar.gz chromium_src-5852edc1b6eab234b9e048c41dd0d664ae7fc747.tar.bz2 |
Initial commit of sync engine code to browser/sync.
The code is not built on any platform yet. That will arrive
as a subsequent checkin.
This is an implementation of the interface exposed earlier
through syncapi.h. It is the client side of a sync
protocol that lets users sync their browser data
(currently, just bookmarks) with their Google Account.
Table of contents:
browser/sync/
protocol - The protocol definition, and
other definitions necessary to connect to
the service.
syncable/ - defines a data model for syncable objects,
and provides a sqlite-based backing store
for this model.
engine/ - includes the core sync logic, including commiting
changes to the server, downloading changes from
the server, resolving conflicts, other parts of
the sync algorithm.
engine/net - parts of the sync engine focused on the
business of talking to the server. Some of
this is binds a generic "server connection"
interface to a concrete implementation
provided by Chromium.
notifier - the part of the syncer focused on the business
of sending and receiving xmpp notifications.
Notifications are used instead of polling to
achieve very low latency change propagation.
util - not necessarily sync specific utility code. Much
of this is scaffolding which should either be
replaced by, or merged with, the utility code
in base/.
BUG=none
TEST=this code includes its own suite of unit tests.
Review URL: http://codereview.chromium.org/194065
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25850 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/util/path_helpers-posix.cc')
-rw-r--r-- | chrome/browser/sync/util/path_helpers-posix.cc | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/chrome/browser/sync/util/path_helpers-posix.cc b/chrome/browser/sync/util/path_helpers-posix.cc new file mode 100644 index 0000000..02726db --- /dev/null +++ b/chrome/browser/sync/util/path_helpers-posix.cc @@ -0,0 +1,99 @@ +// Copyright (c) 2009 The Chromium 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 <pwd.h> + +#include <string.h> + +#include "base/port.h" +#include "chrome/browser/sync/util/path_helpers.h" +#include "strings/strutil.h" + +#if ((!defined(OS_LINUX)) && (!defined(OS_MACOSX))) +#error Compile this file on Mac OS X or Linux only. +#endif + +PathString ExpandTilde(const PathString& path) { + if (path.empty()) + return path; + if ('~' != path[0]) + return path; + PathString ret; + // TODO(sync): Consider using getpwuid_r. + ret.insert(0, getpwuid(getuid())->pw_dir); + ret.append(++path.begin(), path.end()); + return ret; +} + +namespace { +// TODO(sync): We really should use char[]. +string cache_dir_; +} + +void set_cache_dir(string cache_dir) { + CHECK(cache_dir_.empty()); + cache_dir_ = cache_dir; +} + +string get_cache_dir() { + CHECK(!cache_dir_.empty()); + return cache_dir_; +} + +// On Posix, PathStrings are UTF-8, not UTF-16 as they are on Windows. +// Thus, this function is different from the Windows version. +PathString TruncatePathString(const PathString& original, int length) { + if (original.size() <= length) + return original; + if (length <= 0) + return original; + PathString ret(original.begin(), original.begin() + length); + COMPILE_ASSERT(sizeof(PathChar) == sizeof(uint8), PathStringNotUTF8); + PathString::reverse_iterator last_char = ret.rbegin(); + + // Values taken from + // http://en.wikipedia.org/w/index.php?title=UTF-8&oldid=252875566 + if (0 == (*last_char & 0x80)) + return ret; + + for (; last_char != ret.rend(); ++last_char) { + if (0 == (*last_char & 0x80)) { + // got malformed UTF-8; bail + return ret; + } + if (0 == (*last_char & 0x40)) { + // got another trailing byte + continue; + } + break; + } + + if (ret.rend() == last_char) { + // We hit the beginning of the string. bail. + return ret; + } + + int last_codepoint_len = last_char - ret.rbegin() + 1; + + if (((0xC0 == (*last_char & 0xE0)) && (2 == last_codepoint_len)) || + ((0xE0 == (*last_char & 0xF0)) && (3 == last_codepoint_len)) || + ((0xF0 == (*last_char & 0xF8)) && (4 == last_codepoint_len))) { + // Valid utf-8. + return ret; + } + + // Invalid utf-8. chop off last "codepoint" and return. + ret.resize(ret.size() - last_codepoint_len); + return ret; +} + +// Convert /s to :s . +PathString MakePathComponentOSLegal(const PathString& component) { + if (PathString::npos == component.find("/")) + return PSTR(""); + PathString new_name; + new_name.reserve(component.size()); + StringReplace(component, "/", ":", true, &new_name); + return new_name; +} |