blob: 8db2ee4a8151b6f2d02e0bcdfba9c9c4a164cb85 [file] [log] [blame]
Elliott Hughes76160052012-12-12 16:31:20 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_
18#define ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_
Elliott Hughes76160052012-12-12 16:31:20 -080019
20#include <fcntl.h>
21#include <string>
22#include "base/unix_file/random_access_file.h"
23#include "base/macros.h"
24
25namespace unix_file {
26
Andreas Gampe4303ba92014-11-06 01:00:46 -080027// If true, check whether Flush and Close are called before destruction.
28static constexpr bool kCheckSafeUsage = true;
29
Elliott Hughes76160052012-12-12 16:31:20 -080030// A RandomAccessFile implementation backed by a file descriptor.
31//
32// Not thread safe.
33class FdFile : public RandomAccessFile {
34 public:
35 FdFile();
36 // Creates an FdFile using the given file descriptor. Takes ownership of the
37 // file descriptor. (Use DisableAutoClose to retain ownership.)
Andreas Gampe4303ba92014-11-06 01:00:46 -080038 explicit FdFile(int fd, bool checkUsage);
39 explicit FdFile(int fd, const std::string& path, bool checkUsage);
Elliott Hughes76160052012-12-12 16:31:20 -080040
41 // Destroys an FdFile, closing the file descriptor if Close hasn't already
42 // been called. (If you care about the return value of Close, call it
43 // yourself; this is meant to handle failure cases and read-only accesses.
44 // Note though that calling Close and checking its return value is still no
45 // guarantee that data actually made it to stable storage.)
46 virtual ~FdFile();
47
48 // Opens file 'file_path' using 'flags' and 'mode'.
49 bool Open(const std::string& file_path, int flags);
50 bool Open(const std::string& file_path, int flags, mode_t mode);
51
52 // RandomAccessFile API.
Andreas Gampe4303ba92014-11-06 01:00:46 -080053 virtual int Close() WARN_UNUSED;
54 virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const WARN_UNUSED;
55 virtual int SetLength(int64_t new_length) WARN_UNUSED;
Elliott Hughes76160052012-12-12 16:31:20 -080056 virtual int64_t GetLength() const;
Andreas Gampe4303ba92014-11-06 01:00:46 -080057 virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset) WARN_UNUSED;
58 virtual int Flush() WARN_UNUSED;
59
60 // Short for SetLength(0); Flush(); Close();
61 void Erase();
62
63 // Try to Flush(), then try to Close(); If either fails, call Erase().
64 int FlushCloseOrErase() WARN_UNUSED;
65
66 // Try to Flush and Close(). Attempts both, but returns the first error.
67 int FlushClose() WARN_UNUSED;
Elliott Hughes76160052012-12-12 16:31:20 -080068
69 // Bonus API.
70 int Fd() const;
71 bool IsOpened() const;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070072 const std::string& GetPath() const {
73 return file_path_;
74 }
Elliott Hughes76160052012-12-12 16:31:20 -080075 void DisableAutoClose();
Andreas Gampe4303ba92014-11-06 01:00:46 -080076 bool ReadFully(void* buffer, size_t byte_count) WARN_UNUSED;
77 bool WriteFully(const void* buffer, size_t byte_count) WARN_UNUSED;
78
79 // This enum is public so that we can define the << operator over it.
80 enum class GuardState {
81 kBase, // Base, file has not been flushed or closed.
82 kFlushed, // File has been flushed, but not closed.
83 kClosed, // File has been flushed and closed.
84 kNoCheck // Do not check for the current file instance.
85 };
86
87 protected:
88 // If the guard state indicates checking (!=kNoCheck), go to the target state "target". Print the
89 // given warning if the current state is or exceeds warn_threshold.
90 void moveTo(GuardState target, GuardState warn_threshold, const char* warning);
91
92 // If the guard state indicates checking (<kNoCheck), and is below the target state "target", go
93 // to "target." If the current state is higher (excluding kNoCheck) than the trg state, print the
94 // warning.
95 void moveUp(GuardState target, const char* warning);
96
97 // Forcefully sets the state to the given one. This can overwrite kNoCheck.
98 void resetGuard(GuardState new_state) {
99 if (kCheckSafeUsage) {
100 guard_state_ = new_state;
101 }
102 }
103
104 GuardState guard_state_;
Elliott Hughes76160052012-12-12 16:31:20 -0800105
106 private:
107 int fd_;
108 std::string file_path_;
109 bool auto_close_;
110
111 DISALLOW_COPY_AND_ASSIGN(FdFile);
112};
113
Andreas Gampe4303ba92014-11-06 01:00:46 -0800114std::ostream& operator<<(std::ostream& os, const FdFile::GuardState& kind);
115
Elliott Hughes76160052012-12-12 16:31:20 -0800116} // namespace unix_file
117
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700118#endif // ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_