Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 CROS_DISKS_PLATFORM_H_ |
| 6 | #define CROS_DISKS_PLATFORM_H_ |
| 7 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 8 | #include <sys/stat.h> |
| 9 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include <base/basictypes.h> |
| 13 | |
| 14 | namespace cros_disks { |
| 15 | |
| 16 | // A class that provides functionalities such as creating and removing |
| 17 | // directories, and getting user ID and group ID for a username. |
| 18 | class Platform { |
| 19 | public: |
| 20 | Platform(); |
| 21 | virtual ~Platform(); |
| 22 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 23 | // Creates a directory at |path| if it does not exist. Returns true on |
| 24 | // success. |
| 25 | virtual bool CreateDirectory(const std::string& path) const; |
| 26 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 27 | // Creates a directory at |path| if it does not exist. If |path| already |
| 28 | // exists and is a directory, this function tries to reuse it if it is empty |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 29 | // not in use. The created directory is only accessible by the current user. |
| 30 | // Returns true if the directory is created successfully. |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 31 | virtual bool CreateOrReuseEmptyDirectory(const std::string& path) const; |
| 32 | |
| 33 | // Creates a directory at |path| similar to CreateDirectory() but retries |
| 34 | // on failure by augmenting a numeric suffix (e.g. "mydir (1)"), starting |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 35 | // from 1 to |max_suffix_to_retry|, to the directory name. The created |
| 36 | // directory is only accessible by the current user. Returns true if the |
| 37 | // directory is created successfully. |
| 38 | virtual bool CreateOrReuseEmptyDirectoryWithFallback( |
| 39 | std::string* path, unsigned max_suffix_to_retry) const; |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 40 | |
| 41 | // Gets the user ID and group ID for a given username. Returns true on |
| 42 | // success. |
| 43 | virtual bool GetUserAndGroupId(const std::string& username, |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 44 | uid_t *uid, gid_t *gid) const; |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 45 | |
| 46 | // Removes a directory at |path| if it is empty and not in use. |
| 47 | // Returns true on success. |
| 48 | virtual bool RemoveEmptyDirectory(const std::string& path) const; |
| 49 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 50 | // Sets the user ID and group ID of |path| to |user_id| and |group_id|, |
| 51 | // respectively. Returns true on success. |
| 52 | virtual bool SetOwnership(const std::string& path, |
| 53 | uid_t user_id, gid_t group_id) const; |
| 54 | |
| 55 | // Sets the permissions of |path| to |mode|. Returns true on success. |
| 56 | virtual bool SetPermissions(const std::string& path, mode_t mode) const; |
| 57 | |
| 58 | // Unmounts |path|. Returns true on success. |
| 59 | virtual bool Unmount(const std::string& path) const; |
| 60 | |
| 61 | bool experimental_features_enabled() const { |
| 62 | return experimental_features_enabled_; |
| 63 | } |
| 64 | void set_experimental_features_enabled(bool experimental_features_enabled) { |
| 65 | experimental_features_enabled_ = experimental_features_enabled; |
| 66 | } |
| 67 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 68 | private: |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 69 | // This variable is set to true if the experimental features are enabled. |
| 70 | bool experimental_features_enabled_; |
| 71 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 72 | DISALLOW_COPY_AND_ASSIGN(Platform); |
| 73 | }; |
| 74 | |
| 75 | } // namespace cros_disks |
| 76 | |
| 77 | #endif // CROS_DISKS_PLATFORM_H_ |