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