1761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes/* 2761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * Copyright (C) 2009 The Android Open Source Project 3761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * 4761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * you may not use this file except in compliance with the License. 6761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * You may obtain a copy of the License at 7761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * 8761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * 10761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * See the License for the specific language governing permissions and 14761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes * limitations under the License. 15761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes */ 16761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_H_ 18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_H_ 19761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 20761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include <stdint.h> 21761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 22761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughesnamespace unix_file { 23761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 24761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// A file interface supporting random-access reading and writing of content, 25761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// along with the ability to set the length of a file (smaller or greater than 26761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// its current extent). 27761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// 28761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// This interface does not support a stream position (i.e. every read or write 29761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// must specify an offset). This interface does not imply any buffering policy. 30761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// 31761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// All operations return >= 0 on success or -errno on failure. 32761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// 33761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// Implementations never return EINTR; callers are spared the need to manually 34761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// retry interrupted operations. 35761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// 36761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// Any concurrent access to files should be externally synchronized. 37761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughesclass RandomAccessFile { 38761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes public: 39761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual ~RandomAccessFile() { } 40761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 41761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual int Close() = 0; 42761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 43761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // Reads 'byte_count' bytes into 'buf' starting at offset 'offset' in the 44761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // file. Returns the number of bytes actually read. 45761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const = 0; 46761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 47761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // Sets the length of the file to 'new_length'. If this is smaller than the 48761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // file's current extent, data is discarded. If this is greater than the 49761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // file's current extent, it is as if a write of the relevant number of zero 50761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // bytes occurred. Returns 0 on success. 51761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual int SetLength(int64_t new_length) = 0; 52761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 53761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // Returns the current size of this file. 54761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual int64_t GetLength() const = 0; 55761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 56761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // Writes 'byte_count' bytes from 'buf' starting at offset 'offset' in the 57761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // file. Zero-byte writes are acceptable, and writes past the end are as if 58761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // a write of the relevant number of zero bytes also occurred. Returns the 59761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // number of bytes actually written. 60761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset) = 0; 61761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 62761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes // Flushes file data. 63761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes virtual int Flush() = 0; 64761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes}; 65761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 66761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes} // namespace unix_file 67761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes 68fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_H_ 69