1 // Copyright (c) 2013 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_system/create_directory_operation.h"
7 #include "components/drive/drive.pb.h"
8 #include "components/drive/file_change.h"
9 #include "components/drive/file_system/operation_delegate.h"
10 #include "components/drive/file_system_core_util.h"
11 #include "components/drive/job_scheduler.h"
12 #include "components/drive/resource_metadata.h"
15 namespace file_system
{
19 FileError
CreateDirectoryRecursively(internal::ResourceMetadata
* metadata
,
20 const std::string
& parent_local_id
,
21 const base::FilePath
& relative_file_path
,
22 std::set
<std::string
>* updated_local_ids
,
23 FileChange
* changed_files
) {
24 // Split the first component and remaining ones of |relative_file_path|.
25 std::vector
<base::FilePath::StringType
> components
;
26 relative_file_path
.GetComponents(&components
);
27 DCHECK(!components
.empty());
28 base::FilePath
title(components
[0]);
29 base::FilePath remaining_path
;
30 title
.AppendRelativePath(relative_file_path
, &remaining_path
);
33 const base::Time now
= base::Time::Now();
34 entry
.set_title(title
.AsUTF8Unsafe());
35 entry
.mutable_file_info()->set_is_directory(true);
36 entry
.mutable_file_info()->set_last_modified(now
.ToInternalValue());
37 entry
.mutable_file_info()->set_last_accessed(now
.ToInternalValue());
38 entry
.set_parent_local_id(parent_local_id
);
39 entry
.set_metadata_edit_state(ResourceEntry::DIRTY
);
40 entry
.set_modification_date(base::Time::Now().ToInternalValue());
43 FileError error
= metadata
->AddEntry(entry
, &local_id
);
44 if (error
!= FILE_ERROR_OK
)
48 error
= metadata
->GetFilePath(local_id
, &path
);
49 if (error
!= FILE_ERROR_OK
)
52 updated_local_ids
->insert(local_id
);
53 DCHECK(changed_files
);
54 changed_files
->Update(path
, FileChange::FILE_TYPE_DIRECTORY
,
55 FileChange::CHANGE_TYPE_ADD_OR_UPDATE
);
57 if (remaining_path
.empty()) // All directories are created successfully.
60 // Create descendant directories.
61 return CreateDirectoryRecursively(
62 metadata
, local_id
, remaining_path
, updated_local_ids
, changed_files
);
65 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
66 const base::FilePath
& directory_path
,
69 std::set
<std::string
>* updated_local_ids
,
70 FileChange
* changed_files
) {
71 // Get the existing deepest entry.
72 std::vector
<base::FilePath::StringType
> components
;
73 directory_path
.GetComponents(&components
);
75 if (components
.empty() ||
76 components
[0] != util::GetDriveGrandRootPath().value())
77 return FILE_ERROR_NOT_FOUND
;
79 base::FilePath
existing_deepest_path(components
[0]);
80 std::string local_id
= util::kDriveGrandRootLocalId
;
81 for (size_t i
= 1; i
< components
.size(); ++i
) {
82 const std::string component
= base::FilePath(components
[i
]).AsUTF8Unsafe();
83 std::string child_local_id
;
85 metadata
->GetChildId(local_id
, component
, &child_local_id
);
86 if (error
== FILE_ERROR_NOT_FOUND
)
88 if (error
!= FILE_ERROR_OK
)
90 existing_deepest_path
= existing_deepest_path
.Append(components
[i
]);
91 local_id
= child_local_id
;
95 FileError error
= metadata
->GetResourceEntryById(local_id
, &entry
);
96 if (error
!= FILE_ERROR_OK
)
99 if (!entry
.file_info().is_directory())
100 return FILE_ERROR_NOT_A_DIRECTORY
;
102 if (directory_path
== existing_deepest_path
)
103 return is_exclusive
? FILE_ERROR_EXISTS
: FILE_ERROR_OK
;
105 // If it is not recursive creation, the found directory must be the direct
106 // parent of |directory_path| to ensure creating exact one directory.
107 if (!is_recursive
&& existing_deepest_path
!= directory_path
.DirName())
108 return FILE_ERROR_NOT_FOUND
;
110 // Create directories under the found directory.
111 base::FilePath remaining_path
;
112 existing_deepest_path
.AppendRelativePath(directory_path
, &remaining_path
);
113 return CreateDirectoryRecursively(metadata
,
122 CreateDirectoryOperation::CreateDirectoryOperation(
123 base::SequencedTaskRunner
* blocking_task_runner
,
124 OperationDelegate
* delegate
,
125 internal::ResourceMetadata
* metadata
)
126 : blocking_task_runner_(blocking_task_runner
),
129 weak_ptr_factory_(this) {
132 CreateDirectoryOperation::~CreateDirectoryOperation() {
133 DCHECK(thread_checker_
.CalledOnValidThread());
136 void CreateDirectoryOperation::CreateDirectory(
137 const base::FilePath
& directory_path
,
140 const FileOperationCallback
& callback
) {
141 DCHECK(thread_checker_
.CalledOnValidThread());
142 DCHECK(!callback
.is_null());
144 std::set
<std::string
>* updated_local_ids
= new std::set
<std::string
>;
145 FileChange
* changed_files(new FileChange
);
146 base::PostTaskAndReplyWithResult(
147 blocking_task_runner_
.get(),
149 base::Bind(&UpdateLocalState
,
157 &CreateDirectoryOperation::CreateDirectoryAfterUpdateLocalState
,
158 weak_ptr_factory_
.GetWeakPtr(),
160 base::Owned(updated_local_ids
),
161 base::Owned(changed_files
)));
164 void CreateDirectoryOperation::CreateDirectoryAfterUpdateLocalState(
165 const FileOperationCallback
& callback
,
166 const std::set
<std::string
>* updated_local_ids
,
167 const FileChange
* changed_files
,
169 DCHECK(thread_checker_
.CalledOnValidThread());
170 DCHECK(!callback
.is_null());
172 for (const auto& id
: *updated_local_ids
) {
173 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
), id
);
176 delegate_
->OnFileChangedByOperation(*changed_files
);
181 } // namespace file_system