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 "chrome/browser/chromeos/drive/file_system/create_directory_operation.h"
7 #include "chrome/browser/chromeos/drive/drive.pb.h"
8 #include "chrome/browser/chromeos/drive/file_change.h"
9 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
10 #include "chrome/browser/chromeos/drive/file_system_util.h"
11 #include "chrome/browser/chromeos/drive/job_scheduler.h"
12 #include "chrome/browser/chromeos/drive/resource_metadata.h"
13 #include "content/public/browser/browser_thread.h"
15 using content::BrowserThread
;
18 namespace file_system
{
22 FileError
CreateDirectoryRecursively(internal::ResourceMetadata
* metadata
,
23 const std::string
& parent_local_id
,
24 const base::FilePath
& relative_file_path
,
25 std::set
<std::string
>* updated_local_ids
,
26 FileChange
* changed_files
) {
27 // Split the first component and remaining ones of |relative_file_path|.
28 std::vector
<base::FilePath::StringType
> components
;
29 relative_file_path
.GetComponents(&components
);
30 DCHECK(!components
.empty());
31 base::FilePath
title(components
[0]);
32 base::FilePath remaining_path
;
33 title
.AppendRelativePath(relative_file_path
, &remaining_path
);
36 const base::Time now
= base::Time::Now();
37 entry
.set_title(title
.AsUTF8Unsafe());
38 entry
.mutable_file_info()->set_is_directory(true);
39 entry
.mutable_file_info()->set_last_modified(now
.ToInternalValue());
40 entry
.mutable_file_info()->set_last_accessed(now
.ToInternalValue());
41 entry
.set_parent_local_id(parent_local_id
);
42 entry
.set_metadata_edit_state(ResourceEntry::DIRTY
);
43 entry
.set_modification_date(base::Time::Now().ToInternalValue());
46 FileError error
= metadata
->AddEntry(entry
, &local_id
);
47 if (error
!= FILE_ERROR_OK
)
51 error
= metadata
->GetFilePath(local_id
, &path
);
52 if (error
!= FILE_ERROR_OK
)
55 updated_local_ids
->insert(local_id
);
56 DCHECK(changed_files
);
57 changed_files
->Update(
58 path
, FileChange::FILE_TYPE_DIRECTORY
, FileChange::ADD_OR_UPDATE
);
60 if (remaining_path
.empty()) // All directories are created successfully.
63 // Create descendant directories.
64 return CreateDirectoryRecursively(
65 metadata
, local_id
, remaining_path
, updated_local_ids
, changed_files
);
68 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
69 const base::FilePath
& directory_path
,
72 std::set
<std::string
>* updated_local_ids
,
73 FileChange
* changed_files
) {
74 // Get the existing deepest entry.
75 std::vector
<base::FilePath::StringType
> components
;
76 directory_path
.GetComponents(&components
);
78 if (components
.empty() || components
[0] != util::kDriveGrandRootDirName
)
79 return FILE_ERROR_NOT_FOUND
;
81 base::FilePath
existing_deepest_path(components
[0]);
82 std::string local_id
= util::kDriveGrandRootLocalId
;
83 for (size_t i
= 1; i
< components
.size(); ++i
) {
84 std::string child_local_id
;
86 metadata
->GetChildId(local_id
, components
[i
], &child_local_id
);
87 if (error
== FILE_ERROR_NOT_FOUND
)
89 if (error
!= FILE_ERROR_OK
)
91 existing_deepest_path
= existing_deepest_path
.Append(components
[i
]);
92 local_id
= child_local_id
;
96 FileError error
= metadata
->GetResourceEntryById(local_id
, &entry
);
97 if (error
!= FILE_ERROR_OK
)
100 if (!entry
.file_info().is_directory())
101 return FILE_ERROR_NOT_A_DIRECTORY
;
103 if (directory_path
== existing_deepest_path
)
104 return is_exclusive
? FILE_ERROR_EXISTS
: FILE_ERROR_OK
;
106 // If it is not recursive creation, the found directory must be the direct
107 // parent of |directory_path| to ensure creating exact one directory.
108 if (!is_recursive
&& existing_deepest_path
!= directory_path
.DirName())
109 return FILE_ERROR_NOT_FOUND
;
111 // Create directories under the found directory.
112 base::FilePath remaining_path
;
113 existing_deepest_path
.AppendRelativePath(directory_path
, &remaining_path
);
114 return CreateDirectoryRecursively(metadata
,
123 CreateDirectoryOperation::CreateDirectoryOperation(
124 base::SequencedTaskRunner
* blocking_task_runner
,
125 OperationDelegate
* delegate
,
126 internal::ResourceMetadata
* metadata
)
127 : blocking_task_runner_(blocking_task_runner
),
130 weak_ptr_factory_(this) {
131 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
134 CreateDirectoryOperation::~CreateDirectoryOperation() {
135 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
138 void CreateDirectoryOperation::CreateDirectory(
139 const base::FilePath
& directory_path
,
142 const FileOperationCallback
& callback
) {
143 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
144 DCHECK(!callback
.is_null());
146 std::set
<std::string
>* updated_local_ids
= new std::set
<std::string
>;
147 FileChange
* changed_files(new FileChange
);
148 base::PostTaskAndReplyWithResult(
149 blocking_task_runner_
.get(),
151 base::Bind(&UpdateLocalState
,
159 &CreateDirectoryOperation::CreateDirectoryAfterUpdateLocalState
,
160 weak_ptr_factory_
.GetWeakPtr(),
162 base::Owned(updated_local_ids
),
163 base::Owned(changed_files
)));
166 void CreateDirectoryOperation::CreateDirectoryAfterUpdateLocalState(
167 const FileOperationCallback
& callback
,
168 const std::set
<std::string
>* updated_local_ids
,
169 const FileChange
* changed_files
,
171 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
172 DCHECK(!callback
.is_null());
174 for (const auto& id
: *updated_local_ids
) {
175 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
), id
);
178 delegate_
->OnFileChangedByOperation(*changed_files
);
183 } // namespace file_system