summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync_file_system/file_change.cc
blob: 7e381bbf36430817ded105e40ee1bf04382a53d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2013 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 <sstream>

#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/sync_file_system/file_change.h"

namespace sync_file_system {

FileChange::FileChange(
    ChangeType change,
    SyncFileType file_type)
    : change_(change),
      file_type_(file_type) {}

std::string FileChange::DebugString() const {
  const char* change_string = NULL;
  switch (change()) {
    case FILE_CHANGE_ADD_OR_UPDATE:
      change_string = "ADD_OR_UPDATE";
      break;
    case FILE_CHANGE_DELETE:
      change_string = "DELETE";
      break;
  }
  const char* type_string = "UNKNOWN";
  switch (file_type()) {
    case SYNC_FILE_TYPE_FILE:
      type_string = "FILE";
      break;
    case SYNC_FILE_TYPE_DIRECTORY:
      type_string = "DIRECTORY";
      break;
    case SYNC_FILE_TYPE_UNKNOWN:
      type_string = "UNKNOWN";
      break;
  }
  return base::StringPrintf("%s:%s", change_string, type_string);
}

FileChangeList::FileChangeList() {}
FileChangeList::~FileChangeList() {}

void FileChangeList::Update(const FileChange& new_change) {
  if (list_.empty()) {
    list_.push_back(new_change);
    return;
  }

  FileChange& last = list_.back();
  if (last.IsFile() != new_change.IsFile()) {
    list_.push_back(new_change);
    return;
  }

  if (last.change() == new_change.change())
    return;

  // ADD + DELETE on directory -> revert
  if (!last.IsFile() && last.IsAddOrUpdate() && new_change.IsDelete()) {
    list_.pop_back();
    return;
  }

  // DELETE + ADD/UPDATE -> ADD/UPDATE
  // ADD/UPDATE + DELETE -> DELETE
  last = new_change;
}

FileChangeList FileChangeList::PopAndGetNewList() const {
  FileChangeList changes;
  changes.list_ = this->list_;
  changes.list_.pop_front();
  return changes;
}

std::string FileChangeList::DebugString() const {
  std::ostringstream ss;
  ss << "{ ";
  for (size_t i = 0; i < list_.size(); ++i)
    ss << list_[i].DebugString() << ", ";
  ss << "}";
  return ss.str();
}

}  // namespace sync_file_system