blob: efadac08d2544ba50455d6c7345303369aa35328 [file] [log] [blame]
Zonr Chang4f94c522012-04-05 15:09:28 +08001/*
2 * Copyright 2012, 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
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#ifndef BCC_SUPPORT_FILE_BASE_H
18#define BCC_SUPPORT_FILE_BASE_H
Zonr Chang4f94c522012-04-05 15:09:28 +080019
20#include <fcntl.h>
Tim Murrayc2074ca2014-04-08 15:39:08 -070021#include <unistd.h>
Zonr Chang4f94c522012-04-05 15:09:28 +080022#include <string>
Stephen Hinesd0993af2014-07-15 16:49:25 -070023#include <system_error>
Zonr Chang4f94c522012-04-05 15:09:28 +080024
25namespace android {
26 class FileMap;
27}
28
29namespace bcc {
30
31class FileBase {
32public:
33 enum OpenModeEnum {
34 kReadMode = 1 << 0,
35 kWriteMode = 1 << 1,
36 kReadWriteMode = (kReadMode | kWriteMode),
37 };
38
39 enum FlagEnum {
Zonr Chang4f94c522012-04-05 15:09:28 +080040 kBinary = 1 << 0,
Shih-wei Liaoc02eae62012-07-22 16:23:32 -070041 kTruncate = 1 << 1,
Shih-wei Liaob2b8c642012-07-22 16:25:02 -070042 kAppend = 1 << 2,
Stephen Hines331310e2012-10-26 19:27:55 -070043 kDeleteOnClose = 1 << 3
Zonr Chang4f94c522012-04-05 15:09:28 +080044 };
45
46 enum LockModeEnum {
47 // The shared resource behind the Stream locked in ReadLock mode can be
48 // locked by other processes at the same time.
49 kReadLock,
50
51 // The shared resource behind the Stream locked in WriteLock mode can only
52 // be locked by one process. It's exclusive. That is, the shared resource
53 // cannot have both ReadLock and WriteLock simultaneously.
54 kWriteLock
55 };
56
57 // Default configuration to the lock().
58 enum {
59 kDefaultMaxRetryLock = 4,
60 kDefaultRetryLockInterval = 200000UL,
61 };
62
63protected:
64 // Grant direct access of the internal file descriptor to the sub-class and
65 // error message such that they can implement their own I/O functionality.
66 int mFD;
67
Stephen Hinesd0993af2014-07-15 16:49:25 -070068 std::error_code mError;
Zonr Chang4f94c522012-04-05 15:09:28 +080069
70private:
71 std::string mName;
72
73 // The 2nd argument to the POSIX open().
74 unsigned mOpenFlags;
75
76 // True true if we should call unlock() in destructor.
77 bool mShouldUnlock;
78
Stephen Hines331310e2012-10-26 19:27:55 -070079 // True if file should be deleted in destructor.
80 bool mShouldDelete;
81
Zonr Chang4f94c522012-04-05 15:09:28 +080082 // Open mName with flag mOpenFlags (using POSIX open().)
83 bool open();
84
85 // Return true if mFD is the corresponded file descriptor to the file named
86 // mName on the filesystem. This check may returns failed, for example,
87 // someone re-create the file with the same name after we openning the file.
88 bool checkFileIntegrity();
89
90 inline bool reopen() {
91 // It's a private method, and all its callers are the few that can invoke it.
92 // That is, the pre-condition will be checked by the caller. Therefore, we don't
93 // need to check it again in reopen().
94 close();
95 return open();
96 }
97
98private:
99 FileBase(FileBase &); // Do not implement.
100 void operator=(const FileBase &); // Do not implement.
101
102protected:
103 // pOpenFlags is the 2nd argument to the POSIX open(). pFlags are the flags to
104 // FileBase. It's a bit set composed by the value defined in
105 // FileBase::FlagEnum.
106 FileBase(const std::string &pFilename, unsigned pOpenFlags, unsigned pFlags);
107
108 void detectError();
109
110public:
111 // Lock the file descriptor in given pMode. If pNonblocking is true, the lock
112 // request issued will return immediately when the shared resource is locked.
113 // In this case, it retries pMaxRetry times, each wait pRetryInterval (in
114 // usecs) before the previous retry getting done.
115 //
116 // Only file is allowed to use this API.
117 bool lock(enum LockModeEnum pMode, bool pNonblocking = true,
118 unsigned pMaxRetry = kDefaultMaxRetryLock,
119 useconds_t pRetryInterval = kDefaultRetryLockInterval);
120
121 void unlock();
122
Zonr Chang4f94c522012-04-05 15:09:28 +0800123 inline bool hasError() const
Stephen Hinesd0993af2014-07-15 16:49:25 -0700124 { return (bool) mError; }
Zonr Chang4f94c522012-04-05 15:09:28 +0800125
Stephen Hinesd0993af2014-07-15 16:49:25 -0700126 inline const std::error_code &getError() const
Zonr Chang4f94c522012-04-05 15:09:28 +0800127 { return mError; }
128
Stephen Hinesd0993af2014-07-15 16:49:25 -0700129 // The return value of std::error_code::message() is obtained upon the call
130 // and is passed by value (that is, it's not a member of std::error_code.)
Zonr Chang4f94c522012-04-05 15:09:28 +0800131 inline std::string getErrorMessage() const
132 { return mError.message(); }
133
Zonr Chang4f94c522012-04-05 15:09:28 +0800134 void close();
135
136 virtual ~FileBase();
137};
138
139} // end namespace bcc
140
Zonr Changc72c4dd2012-04-12 15:38:53 +0800141#endif // BCC_SUPPORT_FILE_BASE_H