blob: 47024f75d510f8173732811a465ee62156adaeed [file] [log] [blame]
Ben Chan29be9152011-07-25 14:39:48 -07001// 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 Chan8dcede82011-07-25 20:56:13 -07008#include <sys/stat.h>
9
Ben Chan29be9152011-07-25 14:39:48 -070010#include <string>
11
12#include <base/basictypes.h>
13
14namespace 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.
18class Platform {
19 public:
20 Platform();
21 virtual ~Platform();
22
Ben Chan8dcede82011-07-25 20:56:13 -070023 // 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 Chan29be9152011-07-25 14:39:48 -070027 // 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 Chan8dcede82011-07-25 20:56:13 -070029 // not in use. The created directory is only accessible by the current user.
30 // Returns true if the directory is created successfully.
Ben Chan29be9152011-07-25 14:39:48 -070031 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 Chan8dcede82011-07-25 20:56:13 -070035 // 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 Chan29be9152011-07-25 14:39:48 -070040
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 Chan8dcede82011-07-25 20:56:13 -070044 uid_t *uid, gid_t *gid) const;
Ben Chan29be9152011-07-25 14:39:48 -070045
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 Chan8dcede82011-07-25 20:56:13 -070050 // 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 Chan29be9152011-07-25 14:39:48 -070068 private:
Ben Chan8dcede82011-07-25 20:56:13 -070069 // This variable is set to true if the experimental features are enabled.
70 bool experimental_features_enabled_;
71
Ben Chan29be9152011-07-25 14:39:48 -070072 DISALLOW_COPY_AND_ASSIGN(Platform);
73};
74
75} // namespace cros_disks
76
77#endif // CROS_DISKS_PLATFORM_H_