Home | History | Annotate | Download | only in fileapi
      1 // Copyright (c) 2012 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 "webkit/browser/fileapi/local_file_util.h"
      6 
      7 #include "base/file_util.h"
      8 #include "base/files/file_enumerator.h"
      9 #include "base/files/file_util_proxy.h"
     10 #include "url/gurl.h"
     11 #include "webkit/browser/fileapi/async_file_util_adapter.h"
     12 #include "webkit/browser/fileapi/file_system_context.h"
     13 #include "webkit/browser/fileapi/file_system_operation_context.h"
     14 #include "webkit/browser/fileapi/file_system_url.h"
     15 #include "webkit/browser/fileapi/native_file_util.h"
     16 #include "webkit/common/fileapi/file_system_types.h"
     17 #include "webkit/common/fileapi/file_system_util.h"
     18 
     19 namespace fileapi {
     20 
     21 AsyncFileUtil* AsyncFileUtil::CreateForLocalFileSystem() {
     22   return new AsyncFileUtilAdapter(new LocalFileUtil());
     23 }
     24 
     25 using base::PlatformFileError;
     26 
     27 class LocalFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator {
     28  public:
     29   LocalFileEnumerator(const base::FilePath& platform_root_path,
     30                       const base::FilePath& virtual_root_path,
     31                       int file_type)
     32       : file_enum_(platform_root_path, false /* recursive */, file_type),
     33         platform_root_path_(platform_root_path),
     34         virtual_root_path_(virtual_root_path) {
     35   }
     36 
     37   virtual ~LocalFileEnumerator() {}
     38 
     39   virtual base::FilePath Next() OVERRIDE;
     40   virtual int64 Size() OVERRIDE;
     41   virtual base::Time LastModifiedTime() OVERRIDE;
     42   virtual bool IsDirectory() OVERRIDE;
     43 
     44  private:
     45   base::FileEnumerator file_enum_;
     46   base::FileEnumerator::FileInfo file_util_info_;
     47   base::FilePath platform_root_path_;
     48   base::FilePath virtual_root_path_;
     49 };
     50 
     51 base::FilePath LocalFileEnumerator::Next() {
     52   base::FilePath next = file_enum_.Next();
     53   // Don't return symlinks.
     54   while (!next.empty() && base::IsLink(next))
     55     next = file_enum_.Next();
     56   if (next.empty())
     57     return next;
     58   file_util_info_ = file_enum_.GetInfo();
     59 
     60   base::FilePath path;
     61   platform_root_path_.AppendRelativePath(next, &path);
     62   return virtual_root_path_.Append(path);
     63 }
     64 
     65 int64 LocalFileEnumerator::Size() {
     66   return file_util_info_.GetSize();
     67 }
     68 
     69 base::Time LocalFileEnumerator::LastModifiedTime() {
     70   return file_util_info_.GetLastModifiedTime();
     71 }
     72 
     73 bool LocalFileEnumerator::IsDirectory() {
     74   return file_util_info_.IsDirectory();
     75 }
     76 
     77 LocalFileUtil::LocalFileUtil() {}
     78 
     79 LocalFileUtil::~LocalFileUtil() {}
     80 
     81 PlatformFileError LocalFileUtil::CreateOrOpen(
     82     FileSystemOperationContext* context,
     83     const FileSystemURL& url, int file_flags,
     84     base::PlatformFile* file_handle, bool* created) {
     85   *created = false;
     86   base::FilePath file_path;
     87   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
     88   if (error != base::PLATFORM_FILE_OK)
     89     return error;
     90   // Disallow opening files in symlinked paths.
     91   if (base::IsLink(file_path))
     92     return base::PLATFORM_FILE_ERROR_NOT_FOUND;
     93   return NativeFileUtil::CreateOrOpen(
     94       file_path, file_flags, file_handle, created);
     95 }
     96 
     97 PlatformFileError LocalFileUtil::Close(FileSystemOperationContext* context,
     98                                        base::PlatformFile file) {
     99   return NativeFileUtil::Close(file);
    100 }
    101 
    102 PlatformFileError LocalFileUtil::EnsureFileExists(
    103     FileSystemOperationContext* context,
    104     const FileSystemURL& url,
    105     bool* created) {
    106   base::FilePath file_path;
    107   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    108   if (error != base::PLATFORM_FILE_OK)
    109     return error;
    110   return NativeFileUtil::EnsureFileExists(file_path, created);
    111 }
    112 
    113 PlatformFileError LocalFileUtil::CreateDirectory(
    114     FileSystemOperationContext* context,
    115     const FileSystemURL& url,
    116     bool exclusive,
    117     bool recursive) {
    118   base::FilePath file_path;
    119   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    120   if (error != base::PLATFORM_FILE_OK)
    121     return error;
    122   return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive);
    123 }
    124 
    125 PlatformFileError LocalFileUtil::GetFileInfo(
    126     FileSystemOperationContext* context,
    127     const FileSystemURL& url,
    128     base::PlatformFileInfo* file_info,
    129     base::FilePath* platform_file_path) {
    130   base::FilePath file_path;
    131   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    132   if (error != base::PLATFORM_FILE_OK)
    133     return error;
    134   // We should not follow symbolic links in sandboxed file system.
    135   if (base::IsLink(file_path))
    136     return base::PLATFORM_FILE_ERROR_NOT_FOUND;
    137   error = NativeFileUtil::GetFileInfo(file_path, file_info);
    138   if (error == base::PLATFORM_FILE_OK)
    139     *platform_file_path = file_path;
    140   return error;
    141 }
    142 
    143 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> LocalFileUtil::
    144     CreateFileEnumerator(
    145         FileSystemOperationContext* context,
    146         const FileSystemURL& root_url) {
    147   base::FilePath file_path;
    148   if (GetLocalFilePath(context, root_url, &file_path) !=
    149       base::PLATFORM_FILE_OK) {
    150     return make_scoped_ptr(new EmptyFileEnumerator)
    151         .PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
    152   }
    153   return make_scoped_ptr(new LocalFileEnumerator(
    154       file_path, root_url.path(),
    155       base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES))
    156       .PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
    157 }
    158 
    159 PlatformFileError LocalFileUtil::GetLocalFilePath(
    160     FileSystemOperationContext* context,
    161     const FileSystemURL& url,
    162     base::FilePath* local_file_path) {
    163   DCHECK(local_file_path);
    164   DCHECK(url.is_valid());
    165   if (url.path().empty()) {
    166     // Root direcory case, which should not be accessed.
    167     return base::PLATFORM_FILE_ERROR_ACCESS_DENIED;
    168   }
    169   *local_file_path = url.path();
    170   return base::PLATFORM_FILE_OK;
    171 }
    172 
    173 PlatformFileError LocalFileUtil::Touch(
    174     FileSystemOperationContext* context,
    175     const FileSystemURL& url,
    176     const base::Time& last_access_time,
    177     const base::Time& last_modified_time) {
    178   base::FilePath file_path;
    179   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    180   if (error != base::PLATFORM_FILE_OK)
    181     return error;
    182   return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time);
    183 }
    184 
    185 PlatformFileError LocalFileUtil::Truncate(
    186     FileSystemOperationContext* context,
    187     const FileSystemURL& url,
    188     int64 length) {
    189   base::FilePath file_path;
    190   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    191   if (error != base::PLATFORM_FILE_OK)
    192     return error;
    193   return NativeFileUtil::Truncate(file_path, length);
    194 }
    195 
    196 PlatformFileError LocalFileUtil::CopyOrMoveFile(
    197     FileSystemOperationContext* context,
    198     const FileSystemURL& src_url,
    199     const FileSystemURL& dest_url,
    200     CopyOrMoveOption option,
    201     bool copy) {
    202   base::FilePath src_file_path;
    203   PlatformFileError error = GetLocalFilePath(context, src_url, &src_file_path);
    204   if (error != base::PLATFORM_FILE_OK)
    205     return error;
    206 
    207   base::FilePath dest_file_path;
    208   error = GetLocalFilePath(context, dest_url, &dest_file_path);
    209   if (error != base::PLATFORM_FILE_OK)
    210     return error;
    211 
    212   return NativeFileUtil::CopyOrMoveFile(
    213       src_file_path, dest_file_path, option,
    214       fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url, copy));
    215 }
    216 
    217 PlatformFileError LocalFileUtil::CopyInForeignFile(
    218     FileSystemOperationContext* context,
    219     const base::FilePath& src_file_path,
    220     const FileSystemURL& dest_url) {
    221   if (src_file_path.empty())
    222     return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
    223 
    224   base::FilePath dest_file_path;
    225   PlatformFileError error =
    226       GetLocalFilePath(context, dest_url, &dest_file_path);
    227   if (error != base::PLATFORM_FILE_OK)
    228     return error;
    229   return NativeFileUtil::CopyOrMoveFile(
    230       src_file_path, dest_file_path, FileSystemOperation::OPTION_NONE,
    231       fileapi::NativeFileUtil::CopyOrMoveModeForDestination(dest_url,
    232                                                             true /* copy */));
    233 }
    234 
    235 PlatformFileError LocalFileUtil::DeleteFile(
    236     FileSystemOperationContext* context,
    237     const FileSystemURL& url) {
    238   base::FilePath file_path;
    239   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    240   if (error != base::PLATFORM_FILE_OK)
    241     return error;
    242   return NativeFileUtil::DeleteFile(file_path);
    243 }
    244 
    245 PlatformFileError LocalFileUtil::DeleteDirectory(
    246     FileSystemOperationContext* context,
    247     const FileSystemURL& url) {
    248   base::FilePath file_path;
    249   PlatformFileError error = GetLocalFilePath(context, url, &file_path);
    250   if (error != base::PLATFORM_FILE_OK)
    251     return error;
    252   return NativeFileUtil::DeleteDirectory(file_path);
    253 }
    254 
    255 webkit_blob::ScopedFile LocalFileUtil::CreateSnapshotFile(
    256     FileSystemOperationContext* context,
    257     const FileSystemURL& url,
    258     base::PlatformFileError* error,
    259     base::PlatformFileInfo* file_info,
    260     base::FilePath* platform_path) {
    261   DCHECK(file_info);
    262   // We're just returning the local file information.
    263   *error = GetFileInfo(context, url, file_info, platform_path);
    264   if (*error == base::PLATFORM_FILE_OK && file_info->is_directory)
    265     *error = base::PLATFORM_FILE_ERROR_NOT_A_FILE;
    266   return webkit_blob::ScopedFile();
    267 }
    268 
    269 }  // namespace fileapi
    270