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/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<FileInformationPtr>)>& callback) { |
| 42 // TODO(vtl): FIXME soon |
| 43 NOTIMPLEMENTED(); |
| 44 callback.Run(ERROR_UNIMPLEMENTED, Array<FileInformationPtr>()); |
| 45 } |
| 46 |
| 47 void DirectoryImpl::Change(const String& path, |
| 48 const Callback<void(Error)>& callback) { |
| 49 DCHECK(!path.is_null()); |
| 50 DCHECK(dir_fd_.is_valid()); |
| 51 |
| 52 // TODO(vtl): FIXME soon |
| 53 NOTIMPLEMENTED(); |
| 54 callback.Run(ERROR_UNIMPLEMENTED); |
| 55 } |
| 56 |
| 57 // TODO(vtl): Move the implementation to a thread pool. |
| 58 void DirectoryImpl::OpenFile(const String& path, |
| 59 InterfaceRequest<File> file, |
| 60 uint32_t access_flags, |
| 61 uint32_t open_flags, |
| 62 const Callback<void(Error)>& callback) { |
| 63 DCHECK(!path.is_null()); |
| 64 DCHECK(dir_fd_.is_valid()); |
| 65 |
| 66 if (Error error = IsPathValid(path)) { |
| 67 callback.Run(error); |
| 68 return; |
| 69 } |
| 70 // TODO(vtl): Make sure the path doesn't exit this directory (if appropriate). |
| 71 // TODO(vtl): Maybe allow absolute paths? |
| 72 |
| 73 // Treat unknown flags as "unimplemented". |
| 74 if ((access_flags & ~(kAccessFlagRead | kAccessFlagWrite)) || |
| 75 (open_flags & |
| 76 ~(kOpenFlagCreate | kOpenFlagExclusive | kOpenFlagAppend | |
| 77 kOpenFlagTruncate))) { |
| 78 callback.Run(ERROR_UNIMPLEMENTED); |
| 79 return; |
| 80 } |
| 81 if (!access_flags) { |
| 82 callback.Run(ERROR_INVALID_ARGUMENT); |
| 83 return; |
| 84 } |
| 85 |
| 86 int flags = 0; |
| 87 if ((access_flags & kAccessFlagRead)) { |
| 88 flags = (access_flags & kAccessFlagWrite) ? O_RDWR : O_RDONLY; |
| 89 } else { |
| 90 DCHECK((access_flags & kAccessFlagWrite)); |
| 91 flags = O_WRONLY; |
| 92 } |
| 93 if ((open_flags & kOpenFlagCreate)) |
| 94 flags |= O_CREAT; |
| 95 if ((open_flags & kOpenFlagExclusive)) |
| 96 flags |= O_EXCL; |
| 97 if ((open_flags & kOpenFlagAppend)) |
| 98 flags |= O_APPEND; |
| 99 if ((open_flags & kOpenFlagTruncate)) |
| 100 flags |= O_TRUNC; |
| 101 |
| 102 base::ScopedFD file_fd( |
| 103 HANDLE_EINTR(openat(dir_fd_.get(), path.get().c_str(), flags, 0600))); |
| 104 if (!file_fd.is_valid()) { |
| 105 callback.Run(ErrnoToError(errno)); |
| 106 return; |
| 107 } |
| 108 |
| 109 new FileImpl(file.Pass(), file_fd.Pass()); |
| 110 callback.Run(ERROR_OK); |
| 111 } |
| 112 |
| 113 void DirectoryImpl::OpenDirectory(const String& path, |
| 114 InterfaceRequest<Directory> directory, |
| 115 const Callback<void(Error)>& callback) { |
| 116 DCHECK(!path.is_null()); |
| 117 DCHECK(dir_fd_.is_valid()); |
| 118 |
| 119 // TODO(vtl): FIXME soon |
| 120 NOTIMPLEMENTED(); |
| 121 callback.Run(ERROR_UNIMPLEMENTED); |
| 122 } |
| 123 |
| 124 void DirectoryImpl::Rename(const String& path, |
| 125 const String& new_path, |
| 126 const Callback<void(Error)>& callback) { |
| 127 DCHECK(!path.is_null()); |
| 128 DCHECK(!new_path.is_null()); |
| 129 DCHECK(dir_fd_.is_valid()); |
| 130 |
| 131 if (Error error = IsPathValid(path)) { |
| 132 callback.Run(error); |
| 133 return; |
| 134 } |
| 135 if (Error error = IsPathValid(new_path)) { |
| 136 callback.Run(error); |
| 137 return; |
| 138 } |
| 139 // TODO(vtl): see TODOs about |path| in OpenFile() |
| 140 |
| 141 if (renameat(dir_fd_.get(), path.get().c_str(), dir_fd_.get(), |
| 142 new_path.get().c_str())) { |
| 143 callback.Run(ErrnoToError(errno)); |
| 144 return; |
| 145 } |
| 146 |
| 147 callback.Run(ERROR_OK); |
| 148 } |
| 149 |
| 150 } // namespace files |
| 151 } // namespace mojo |
OLD | NEW |