This source file includes following definitions.
- RemovePrefix
- ParseV0FormatFileSystemURL
- AddWapiFilePrefix
- AddWapiFolderPrefix
- AddWapiIdPrefix
- RemoveWapiIdPrefix
- MigrateDatabaseFromV0ToV1
- MigrateDatabaseFromV1ToV2
#include "chrome/browser/sync_file_system/drive_backend/metadata_db_migration_util.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
#include "url/gurl.h"
#include "webkit/common/fileapi/file_system_types.h"
#include "webkit/common/fileapi/file_system_util.h"
namespace sync_file_system {
namespace drive_backend {
namespace {
const base::FilePath::CharType kV0FormatPathPrefix[] =
FILE_PATH_LITERAL("drive/");
const char kWapiFileIdPrefix[] = "file:";
const char kWapiFolderIdPrefix[] = "folder:";
std::string RemovePrefix(const std::string& str, const std::string& prefix) {
if (StartsWithASCII(str, prefix, true))
return std::string(str.begin() + prefix.size(), str.end());
return str;
}
}
bool ParseV0FormatFileSystemURL(const GURL& url,
GURL* origin,
base::FilePath* path) {
fileapi::FileSystemType mount_type;
base::FilePath virtual_path;
if (!fileapi::ParseFileSystemSchemeURL(
url, origin, &mount_type, &virtual_path) ||
mount_type != fileapi::kFileSystemTypeExternal) {
NOTREACHED() << "Failed to parse filesystem scheme URL " << url.spec();
return false;
}
base::FilePath::StringType prefix =
base::FilePath(kV0FormatPathPrefix).NormalizePathSeparators().value();
if (virtual_path.value().substr(0, prefix.size()) != prefix)
return false;
*path = base::FilePath(virtual_path.value().substr(prefix.size()));
return true;
}
std::string AddWapiFilePrefix(const std::string& resource_id) {
DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true));
DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true));
if (resource_id.empty() ||
StartsWithASCII(resource_id, kWapiFileIdPrefix, true) ||
StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
return resource_id;
return kWapiFileIdPrefix + resource_id;
}
std::string AddWapiFolderPrefix(const std::string& resource_id) {
DCHECK(!StartsWithASCII(resource_id, kWapiFileIdPrefix, true));
DCHECK(!StartsWithASCII(resource_id, kWapiFolderIdPrefix, true));
if (resource_id.empty() ||
StartsWithASCII(resource_id, kWapiFileIdPrefix, true) ||
StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
return resource_id;
return kWapiFolderIdPrefix + resource_id;
}
std::string AddWapiIdPrefix(const std::string& resource_id,
DriveMetadata_ResourceType type) {
switch (type) {
case DriveMetadata_ResourceType_RESOURCE_TYPE_FILE:
return AddWapiFilePrefix(resource_id);
case DriveMetadata_ResourceType_RESOURCE_TYPE_FOLDER:
return AddWapiFolderPrefix(resource_id);
}
NOTREACHED();
return resource_id;
}
std::string RemoveWapiIdPrefix(const std::string& resource_id) {
if (StartsWithASCII(resource_id, kWapiFileIdPrefix, true))
return RemovePrefix(resource_id, kWapiFileIdPrefix);
if (StartsWithASCII(resource_id, kWapiFolderIdPrefix, true))
return RemovePrefix(resource_id, kWapiFolderIdPrefix);
return resource_id;
}
SyncStatusCode MigrateDatabaseFromV0ToV1(leveldb::DB* db) {
const char kDatabaseVersionKey[] = "VERSION";
const char kDriveMetadataKeyPrefix[] = "METADATA: ";
const char kMetadataKeySeparator = ' ';
leveldb::WriteBatch write_batch;
write_batch.Put(kDatabaseVersionKey, "1");
scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions()));
for (itr->Seek(kDriveMetadataKeyPrefix); itr->Valid(); itr->Next()) {
std::string key = itr->key().ToString();
if (!StartsWithASCII(key, kDriveMetadataKeyPrefix, true))
break;
std::string serialized_url(RemovePrefix(key, kDriveMetadataKeyPrefix));
GURL origin;
base::FilePath path;
bool success = ParseV0FormatFileSystemURL(
GURL(serialized_url), &origin, &path);
DCHECK(success) << serialized_url;
std::string new_key = kDriveMetadataKeyPrefix + origin.spec() +
kMetadataKeySeparator + path.AsUTF8Unsafe();
write_batch.Put(new_key, itr->value());
write_batch.Delete(key);
}
return LevelDBStatusToSyncStatusCode(
db->Write(leveldb::WriteOptions(), &write_batch));
}
SyncStatusCode MigrateDatabaseFromV1ToV2(leveldb::DB* db) {
const char kDatabaseVersionKey[] = "VERSION";
const char kSyncRootDirectoryKey[] = "SYNC_ROOT_DIR";
const char kDriveMetadataKeyPrefix[] = "METADATA: ";
const char kDriveBatchSyncOriginKeyPrefix[] = "BSYNC_ORIGIN: ";
const char kDriveIncrementalSyncOriginKeyPrefix[] = "ISYNC_ORIGIN: ";
const char kDriveDisabledOriginKeyPrefix[] = "DISABLED_ORIGIN: ";
leveldb::WriteBatch write_batch;
write_batch.Put(kDatabaseVersionKey, "2");
scoped_ptr<leveldb::Iterator> itr(db->NewIterator(leveldb::ReadOptions()));
for (itr->SeekToFirst(); itr->Valid(); itr->Next()) {
std::string key = itr->key().ToString();
if (StartsWithASCII(key, kSyncRootDirectoryKey, true)) {
write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString()));
continue;
}
if (StartsWithASCII(key, kDriveMetadataKeyPrefix, true)) {
DriveMetadata metadata;
bool success = metadata.ParseFromString(itr->value().ToString());
DCHECK(success);
metadata.set_resource_id(RemoveWapiIdPrefix(metadata.resource_id()));
std::string metadata_string;
metadata.SerializeToString(&metadata_string);
write_batch.Put(key, metadata_string);
continue;
}
if (StartsWithASCII(key, kDriveBatchSyncOriginKeyPrefix, true)) {
write_batch.Delete(key);
continue;
}
if (StartsWithASCII(key, kDriveIncrementalSyncOriginKeyPrefix, true)) {
write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString()));
continue;
}
if (StartsWithASCII(key, kDriveDisabledOriginKeyPrefix, true)) {
write_batch.Put(key, RemoveWapiIdPrefix(itr->value().ToString()));
continue;
}
}
return LevelDBStatusToSyncStatusCode(
db->Write(leveldb::WriteOptions(), &write_batch));
}
}
}