Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: services/file_manager/directory_impl.cc

Issue 875643004: Prototype of Files service. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Directory: make OpenFile()'s |file| arg optional; add flags to OpenDirectory(); add Delete() Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "services/file_manager/directory_impl.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12
13 #include "base/files/file_util.h"
14 #include "base/files/scoped_file.h"
15 #include "base/logging.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "services/file_manager/file_impl.h"
18 #include "services/file_manager/util.h"
19
20 namespace mojo {
21 namespace files {
22
23 DirectoryImpl::DirectoryImpl(InterfaceRequest<Directory> request,
24 base::ScopedFD dir_fd,
25 const base::FilePath& owned_dir_name)
26 : binding_(this, request.Pass()),
27 dir_fd_(dir_fd.Pass()),
28 owned_dir_name_(owned_dir_name) {
29 DCHECK(dir_fd_.is_valid());
30 }
31
32 DirectoryImpl::~DirectoryImpl() {
33 if (!owned_dir_name_.empty()) {
34 DVLOG(1) << "Deleting owned directory: " << owned_dir_name_.value();
35 LOG_IF(ERROR, !base::DeleteFile(owned_dir_name_, true))
36 << "Failed to delete owned directory: " << owned_dir_name_.value();
37 }
38 }
39
40 void DirectoryImpl::Read(
41 const Callback<void(Error, Array<DirectoryEntryPtr>)>& callback) {
42 // TODO(vtl): FIXME soon
43 NOTIMPLEMENTED();
44 callback.Run(ERROR_UNIMPLEMENTED, Array<DirectoryEntryPtr>());
45 }
46
47 void DirectoryImpl::Stat(
48 const Callback<void(Error, FileInformationPtr)>& callback) {
49 DCHECK(dir_fd_.is_valid());
50
51 // TODO(vtl): FIXME soon
52 NOTIMPLEMENTED();
53 callback.Run(ERROR_UNIMPLEMENTED, nullptr);
54 }
55
56 void DirectoryImpl::Touch(FileTimesPtr times,
57 const Callback<void(Error)>& callback) {
58 DCHECK(dir_fd_.is_valid());
59
60 // TODO(vtl): FIXME soon
61 NOTIMPLEMENTED();
62 callback.Run(ERROR_UNIMPLEMENTED);
63 }
64
65 // TODO(vtl): Move the implementation to a thread pool.
66 void DirectoryImpl::OpenFile(const String& path,
67 InterfaceRequest<File> file,
68 uint32_t access_flags,
69 uint32_t open_flags,
70 const Callback<void(Error)>& callback) {
71 DCHECK(!path.is_null());
72 DCHECK(dir_fd_.is_valid());
73
74 if (Error error = IsPathValid(path)) {
75 callback.Run(error);
76 return;
77 }
78 // TODO(vtl): Make sure the path doesn't exit this directory (if appropriate).
79 // TODO(vtl): Maybe allow absolute paths?
80
81 // Treat unknown flags as "unimplemented".
82 if ((access_flags & ~(kAccessFlagRead | kAccessFlagWrite)) ||
83 (open_flags &
84 ~(kOpenFlagCreate | kOpenFlagExclusive | kOpenFlagAppend |
85 kOpenFlagTruncate))) {
86 callback.Run(ERROR_UNIMPLEMENTED);
87 return;
88 }
89 if (!access_flags) {
90 callback.Run(ERROR_INVALID_ARGUMENT);
91 return;
92 }
93
94 int flags = 0;
95 if ((access_flags & kAccessFlagRead)) {
96 flags = (access_flags & kAccessFlagWrite) ? O_RDWR : O_RDONLY;
97 } else {
98 DCHECK((access_flags & kAccessFlagWrite));
99 flags = O_WRONLY;
100 }
101 if ((open_flags & kOpenFlagCreate))
102 flags |= O_CREAT;
103 if ((open_flags & kOpenFlagExclusive))
104 flags |= O_EXCL;
105 if ((open_flags & kOpenFlagAppend))
106 flags |= O_APPEND;
107 if ((open_flags & kOpenFlagTruncate))
108 flags |= O_TRUNC;
109
110 base::ScopedFD file_fd(
111 HANDLE_EINTR(openat(dir_fd_.get(), path.get().c_str(), flags, 0600)));
112 if (!file_fd.is_valid()) {
113 callback.Run(ErrnoToError(errno));
114 return;
115 }
116
117 if (file.is_pending())
118 new FileImpl(file.Pass(), file_fd.Pass());
119 callback.Run(ERROR_OK);
120 }
121
122 void DirectoryImpl::OpenDirectory(const String& path,
123 InterfaceRequest<Directory> directory,
124 uint32_t access_flags,
125 uint32_t open_flags,
126 const Callback<void(Error)>& callback) {
127 DCHECK(!path.is_null());
128 DCHECK(dir_fd_.is_valid());
129
130 // TODO(vtl): FIXME soon
131 NOTIMPLEMENTED();
132 callback.Run(ERROR_UNIMPLEMENTED);
133 }
134
135 void DirectoryImpl::Rename(const String& path,
136 const String& new_path,
137 const Callback<void(Error)>& callback) {
138 DCHECK(!path.is_null());
139 DCHECK(!new_path.is_null());
140 DCHECK(dir_fd_.is_valid());
141
142 if (Error error = IsPathValid(path)) {
143 callback.Run(error);
144 return;
145 }
146 if (Error error = IsPathValid(new_path)) {
147 callback.Run(error);
148 return;
149 }
150 // TODO(vtl): see TODOs about |path| in OpenFile()
151
152 if (renameat(dir_fd_.get(), path.get().c_str(), dir_fd_.get(),
153 new_path.get().c_str())) {
154 callback.Run(ErrnoToError(errno));
155 return;
156 }
157
158 callback.Run(ERROR_OK);
159 }
160
161 void DirectoryImpl::Delete(const String& path,
162 const Callback<void(Error)>& callback) {
163 DCHECK(!path.is_null());
164 DCHECK(dir_fd_.is_valid());
165
166 if (Error error = IsPathValid(path)) {
167 callback.Run(error);
168 return;
169 }
170 // TODO(vtl): see TODOs about |path| in OpenFile()
171
172 // TODO(vtl): FIXME soon
173 NOTIMPLEMENTED();
174 callback.Run(ERROR_UNIMPLEMENTED);
175 }
176
177 } // namespace files
178 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698