file_system_util.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/platform_file.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/WebKit/Source/Platform/chromium/public/WebFileSystemType.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/WebKit/Source/WebKit/chromium/public/WebFileError.h"
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/common/fileapi/file_system_types.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "webkit/quota/quota_types.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "webkit/storage/webkit_storage_export.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace fileapi {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char kPersistentDir[];
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char kTemporaryDir[];
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char kExternalDir[];
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char kIsolatedDir[];
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern const char kTestDir[];
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class WEBKIT_STORAGE_EXPORT VirtualPath {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const base::FilePath::CharType kRoot[];
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const base::FilePath::CharType kSeparator;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Use this instead of base::FilePath::BaseName when operating on virtual
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // paths. FilePath::BaseName will get confused by ':' on Windows when it
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // looks like a drive letter separator; this will treat it as just another
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // character.
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static base::FilePath BaseName(const base::FilePath& virtual_path);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Use this instead of base::FilePath::DirName when operating on virtual
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // paths.
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static base::FilePath DirName(const base::FilePath& virtual_path);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Likewise, use this instead of base::FilePath::GetComponents when
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // operating on virtual paths.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note that this assumes very clean input, with no leading slash, and
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it will not evaluate '..' components.
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void GetComponents(
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& path,
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::vector<base::FilePath::StringType>* components);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a path name ensuring that it begins with kRoot and all path
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // separators are forward slashes /.
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static base::FilePath::StringType GetNormalizedFilePath(
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::FilePath& path);
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the given path begins with kRoot.
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool IsAbsolute(const base::FilePath::StringType& path);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the root URI of the filesystem that can be specified by a pair of
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |origin_url| and |type|.  The returned URI can be used as a root path
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the filesystem (e.g. <returned_URI> + "/relative/path" will compose
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a path pointing to the entry "/relative/path" in the filesystem).
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For Isolated filesystem this returns the 'common' root part, e.g.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returns URL without the filesystem ID.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |type| needs to be public type as the returned URI is given to the renderer.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT GURL GetFileSystemRootURI(const GURL& origin_url,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         FileSystemType type);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the name for the filesystem that is specified by a pair of
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |origin_url| and |type|.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (The name itself is neither really significant nor a formal identifier
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// but can be read as the .name field of the returned FileSystem object
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as a user-friendly name in the javascript layer).
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |type| needs to be public type as the returned name is given to the renderer.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example:
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   The name for a TEMPORARY filesystem of "http://www.example.com:80/"
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   should look like: "http_www.example.host_80:temporary"
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT std::string GetFileSystemName(const GURL& origin_url,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             FileSystemType type);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts FileSystemType |type| to/from the StorageType |storage_type| that
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is used for the unified quota system.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (Basically this naively maps TEMPORARY storage type to TEMPORARY filesystem
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// type, PERSISTENT storage type to PERSISTENT filesystem type and vice versa.)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT FileSystemType QuotaStorageTypeToFileSystemType(
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    quota::StorageType storage_type);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT quota::StorageType FileSystemTypeToQuotaStorageType(
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FileSystemType type);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the string representation of the given filesystem |type|.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns an empty string if the |type| is invalid.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT std::string GetFileSystemTypeString(FileSystemType type);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Sets type to FileSystemType enum that corresponds to the string name.
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns false if the |type_string| is invalid.
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)WEBKIT_STORAGE_EXPORT bool GetFileSystemPublicType(
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string type_string,
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WebKit::WebFileSystemType* type);
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encodes |file_path| to a string.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Following conditions should be held:
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  - StringToFilePath(FilePathToString(path)) == path
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  - StringToFilePath(FilePathToString(path) + "/" + "SubDirectory") ==
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    path.AppendASCII("SubDirectory");
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(tzik): Replace CreateFilePath and FilePathToString in
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// third_party/leveldatabase/env_chromium.cc with them.
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)WEBKIT_STORAGE_EXPORT std::string FilePathToString(
11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const base::FilePath& file_path);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Decode a file path from |file_path_string|.
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)WEBKIT_STORAGE_EXPORT base::FilePath StringToFilePath(
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& file_path_string);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// File error conversion
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT WebKit::WebFileError PlatformFileErrorToWebFileError(
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::PlatformFileError error_code);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generate a file system name for the given arguments. Should only be used by
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform apps.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT std::string GetIsolatedFileSystemName(
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin_url,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& filesystem_id);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Find the file system id from |filesystem_name|. Should only be used by
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platform apps. This function will return false if the file system name is
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not of the form {origin}:Isolated_{id}, and will also check that there is an
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// origin and id present. It will not check that the origin or id are valid.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WEBKIT_STORAGE_EXPORT bool CrackIsolatedFileSystemName(
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& filesystem_name,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* filesystem_id);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns the root URI for an isolated filesystem for origin |origin_url|
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// and |filesystem_id|. If the |optional_root_name| is given the resulting
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// root URI will point to the subfolder within the isolated filesystem.
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)WEBKIT_STORAGE_EXPORT std::string GetIsolatedFileSystemRootURIString(
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& origin_url,
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& filesystem_id,
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& optional_root_name);
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace fileapi
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif  // WEBKIT_COMMON_FILEAPI_FILE_SYSTEM_UTIL_H_
150