1/*
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
17#ifndef ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_H_
18#define ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_H_
19
20#include <stdint.h>
21
22namespace unix_file {
23
24// A file interface supporting random-access reading and writing of content,
25// along with the ability to set the length of a file (smaller or greater than
26// its current extent).
27//
28// This interface does not support a stream position (i.e. every read or write
29// must specify an offset). This interface does not imply any buffering policy.
30//
31// All operations return >= 0 on success or -errno on failure.
32//
33// Implementations never return EINTR; callers are spared the need to manually
34// retry interrupted operations.
35//
36// Any concurrent access to files should be externally synchronized.
37class RandomAccessFile {
38 public:
39  virtual ~RandomAccessFile() { }
40
41  virtual int Close() = 0;
42
43  // Reads 'byte_count' bytes into 'buf' starting at offset 'offset' in the
44  // file. Returns the number of bytes actually read.
45  virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const = 0;
46
47  // Sets the length of the file to 'new_length'. If this is smaller than the
48  // file's current extent, data is discarded. If this is greater than the
49  // file's current extent, it is as if a write of the relevant number of zero
50  // bytes occurred. Returns 0 on success.
51  virtual int SetLength(int64_t new_length) = 0;
52
53  // Returns the current size of this file.
54  virtual int64_t GetLength() const = 0;
55
56  // Writes 'byte_count' bytes from 'buf' starting at offset 'offset' in the
57  // file. Zero-byte writes are acceptable, and writes past the end are as if
58  // a write of the relevant number of zero bytes also occurred. Returns the
59  // number of bytes actually written.
60  virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset) = 0;
61
62  // Flushes file data.
63  virtual int Flush() = 0;
64};
65
66}  // namespace unix_file
67
68#endif  // ART_RUNTIME_BASE_UNIX_FILE_RANDOM_ACCESS_FILE_H_
69