file_system_util.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
7
8#include <string>
9
10#include "base/callback_forward.h"
11#include "chrome/browser/chromeos/drive/file_errors.h"
12#include "url/gurl.h"
13
14class Profile;
15
16namespace base {
17class FilePath;
18}
19
20namespace fileapi {
21class FileSystemURL;
22}
23
24namespace drive {
25
26class FileSystemInterface;
27class ResourceEntry;
28
29namespace util {
30
31// Path constants.
32
33// Name of the directory used to store metadata.
34const base::FilePath::CharType kMetadataDirectory[] = FILE_PATH_LITERAL("meta");
35
36// Name of the directory used to store cached files.
37const base::FilePath::CharType kCacheFileDirectory[] =
38    FILE_PATH_LITERAL("files");
39
40// Name of the directory used to store temporary files.
41const base::FilePath::CharType kTemporaryFileDirectory[] =
42    FILE_PATH_LITERAL("tmp");
43
44// Special resource IDs introduced to manage pseudo directory tree locally.
45// These strings are supposed to be different from any resource ID used on the
46// server, and are never sent to the server. Practical resource IDs used so far
47// have only alphabets/numbers ([a-zA-Z0-9]) and ':'.
48// Hence '<' and '>' around the directory name have been added to make them
49// different from normal server-side IDs.
50const char kDriveGrandRootSpecialResourceId[] = "<drive>";
51
52const char kDriveOtherDirSpecialResourceId[] = "<other>";
53
54// The directory names used for the Google Drive file system tree. These names
55// are used in URLs for the file manager, hence user-visible.
56const base::FilePath::CharType kDriveGrandRootDirName[] =
57    FILE_PATH_LITERAL("drive");
58
59const base::FilePath::CharType kDriveMyDriveRootDirName[] =
60    FILE_PATH_LITERAL("root");
61
62const base::FilePath::CharType kDriveOtherDirName[] =
63    FILE_PATH_LITERAL("other");
64
65// Returns the path of the top root of the pseudo tree.
66const base::FilePath& GetDriveGrandRootPath();
67
68// Returns the path of the directory representing "My Drive".
69const base::FilePath& GetDriveMyDriveRootPath();
70
71// Returns the Drive mount point path, which looks like "/special/drive".
72const base::FilePath& GetDriveMountPointPath();
73
74// Returns a FileSystemInterface instance for the |profile_id|, or NULL
75// if the Profile for |profile_id| is destructed or Drive File System is
76// disabled for the profile.
77// Note: |profile_id| should be the pointer of the Profile instance if it is
78// alive. Considering timing issues due to task posting across threads,
79// this function can accept a dangling pointer as |profile_id| (and will return
80// NULL for such a case).
81// This function must be called on UI thread.
82FileSystemInterface* GetFileSystemByProfileId(void* profile_id);
83
84// Checks if the resource ID is a special one, which is effective only in our
85// implementation and is not supposed to be sent to the server.
86bool IsSpecialResourceId(const std::string& resource_id);
87
88// Returns a ResourceEntry for "/drive/root" directory.
89ResourceEntry CreateMyDriveRootEntry(const std::string& root_resource_id);
90
91// Returns a ResourceEntry for "/drive/other" directory.
92ResourceEntry CreateOtherDirEntry();
93
94// Returns the Drive mount path as string.
95const std::string& GetDriveMountPointPathAsString();
96
97// Returns the gdata file resource url formatted as "drive:<path>"
98GURL FilePathToDriveURL(const base::FilePath& path);
99
100// Converts a drive: URL back to a path that can be passed to FileSystem.
101base::FilePath DriveURLToFilePath(const GURL& url);
102
103// Overwrites |url| with a Drive URL when appropriate.
104void MaybeSetDriveURL(Profile* profile, const base::FilePath& path, GURL* url);
105
106// Returns true if the given path is under the Drive mount point.
107bool IsUnderDriveMountPoint(const base::FilePath& path);
108
109// Returns true if the given path is under the Drive mount point and needs to be
110// migrated to the new namespace. http://crbug.com/174233.
111bool NeedsNamespaceMigration(const base::FilePath& path);
112
113// Returns new FilePath with a namespace "root" inserted at the 3rd component.
114// e.g. "/special/drive/root/dir" for "/special/drive/dir".
115// NeedsNamespaceMigration(path) should be true (after the TODOs are resolved).
116base::FilePath ConvertToMyDriveNamespace(const base::FilePath& path);
117
118// Extracts the Drive path from the given path located under the Drive mount
119// point. Returns an empty path if |path| is not under the Drive mount point.
120// Examples: ExtractDrivePath("/special/drive/foo.txt") => "drive/foo.txt"
121base::FilePath ExtractDrivePath(const base::FilePath& path);
122
123// Extracts the Drive path (e.g., "drive/foo.txt") from the filesystem URL.
124// Returns an empty path if |url| does not point under Drive mount point.
125base::FilePath ExtractDrivePathFromFileSystemUrl(
126    const fileapi::FileSystemURL& url);
127
128// Escapes a file name in Drive cache.
129// Replaces percent ('%'), period ('.') and slash ('/') with %XX (hex)
130std::string EscapeCacheFileName(const std::string& filename);
131
132// Unescapes a file path in Drive cache.
133// This is the inverse of EscapeCacheFileName.
134std::string UnescapeCacheFileName(const std::string& filename);
135
136// Converts the given string to a form suitable as a file name. Specifically,
137// - Normalizes in Unicode Normalization Form C.
138// - Replaces slashes '/' with \u2215 that pretty much looks the same in UI.
139// |input| must be a valid UTF-8 encoded string.
140std::string NormalizeFileName(const std::string& input);
141
142// Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the
143// profile.
144base::FilePath GetCacheRootPath(Profile* profile);
145
146// Migrates cache files from old "persistent" and "tmp" directories to the new
147// "files" directory (see crbug.com/248905).
148// TODO(hashimoto): Remove this function at some point.
149void MigrateCacheFilesFromOldDirectories(
150    const base::FilePath& cache_root_directory);
151
152// Callback type for PrepareWritableFileAndRun.
153typedef base::Callback<void (FileError, const base::FilePath& path)>
154    OpenFileCallback;
155
156// Invokes |callback| on blocking thread pool, after converting virtual |path|
157// string like "/special/drive/foo.txt" to the concrete local cache file path.
158// After |callback| returns, the written content is synchronized to the server.
159//
160// If |path| is not a Drive path, it is regarded as a local path and no path
161// conversion takes place.
162//
163// Must be called from UI thread.
164void PrepareWritableFileAndRun(Profile* profile,
165                               const base::FilePath& path,
166                               const OpenFileCallback& callback);
167
168// Ensures the existence of |directory| of '/special/drive/foo'.  This will
169// create |directory| and its ancestors if they don't exist.  |callback| is
170// invoked after making sure that |directory| exists.  |callback| should
171// interpret error codes of either FILE_ERROR_OK or FILE_ERROR_EXISTS as
172// indicating that |directory| now exists.
173//
174// If |directory| is not a Drive path, it won't check the existence and just
175// runs |callback|.
176//
177// Must be called from UI thread.
178void EnsureDirectoryExists(Profile* profile,
179                           const base::FilePath& directory,
180                           const FileOperationCallback& callback);
181
182// Does nothing with |error|. Used with functions taking FileOperationCallback.
183void EmptyFileOperationCallback(FileError error);
184
185// Helper to destroy objects which needs Destroy() to be called on destruction.
186struct DestroyHelper {
187  template<typename T>
188  void operator()(T* object) const {
189    if (object)
190      object->Destroy();
191  }
192};
193
194// Creates a GDoc file with given values.
195//
196// GDoc files are used to represent hosted documents on local filesystems.
197// A GDoc file contains a JSON whose content is a URL to view the document and
198// a resource ID of the entry.
199bool CreateGDocFile(const base::FilePath& file_path,
200                    const GURL& url,
201                    const std::string& resource_id);
202
203// Returns true if |file_path| has a GDoc file extension. (e.g. ".gdoc")
204bool HasGDocFileExtension(const base::FilePath& file_path);
205
206// Reads URL from a GDoc file.
207GURL ReadUrlFromGDocFile(const base::FilePath& file_path);
208
209// Reads resource ID from a GDoc file.
210std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path);
211
212// Returns the (base-16 encoded) MD5 digest of the file content at |file_path|,
213// or an empty string if an error is found.
214std::string GetMd5Digest(const base::FilePath& file_path);
215
216}  // namespace util
217}  // namespace drive
218
219#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_UTIL_H_
220