1 // Copyright (c) 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 "components/drive/file_change.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "components/drive/drive.pb.h"
15 FileChange::Change::Change(ChangeType change
, FileType file_type
)
16 : change_(change
), file_type_(file_type
) {
19 std::string
FileChange::Change::DebugString() const {
20 const char* change_string
= NULL
;
22 case CHANGE_TYPE_ADD_OR_UPDATE
:
23 change_string
= "ADD_OR_UPDATE";
25 case CHANGE_TYPE_DELETE
:
26 change_string
= "DELETE";
29 const char* type_string
= "NO_INFO";
30 switch (file_type()) {
31 case FileChange::FILE_TYPE_FILE
:
34 case FileChange::FILE_TYPE_DIRECTORY
:
35 type_string
= "DIRECTORY";
37 case FILE_TYPE_NO_INFO
:
38 // Keeps it as "no_info".
41 return base::StringPrintf("%s:%s", change_string
, type_string
);
44 FileChange::ChangeList::ChangeList() {
46 FileChange::ChangeList::~ChangeList() {
49 void FileChange::ChangeList::Update(const Change
& new_change
) {
51 list_
.push_back(new_change
);
55 Change
& last
= list_
.back();
56 if (last
.IsFile() != new_change
.IsFile()) {
57 list_
.push_back(new_change
);
61 if (last
.change() == new_change
.change())
64 // ADD + DELETE on directory -> revert
65 if (!last
.IsFile() && last
.IsAddOrUpdate() && new_change
.IsDelete()) {
70 // DELETE + ADD/UPDATE -> ADD/UPDATE
71 // ADD/UPDATE + DELETE -> DELETE
75 FileChange::ChangeList
FileChange::ChangeList::PopAndGetNewList() const {
77 changes
.list_
= this->list_
;
78 changes
.list_
.pop_front();
82 std::string
FileChange::ChangeList::DebugString() const {
83 std::ostringstream ss
;
85 for (size_t i
= 0; i
< list_
.size(); ++i
)
86 ss
<< list_
[i
].DebugString() << ", ";
91 FileChange::FileChange() {
93 FileChange::~FileChange() {}
95 void FileChange::Update(const base::FilePath file_path
,
96 const FileChange::Change
& new_change
) {
97 map_
[file_path
].Update(new_change
);
100 void FileChange::Update(const base::FilePath file_path
,
101 const FileChange::ChangeList
& new_change
) {
102 for (ChangeList::List::const_iterator it
= new_change
.list().begin();
103 it
!= new_change
.list().end();
105 map_
[file_path
].Update(*it
);
109 void FileChange::Update(const base::FilePath file_path
,
111 FileChange::ChangeType change
) {
112 Update(file_path
, FileChange::Change(change
, file_type
));
115 void FileChange::Update(const base::FilePath file_path
,
116 const ResourceEntry
& entry
,
117 FileChange::ChangeType change
) {
118 FileType type
= !entry
.has_file_info()
120 : entry
.file_info().is_directory() ? FILE_TYPE_DIRECTORY
123 Update(file_path
, type
, change
);
126 void FileChange::Apply(const FileChange
& new_changed_files
) {
127 for (Map::const_iterator it
= new_changed_files
.map().begin();
128 it
!= new_changed_files
.map().end();
130 Update(it
->first
, it
->second
);
134 size_t FileChange::CountDirectory(const base::FilePath
& directory_path
) const {
136 for (Map::const_iterator it
= map_
.begin(); it
!= map_
.end(); it
++) {
137 if (it
->first
.DirName() == directory_path
)
143 std::string
FileChange::DebugString() const {
144 std::ostringstream ss
;
146 for (FileChange::Map::const_iterator it
= map_
.begin(); it
!= map_
.end();
148 ss
<< it
->first
.value() << ": " << it
->second
.DebugString() << ", ";