Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / sync / engine / syncer_util.cc
blobf8f873959954acbe404bc340536b4417986319e3
1 // Copyright 2012 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.
5 #include "sync/engine/syncer_util.h"
7 #include <algorithm>
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/base64.h"
13 #include "base/location.h"
14 #include "base/metrics/histogram.h"
15 #include "base/rand_util.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "sync/engine/conflict_resolver.h"
18 #include "sync/engine/syncer_proto_util.h"
19 #include "sync/engine/syncer_types.h"
20 #include "sync/internal_api/public/base/attachment_id_proto.h"
21 #include "sync/internal_api/public/base/model_type.h"
22 #include "sync/internal_api/public/base/unique_position.h"
23 #include "sync/protocol/bookmark_specifics.pb.h"
24 #include "sync/protocol/password_specifics.pb.h"
25 #include "sync/protocol/sync.pb.h"
26 #include "sync/syncable/directory.h"
27 #include "sync/syncable/entry.h"
28 #include "sync/syncable/model_neutral_mutable_entry.h"
29 #include "sync/syncable/mutable_entry.h"
30 #include "sync/syncable/syncable_changes_version.h"
31 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
32 #include "sync/syncable/syncable_proto_util.h"
33 #include "sync/syncable/syncable_read_transaction.h"
34 #include "sync/syncable/syncable_util.h"
35 #include "sync/syncable/syncable_write_transaction.h"
36 #include "sync/util/cryptographer.h"
37 #include "sync/util/time.h"
39 namespace syncer {
41 using syncable::BASE_SERVER_SPECIFICS;
42 using syncable::BASE_VERSION;
43 using syncable::CHANGES_VERSION;
44 using syncable::CREATE_NEW_UPDATE_ITEM;
45 using syncable::CTIME;
46 using syncable::Directory;
47 using syncable::Entry;
48 using syncable::GET_BY_HANDLE;
49 using syncable::GET_BY_ID;
50 using syncable::ID;
51 using syncable::IS_DEL;
52 using syncable::IS_DIR;
53 using syncable::IS_UNAPPLIED_UPDATE;
54 using syncable::IS_UNSYNCED;
55 using syncable::Id;
56 using syncable::META_HANDLE;
57 using syncable::MTIME;
58 using syncable::MutableEntry;
59 using syncable::NON_UNIQUE_NAME;
60 using syncable::PARENT_ID;
61 using syncable::SERVER_CTIME;
62 using syncable::SERVER_IS_DEL;
63 using syncable::SERVER_IS_DIR;
64 using syncable::SERVER_MTIME;
65 using syncable::SERVER_NON_UNIQUE_NAME;
66 using syncable::SERVER_PARENT_ID;
67 using syncable::SERVER_SPECIFICS;
68 using syncable::SERVER_UNIQUE_POSITION;
69 using syncable::SERVER_VERSION;
70 using syncable::SPECIFICS;
71 using syncable::SYNCER;
72 using syncable::UNIQUE_BOOKMARK_TAG;
73 using syncable::UNIQUE_CLIENT_TAG;
74 using syncable::UNIQUE_POSITION;
75 using syncable::UNIQUE_SERVER_TAG;
76 using syncable::WriteTransaction;
78 // TODO (stanisc): crbug.com/362467: remove this function once
79 // issue 362467 is fixed.
80 // Validates that the local ID picked by FindLocalIdToUpdate doesn't
81 // conflict with already existing item with update_id.
82 void VerifyLocalIdToUpdate(syncable::BaseTransaction* trans,
83 const syncable::Id& local_id,
84 const syncable::Id& update_id,
85 bool local_deleted,
86 bool deleted_in_update) {
87 if (local_id == update_id) {
88 // ID matches, everything is good.
89 return;
92 // If the ID doesn't match, it means that an entry with |local_id| has been
93 // picked and an entry with |update_id| isn't supposed to exist.
94 syncable::Entry update_entry(trans, GET_BY_ID, update_id);
95 if (!update_entry.good())
96 return;
98 // Fail early so that the crash dump indicates which of the cases below
99 // has triggered the issue.
100 // Crash dumps don't always preserve data. The 2 separate cases below are
101 // to make it easy to see the the state of item with |update_id| in the
102 // crash dump.
103 if (update_entry.GetIsDel()) {
104 LOG(FATAL) << "VerifyLocalIdToUpdate: existing deleted entry " << update_id
105 << " conflicts with local entry " << local_id
106 << " picked by an update.\n"
107 << "Local item deleted: " << local_deleted
108 << ", deleted flag in update: " << deleted_in_update;
109 } else {
110 LOG(FATAL) << "VerifyLocalIdToUpdate: existing entry " << update_id
111 << " conflicts with local entry " << local_id
112 << " picked by an update.\n"
113 << "Local item deleted: " << local_deleted
114 << ", deleted flag in update: " << deleted_in_update;
118 syncable::Id FindLocalIdToUpdate(
119 syncable::BaseTransaction* trans,
120 const sync_pb::SyncEntity& update) {
121 // Expected entry points of this function:
122 // SyncEntity has NOT been applied to SERVER fields.
123 // SyncEntity has NOT been applied to LOCAL fields.
124 // DB has not yet been modified, no entries created for this update.
126 const std::string& client_id = trans->directory()->cache_guid();
127 const syncable::Id& update_id = SyncableIdFromProto(update.id_string());
129 if (update.has_client_defined_unique_tag() &&
130 !update.client_defined_unique_tag().empty()) {
131 // When a server sends down a client tag, the following cases can occur:
132 // 1) Client has entry for tag already, ID is server style, matches
133 // 2) Client has entry for tag already, ID is server, doesn't match.
134 // 3) Client has entry for tag already, ID is local, (never matches)
135 // 4) Client has no entry for tag
137 // Case 1, we don't have to do anything since the update will
138 // work just fine. Update will end up in the proper entry, via ID lookup.
139 // Case 2 - Happens very rarely due to lax enforcement of client tags
140 // on the server, if two clients commit the same tag at the same time.
141 // When this happens, we pick the lexically-least ID and ignore all other
142 // items.
143 // Case 3 - We need to replace the local ID with the server ID so that
144 // this update gets targeted at the correct local entry; we expect conflict
145 // resolution to occur.
146 // Case 4 - Perfect. Same as case 1.
148 syncable::Entry local_entry(trans, syncable::GET_BY_CLIENT_TAG,
149 update.client_defined_unique_tag());
151 // The SyncAPI equivalent of this function will return !good if IS_DEL.
152 // The syncable version will return good even if IS_DEL.
153 // TODO(chron): Unit test the case with IS_DEL and make sure.
154 if (local_entry.good()) {
155 if (local_entry.GetId().ServerKnows()) {
156 if (local_entry.GetId() != update_id) {
157 // Case 2.
158 LOG(WARNING) << "Duplicated client tag.";
159 if (local_entry.GetId() < update_id) {
160 // Signal an error; drop this update on the floor. Note that
161 // we don't server delete the item, because we don't allow it to
162 // exist locally at all. So the item will remain orphaned on
163 // the server, and we won't pay attention to it.
164 return syncable::Id();
167 // Target this change to the existing local entry; later,
168 // we'll change the ID of the local entry to update_id
169 // if needed.
170 VerifyLocalIdToUpdate(trans, local_entry.GetId(), update_id,
171 local_entry.GetIsDel(), update.deleted());
172 return local_entry.GetId();
173 } else {
174 // Case 3: We have a local entry with the same client tag.
175 // We should change the ID of the local entry to the server entry.
176 // This will result in an server ID with base version == 0, but that's
177 // a legal state for an item with a client tag. By changing the ID,
178 // update will now be applied to local_entry.
179 DCHECK(0 == local_entry.GetBaseVersion() ||
180 CHANGES_VERSION == local_entry.GetBaseVersion());
181 VerifyLocalIdToUpdate(trans, local_entry.GetId(), update_id,
182 local_entry.GetIsDel(), update.deleted());
183 return local_entry.GetId();
186 } else if (update.has_originator_cache_guid() &&
187 update.originator_cache_guid() == client_id) {
188 // If a commit succeeds, but the response does not come back fast enough
189 // then the syncer might assume that it was never committed.
190 // The server will track the client that sent up the original commit and
191 // return this in a get updates response. When this matches a local
192 // uncommitted item, we must mutate our local item and version to pick up
193 // the committed version of the same item whose commit response was lost.
194 // There is however still a race condition if the server has not
195 // completed the commit by the time the syncer tries to get updates
196 // again. To mitigate this, we need to have the server time out in
197 // a reasonable span, our commit batches have to be small enough
198 // to process within our HTTP response "assumed alive" time.
200 // We need to check if we have an entry that didn't get its server
201 // id updated correctly. The server sends down a client ID
202 // and a local (negative) id. If we have a entry by that
203 // description, we should update the ID and version to the
204 // server side ones to avoid multiple copies of the same thing.
206 syncable::Id client_item_id = syncable::Id::CreateFromClientString(
207 update.originator_client_item_id());
208 DCHECK(!client_item_id.ServerKnows());
209 syncable::Entry local_entry(trans, GET_BY_ID, client_item_id);
211 // If it exists, then our local client lost a commit response. Use
212 // the local entry.
213 if (local_entry.good() && !local_entry.GetIsDel()) {
214 int64 old_version = local_entry.GetBaseVersion();
215 int64 new_version = update.version();
216 DCHECK_LE(old_version, 0);
217 DCHECK_GT(new_version, 0);
218 // Otherwise setting the base version could cause a consistency failure.
219 // An entry should never be version 0 and SYNCED.
220 DCHECK(local_entry.GetIsUnsynced());
222 // Just a quick sanity check.
223 DCHECK(!local_entry.GetId().ServerKnows());
225 DVLOG(1) << "Reuniting lost commit response IDs. server id: "
226 << update_id << " local id: " << local_entry.GetId()
227 << " new version: " << new_version;
229 VerifyLocalIdToUpdate(trans, local_entry.GetId(), update_id,
230 local_entry.GetIsDel(), update.deleted());
231 return local_entry.GetId();
233 } else if (update.has_server_defined_unique_tag() &&
234 !update.server_defined_unique_tag().empty()) {
235 // The client creates type root folders with a local ID on demand when a
236 // progress marker for the given type is initially set.
237 // The server might also attempt to send a type root folder for the same
238 // type (during the transition period until support for root folders is
239 // removed for new client versions).
240 // TODO(stanisc): crbug.com/438313: remove this once the transition to
241 // implicit root folders on the server is done.
242 syncable::Entry local_entry(trans, syncable::GET_BY_SERVER_TAG,
243 update.server_defined_unique_tag());
244 if (local_entry.good() && !local_entry.GetId().ServerKnows()) {
245 DCHECK(local_entry.GetId() != update_id);
246 VerifyLocalIdToUpdate(trans, local_entry.GetId(), update_id,
247 local_entry.GetIsDel(), update.deleted());
248 return local_entry.GetId();
252 // Fallback: target an entry having the server ID, creating one if needed.
253 return update_id;
256 UpdateAttemptResponse AttemptToUpdateEntry(
257 syncable::WriteTransaction* const trans,
258 syncable::MutableEntry* const entry,
259 Cryptographer* cryptographer) {
260 CHECK(entry->good());
261 if (!entry->GetIsUnappliedUpdate())
262 return SUCCESS; // No work to do.
263 syncable::Id id = entry->GetId();
264 const sync_pb::EntitySpecifics& specifics = entry->GetServerSpecifics();
265 ModelType type = GetModelTypeFromSpecifics(specifics);
267 // Only apply updates that we can decrypt. If we can't decrypt the update, it
268 // is likely because the passphrase has not arrived yet. Because the
269 // passphrase may not arrive within this GetUpdates, we can't just return
270 // conflict, else we try to perform normal conflict resolution prematurely or
271 // the syncer may get stuck. As such, we return CONFLICT_ENCRYPTION, which is
272 // treated as an unresolvable conflict. See the description in syncer_types.h.
273 // This prevents any unsynced changes from commiting and postpones conflict
274 // resolution until all data can be decrypted.
275 if (specifics.has_encrypted() &&
276 !cryptographer->CanDecrypt(specifics.encrypted())) {
277 // We can't decrypt this node yet.
278 DVLOG(1) << "Received an undecryptable "
279 << ModelTypeToString(entry->GetServerModelType())
280 << " update, returning conflict_encryption.";
281 return CONFLICT_ENCRYPTION;
282 } else if (specifics.has_password() &&
283 entry->GetUniqueServerTag().empty()) {
284 // Passwords use their own legacy encryption scheme.
285 const sync_pb::PasswordSpecifics& password = specifics.password();
286 if (!cryptographer->CanDecrypt(password.encrypted())) {
287 DVLOG(1) << "Received an undecryptable password update, returning "
288 << "conflict_encryption.";
289 return CONFLICT_ENCRYPTION;
293 if (!entry->GetServerIsDel()) {
294 syncable::Id new_parent = entry->GetServerParentId();
295 if (!new_parent.IsNull()) {
296 // Perform this step only if the parent is specified.
297 // The unset parent means that the implicit type root would be used.
298 Entry parent(trans, GET_BY_ID, new_parent);
299 // A note on non-directory parents:
300 // We catch most unfixable tree invariant errors at update receipt time,
301 // however we deal with this case here because we may receive the child
302 // first then the illegal parent. Instead of dealing with it twice in
303 // different ways we deal with it once here to reduce the amount of code
304 // and potential errors.
305 if (!parent.good() || parent.GetIsDel() || !parent.GetIsDir()) {
306 DVLOG(1) << "Entry has bad parent, returning conflict_hierarchy.";
307 return CONFLICT_HIERARCHY;
309 if (entry->GetParentId() != new_parent) {
310 if (!entry->GetIsDel() && !IsLegalNewParent(trans, id, new_parent)) {
311 DVLOG(1) << "Not updating item " << id
312 << ", illegal new parent (would cause loop).";
313 return CONFLICT_HIERARCHY;
316 } else {
317 // new_parent is unset.
318 DCHECK(IsTypeWithClientGeneratedRoot(type));
320 } else if (entry->GetIsDir()) {
321 Directory::Metahandles handles;
322 trans->directory()->GetChildHandlesById(trans, id, &handles);
323 if (!handles.empty()) {
324 // If we have still-existing children, then we need to deal with
325 // them before we can process this change.
326 DVLOG(1) << "Not deleting directory; it's not empty " << *entry;
327 return CONFLICT_HIERARCHY;
331 if (entry->GetIsUnsynced()) {
332 DVLOG(1) << "Skipping update, returning conflict for: " << id
333 << " ; it's unsynced.";
334 return CONFLICT_SIMPLE;
337 if (specifics.has_encrypted()) {
338 DVLOG(2) << "Received a decryptable "
339 << ModelTypeToString(entry->GetServerModelType())
340 << " update, applying normally.";
341 } else {
342 DVLOG(2) << "Received an unencrypted "
343 << ModelTypeToString(entry->GetServerModelType())
344 << " update, applying normally.";
347 UpdateLocalDataFromServerData(trans, entry);
349 return SUCCESS;
352 std::string GetUniqueBookmarkTagFromUpdate(const sync_pb::SyncEntity& update) {
353 if (!update.has_originator_cache_guid() ||
354 !update.has_originator_client_item_id()) {
355 LOG(ERROR) << "Update is missing requirements for bookmark position."
356 << " This is a server bug.";
357 return UniquePosition::RandomSuffix();
360 return syncable::GenerateSyncableBookmarkHash(
361 update.originator_cache_guid(), update.originator_client_item_id());
364 UniquePosition GetUpdatePosition(const sync_pb::SyncEntity& update,
365 const std::string& suffix) {
366 DCHECK(UniquePosition::IsValidSuffix(suffix));
367 if (!(SyncerProtoUtil::ShouldMaintainPosition(update))) {
368 return UniquePosition::CreateInvalid();
369 } else if (update.has_unique_position()) {
370 return UniquePosition::FromProto(update.unique_position());
371 } else if (update.has_position_in_parent()) {
372 return UniquePosition::FromInt64(update.position_in_parent(), suffix);
373 } else {
374 LOG(ERROR) << "No position information in update. This is a server bug.";
375 return UniquePosition::FromInt64(0, suffix);
379 namespace {
381 // Helper to synthesize a new-style sync_pb::EntitySpecifics for use locally,
382 // when the server speaks only the old sync_pb::SyncEntity_BookmarkData-based
383 // protocol.
384 void UpdateBookmarkSpecifics(const std::string& singleton_tag,
385 const std::string& url,
386 const std::string& favicon_bytes,
387 syncable::ModelNeutralMutableEntry* local_entry) {
388 // In the new-style protocol, the server no longer sends bookmark info for
389 // the "google_chrome" folder. Mimic that here.
390 if (singleton_tag == "google_chrome")
391 return;
392 sync_pb::EntitySpecifics pb;
393 sync_pb::BookmarkSpecifics* bookmark = pb.mutable_bookmark();
394 if (!url.empty())
395 bookmark->set_url(url);
396 if (!favicon_bytes.empty())
397 bookmark->set_favicon(favicon_bytes);
398 local_entry->PutServerSpecifics(pb);
401 void UpdateBookmarkPositioning(
402 const sync_pb::SyncEntity& update,
403 syncable::ModelNeutralMutableEntry* local_entry) {
404 // Update our unique bookmark tag. In many cases this will be identical to
405 // the tag we already have. However, clients that have recently upgraded to
406 // versions that support unique positions will have incorrect tags. See the
407 // v86 migration logic in directory_backing_store.cc for more information.
409 // Both the old and new values are unique to this element. Applying this
410 // update will not risk the creation of conflicting unique tags.
411 std::string bookmark_tag = GetUniqueBookmarkTagFromUpdate(update);
412 if (UniquePosition::IsValidSuffix(bookmark_tag)) {
413 local_entry->PutUniqueBookmarkTag(bookmark_tag);
416 // Update our position.
417 UniquePosition update_pos =
418 GetUpdatePosition(update, local_entry->GetUniqueBookmarkTag());
419 if (update_pos.IsValid()) {
420 local_entry->PutServerUniquePosition(update_pos);
424 } // namespace
426 void UpdateServerFieldsFromUpdate(
427 syncable::ModelNeutralMutableEntry* target,
428 const sync_pb::SyncEntity& update,
429 const std::string& name) {
430 if (update.deleted()) {
431 if (target->GetServerIsDel()) {
432 // If we already think the item is server-deleted, we're done.
433 // Skipping these cases prevents our committed deletions from coming
434 // back and overriding subsequent undeletions. For non-deleted items,
435 // the version number check has a similar effect.
436 return;
438 // The server returns very lightweight replies for deletions, so we don't
439 // clobber a bunch of fields on delete.
440 target->PutServerIsDel(true);
441 if (!target->GetUniqueClientTag().empty()) {
442 // Items identified by the client unique tag are undeletable; when
443 // they're deleted, they go back to version 0.
444 target->PutServerVersion(0);
445 } else {
446 // Otherwise, fake a server version by bumping the local number.
447 target->PutServerVersion(
448 std::max(target->GetServerVersion(), target->GetBaseVersion()) + 1);
450 target->PutIsUnappliedUpdate(true);
451 return;
454 DCHECK_EQ(target->GetId(), SyncableIdFromProto(update.id_string()))
455 << "ID Changing not supported here";
456 target->PutServerParentId(SyncableIdFromProto(update.parent_id_string()));
457 target->PutServerNonUniqueName(name);
458 target->PutServerVersion(update.version());
459 target->PutServerCtime(ProtoTimeToTime(update.ctime()));
460 target->PutServerMtime(ProtoTimeToTime(update.mtime()));
461 target->PutServerIsDir(IsFolder(update));
462 if (update.has_server_defined_unique_tag()) {
463 const std::string& tag = update.server_defined_unique_tag();
464 target->PutUniqueServerTag(tag);
466 if (update.has_client_defined_unique_tag()) {
467 const std::string& tag = update.client_defined_unique_tag();
468 target->PutUniqueClientTag(tag);
470 // Store the datatype-specific part as a protobuf.
471 if (update.has_specifics()) {
472 DCHECK_NE(GetModelType(update), UNSPECIFIED)
473 << "Storing unrecognized datatype in sync database.";
474 target->PutServerSpecifics(update.specifics());
475 } else if (update.has_bookmarkdata()) {
476 // Legacy protocol response for bookmark data.
477 const sync_pb::SyncEntity::BookmarkData& bookmark = update.bookmarkdata();
478 UpdateBookmarkSpecifics(update.server_defined_unique_tag(),
479 bookmark.bookmark_url(),
480 bookmark.bookmark_favicon(),
481 target);
483 target->PutServerAttachmentMetadata(
484 CreateAttachmentMetadata(update.attachment_id()));
485 if (SyncerProtoUtil::ShouldMaintainPosition(update)) {
486 UpdateBookmarkPositioning(update, target);
489 target->PutServerIsDel(update.deleted());
490 // We only mark the entry as unapplied if its version is greater than the
491 // local data. If we're processing the update that corresponds to one of our
492 // commit we don't apply it as time differences may occur.
493 if (update.version() > target->GetBaseVersion()) {
494 target->PutIsUnappliedUpdate(true);
498 // Creates a new Entry iff no Entry exists with the given id.
499 void CreateNewEntry(syncable::ModelNeutralWriteTransaction *trans,
500 const syncable::Id& id) {
501 syncable::Entry entry(trans, GET_BY_ID, id);
502 if (!entry.good()) {
503 syncable::ModelNeutralMutableEntry new_entry(
504 trans,
505 syncable::CREATE_NEW_UPDATE_ITEM,
506 id);
510 // This function is called on an entry when we can update the user-facing data
511 // from the server data.
512 void UpdateLocalDataFromServerData(
513 syncable::WriteTransaction* trans,
514 syncable::MutableEntry* entry) {
515 DCHECK(!entry->GetIsUnsynced());
516 DCHECK(entry->GetIsUnappliedUpdate());
518 DVLOG(2) << "Updating entry : " << *entry;
519 // Start by setting the properties that determine the model_type.
520 entry->PutSpecifics(entry->GetServerSpecifics());
521 // Clear the previous server specifics now that we're applying successfully.
522 entry->PutBaseServerSpecifics(sync_pb::EntitySpecifics());
523 entry->PutIsDir(entry->GetServerIsDir());
524 // This strange dance around the IS_DEL flag avoids problems when setting
525 // the name.
526 // TODO(chron): Is this still an issue? Unit test this codepath.
527 if (entry->GetServerIsDel()) {
528 entry->PutIsDel(true);
529 } else {
530 entry->PutNonUniqueName(entry->GetServerNonUniqueName());
531 entry->PutParentId(entry->GetServerParentId());
532 entry->PutUniquePosition(entry->GetServerUniquePosition());
533 entry->PutIsDel(false);
536 entry->PutCtime(entry->GetServerCtime());
537 entry->PutMtime(entry->GetServerMtime());
538 entry->PutBaseVersion(entry->GetServerVersion());
539 entry->PutIsDel(entry->GetServerIsDel());
540 entry->PutIsUnappliedUpdate(false);
541 entry->PutAttachmentMetadata(entry->GetServerAttachmentMetadata());
544 VerifyCommitResult ValidateCommitEntry(syncable::Entry* entry) {
545 syncable::Id id = entry->GetId();
546 if (id == entry->GetParentId()) {
547 CHECK(id.IsRoot()) << "Non-root item is self parenting." << *entry;
548 // If the root becomes unsynced it can cause us problems.
549 LOG(ERROR) << "Root item became unsynced " << *entry;
550 return VERIFY_UNSYNCABLE;
552 if (entry->IsRoot()) {
553 LOG(ERROR) << "Permanent item became unsynced " << *entry;
554 return VERIFY_UNSYNCABLE;
556 if (entry->GetIsDel() && !entry->GetId().ServerKnows()) {
557 // Drop deleted uncommitted entries.
558 return VERIFY_UNSYNCABLE;
560 return VERIFY_OK;
563 void MarkDeletedChildrenSynced(
564 syncable::Directory* dir,
565 syncable::BaseWriteTransaction* trans,
566 std::set<syncable::Id>* deleted_folders) {
567 // There's two options here.
568 // 1. Scan deleted unsynced entries looking up their pre-delete tree for any
569 // of the deleted folders.
570 // 2. Take each folder and do a tree walk of all entries underneath it.
571 // #2 has a lower big O cost, but writing code to limit the time spent inside
572 // the transaction during each step is simpler with 1. Changing this decision
573 // may be sensible if this code shows up in profiling.
574 if (deleted_folders->empty())
575 return;
576 Directory::Metahandles handles;
577 dir->GetUnsyncedMetaHandles(trans, &handles);
578 if (handles.empty())
579 return;
580 Directory::Metahandles::iterator it;
581 for (it = handles.begin() ; it != handles.end() ; ++it) {
582 syncable::ModelNeutralMutableEntry entry(trans, GET_BY_HANDLE, *it);
583 if (!entry.GetIsUnsynced() || !entry.GetIsDel())
584 continue;
585 syncable::Id id = entry.GetParentId();
586 while (id != trans->root_id()) {
587 if (deleted_folders->find(id) != deleted_folders->end()) {
588 // We've synced the deletion of this deleted entries parent.
589 entry.PutIsUnsynced(false);
590 break;
592 Entry parent(trans, GET_BY_ID, id);
593 if (!parent.good() || !parent.GetIsDel())
594 break;
595 id = parent.GetParentId();
600 VerifyResult VerifyNewEntry(
601 const sync_pb::SyncEntity& update,
602 syncable::Entry* target,
603 const bool deleted) {
604 if (target->good()) {
605 // Not a new update.
606 return VERIFY_UNDECIDED;
608 if (deleted) {
609 // Deletion of an item we've never seen can be ignored.
610 return VERIFY_SKIP;
613 return VERIFY_SUCCESS;
616 // Assumes we have an existing entry; check here for updates that break
617 // consistency rules.
618 VerifyResult VerifyUpdateConsistency(
619 syncable::ModelNeutralWriteTransaction* trans,
620 const sync_pb::SyncEntity& update,
621 const bool deleted,
622 const bool is_directory,
623 ModelType model_type,
624 syncable::ModelNeutralMutableEntry* target) {
626 CHECK(target->good());
627 const syncable::Id& update_id = SyncableIdFromProto(update.id_string());
629 // If the update is a delete, we don't really need to worry at this stage.
630 if (deleted)
631 return VERIFY_SUCCESS;
633 if (model_type == UNSPECIFIED) {
634 // This update is to an item of a datatype we don't recognize. The server
635 // shouldn't have sent it to us. Throw it on the ground.
636 return VERIFY_SKIP;
639 if (target->GetServerVersion() > 0) {
640 // Then we've had an update for this entry before.
641 if (is_directory != target->GetServerIsDir() ||
642 model_type != target->GetServerModelType()) {
643 if (target->GetIsDel()) { // If we've deleted the item, we don't care.
644 return VERIFY_SKIP;
645 } else {
646 LOG(ERROR) << "Server update doesn't agree with previous updates. ";
647 LOG(ERROR) << " Entry: " << *target;
648 LOG(ERROR) << " Update: "
649 << SyncerProtoUtil::SyncEntityDebugString(update);
650 return VERIFY_FAIL;
654 if (!deleted && (target->GetId() == update_id) &&
655 (target->GetServerIsDel() ||
656 (!target->GetIsUnsynced() && target->GetIsDel() &&
657 target->GetBaseVersion() > 0))) {
658 // An undelete. The latter case in the above condition is for
659 // when the server does not give us an update following the
660 // commit of a delete, before undeleting.
661 // Undeletion is common for items that reuse the client-unique tag.
662 VerifyResult result = VerifyUndelete(trans, update, target);
663 if (VERIFY_UNDECIDED != result)
664 return result;
667 if (target->GetBaseVersion() > 0) {
668 // We've committed this update in the past.
669 if (is_directory != target->GetIsDir() ||
670 model_type != target->GetModelType()) {
671 LOG(ERROR) << "Server update doesn't agree with committed item. ";
672 LOG(ERROR) << " Entry: " << *target;
673 LOG(ERROR) << " Update: "
674 << SyncerProtoUtil::SyncEntityDebugString(update);
675 return VERIFY_FAIL;
677 if (target->GetId() == update_id) {
678 if (target->GetServerVersion() > update.version()) {
679 LOG(WARNING) << "We've already seen a more recent version.";
680 LOG(WARNING) << " Entry: " << *target;
681 LOG(WARNING) << " Update: "
682 << SyncerProtoUtil::SyncEntityDebugString(update);
683 return VERIFY_SKIP;
687 return VERIFY_SUCCESS;
690 // Assumes we have an existing entry; verify an update that seems to be
691 // expressing an 'undelete'
692 VerifyResult VerifyUndelete(syncable::ModelNeutralWriteTransaction* trans,
693 const sync_pb::SyncEntity& update,
694 syncable::ModelNeutralMutableEntry* target) {
695 // TODO(nick): We hit this path for items deleted items that the server
696 // tells us to re-create; only deleted items with positive base versions
697 // will hit this path. However, it's not clear how such an undeletion
698 // would actually succeed on the server; in the protocol, a base
699 // version of 0 is required to undelete an object. This codepath
700 // should be deprecated in favor of client-tag style undeletion
701 // (where items go to version 0 when they're deleted), or else
702 // removed entirely (if this type of undeletion is indeed impossible).
703 CHECK(target->good());
704 DVLOG(1) << "Server update is attempting undelete. " << *target
705 << "Update:" << SyncerProtoUtil::SyncEntityDebugString(update);
706 // Move the old one aside and start over. It's too tricky to get the old one
707 // back into a state that would pass CheckTreeInvariants().
708 if (target->GetIsDel()) {
709 if (target->GetUniqueClientTag().empty())
710 LOG(WARNING) << "Doing move-aside undeletion on client-tagged item.";
711 target->PutId(trans->directory()->NextId());
712 target->PutUniqueClientTag(std::string());
713 target->PutBaseVersion(CHANGES_VERSION);
714 target->PutServerVersion(0);
715 return VERIFY_SUCCESS;
717 if (update.version() < target->GetServerVersion()) {
718 LOG(WARNING) << "Update older than current server version for "
719 << *target << " Update:"
720 << SyncerProtoUtil::SyncEntityDebugString(update);
721 return VERIFY_SUCCESS; // Expected in new sync protocol.
723 return VERIFY_UNDECIDED;
726 } // namespace syncer