OLD | NEW |
(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/files/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 #include <time.h> |
| 13 #include <unistd.h> |
| 14 |
| 15 #include "base/files/file_path.h" |
| 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/logging.h" |
| 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/posix/eintr_wrapper.h" |
| 20 #include "build/build_config.h" |
| 21 #include "services/files/file_impl.h" |
| 22 #include "services/files/util.h" |
| 23 |
| 24 namespace mojo { |
| 25 namespace files { |
| 26 |
| 27 namespace { |
| 28 |
| 29 Error ValidateOpenFlags(uint32_t open_flags, bool is_directory) { |
| 30 // Treat unknown flags as "unimplemented". |
| 31 if ((open_flags & |
| 32 ~(kOpenFlagRead | kOpenFlagWrite | kOpenFlagCreate | kOpenFlagExclusive | |
| 33 kOpenFlagAppend | kOpenFlagTruncate))) |
| 34 return ERROR_UNIMPLEMENTED; |
| 35 |
| 36 // At least one of |kOpenFlagRead| or |kOpenFlagWrite| must be set. |
| 37 if (!(open_flags & (kOpenFlagRead | kOpenFlagWrite))) |
| 38 return ERROR_INVALID_ARGUMENT; |
| 39 |
| 40 // |kOpenFlagCreate| requires |kOpenFlagWrite|. |
| 41 if ((open_flags & kOpenFlagCreate) && !(open_flags & kOpenFlagWrite)) |
| 42 return ERROR_INVALID_ARGUMENT; |
| 43 |
| 44 // |kOpenFlagExclusive| requires |kOpenFlagCreate|. |
| 45 if ((open_flags & kOpenFlagExclusive) && !(open_flags & kOpenFlagCreate)) |
| 46 return ERROR_INVALID_ARGUMENT; |
| 47 |
| 48 if (is_directory) { |
| 49 // Check that file-only flags aren't set. |
| 50 if ((open_flags & (kOpenFlagAppend | kOpenFlagTruncate))) |
| 51 return ERROR_INVALID_ARGUMENT; |
| 52 return ERROR_OK; |
| 53 } |
| 54 |
| 55 // File-only flags: |
| 56 |
| 57 // |kOpenFlagAppend| requires |kOpenFlagWrite|. |
| 58 if ((open_flags & kOpenFlagAppend) && !(open_flags & kOpenFlagWrite)) |
| 59 return ERROR_INVALID_ARGUMENT; |
| 60 |
| 61 // |kOpenFlagTruncate| requires |kOpenFlagWrite|. |
| 62 if ((open_flags & kOpenFlagTruncate) && !(open_flags & kOpenFlagWrite)) |
| 63 return ERROR_INVALID_ARGUMENT; |
| 64 |
| 65 return ERROR_OK; |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 DirectoryImpl::DirectoryImpl(InterfaceRequest<Directory> request, |
| 71 base::ScopedFD dir_fd, |
| 72 scoped_ptr<base::ScopedTempDir> temp_dir) |
| 73 : binding_(this, request.Pass()), |
| 74 dir_fd_(dir_fd.Pass()), |
| 75 temp_dir_(temp_dir.Pass()) { |
| 76 DCHECK(dir_fd_.is_valid()); |
| 77 } |
| 78 |
| 79 DirectoryImpl::~DirectoryImpl() { |
| 80 } |
| 81 |
| 82 void DirectoryImpl::Read( |
| 83 const Callback<void(Error, Array<DirectoryEntryPtr>)>& callback) { |
| 84 // TODO(vtl): FIXME sooner |
| 85 NOTIMPLEMENTED(); |
| 86 callback.Run(ERROR_UNIMPLEMENTED, Array<DirectoryEntryPtr>()); |
| 87 } |
| 88 |
| 89 void DirectoryImpl::Stat( |
| 90 const Callback<void(Error, FileInformationPtr)>& callback) { |
| 91 // TODO(vtl): FIXME sooner |
| 92 NOTIMPLEMENTED(); |
| 93 callback.Run(ERROR_UNIMPLEMENTED, nullptr); |
| 94 } |
| 95 |
| 96 void DirectoryImpl::Touch(TimespecOrNowPtr atime, |
| 97 TimespecOrNowPtr mtime, |
| 98 const Callback<void(Error)>& callback) { |
| 99 // TODO(vtl): FIXME sooner |
| 100 NOTIMPLEMENTED(); |
| 101 callback.Run(ERROR_UNIMPLEMENTED); |
| 102 } |
| 103 |
| 104 // TODO(vtl): Move the implementation to a thread pool. |
| 105 void DirectoryImpl::OpenFile(const String& path, |
| 106 InterfaceRequest<File> file, |
| 107 uint32_t open_flags, |
| 108 const Callback<void(Error)>& callback) { |
| 109 DCHECK(!path.is_null()); |
| 110 DCHECK(dir_fd_.is_valid()); |
| 111 |
| 112 if (Error error = IsPathValid(path)) { |
| 113 callback.Run(error); |
| 114 return; |
| 115 } |
| 116 // TODO(vtl): Make sure the path doesn't exit this directory (if appropriate). |
| 117 // TODO(vtl): Maybe allow absolute paths? |
| 118 |
| 119 if (Error error = ValidateOpenFlags(open_flags, false)) { |
| 120 callback.Run(error); |
| 121 return; |
| 122 } |
| 123 |
| 124 int flags = 0; |
| 125 if ((open_flags & kOpenFlagRead)) |
| 126 flags |= (open_flags & kOpenFlagWrite) ? O_RDWR : O_RDONLY; |
| 127 else |
| 128 flags |= O_WRONLY; |
| 129 if ((open_flags & kOpenFlagCreate)) |
| 130 flags |= O_CREAT; |
| 131 if ((open_flags & kOpenFlagExclusive)) |
| 132 flags |= O_EXCL; |
| 133 if ((open_flags & kOpenFlagAppend)) |
| 134 flags |= O_APPEND; |
| 135 if ((open_flags & kOpenFlagTruncate)) |
| 136 flags |= O_TRUNC; |
| 137 |
| 138 base::ScopedFD file_fd( |
| 139 HANDLE_EINTR(openat(dir_fd_.get(), path.get().c_str(), flags, 0600))); |
| 140 if (!file_fd.is_valid()) { |
| 141 callback.Run(ErrnoToError(errno)); |
| 142 return; |
| 143 } |
| 144 |
| 145 if (file.is_pending()) |
| 146 new FileImpl(file.Pass(), file_fd.Pass()); |
| 147 callback.Run(ERROR_OK); |
| 148 } |
| 149 |
| 150 void DirectoryImpl::OpenDirectory(const String& path, |
| 151 InterfaceRequest<Directory> directory, |
| 152 uint32_t open_flags, |
| 153 const Callback<void(Error)>& callback) { |
| 154 // TODO(vtl): FIXME sooner |
| 155 NOTIMPLEMENTED(); |
| 156 callback.Run(ERROR_UNIMPLEMENTED); |
| 157 } |
| 158 |
| 159 void DirectoryImpl::Rename(const String& path, |
| 160 const String& new_path, |
| 161 const Callback<void(Error)>& callback) { |
| 162 DCHECK(!path.is_null()); |
| 163 DCHECK(!new_path.is_null()); |
| 164 DCHECK(dir_fd_.is_valid()); |
| 165 |
| 166 if (Error error = IsPathValid(path)) { |
| 167 callback.Run(error); |
| 168 return; |
| 169 } |
| 170 if (Error error = IsPathValid(new_path)) { |
| 171 callback.Run(error); |
| 172 return; |
| 173 } |
| 174 // TODO(vtl): see TODOs about |path| in OpenFile() |
| 175 |
| 176 if (renameat(dir_fd_.get(), path.get().c_str(), dir_fd_.get(), |
| 177 new_path.get().c_str())) { |
| 178 callback.Run(ErrnoToError(errno)); |
| 179 return; |
| 180 } |
| 181 |
| 182 callback.Run(ERROR_OK); |
| 183 } |
| 184 |
| 185 void DirectoryImpl::Delete(const String& path, |
| 186 uint32_t delete_flags, |
| 187 const Callback<void(Error)>& callback) { |
| 188 // TODO(vtl): FIXME sooner |
| 189 NOTIMPLEMENTED(); |
| 190 callback.Run(ERROR_UNIMPLEMENTED); |
| 191 } |
| 192 |
| 193 } // namespace files |
| 194 } // namespace mojo |
OLD | NEW |