fd_file.h revision 761600567d73b23324ae0251e871c15d6849ffd8
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
17761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#ifndef BASE_UNIX_FILE_FD_FILE_H_
18761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#define BASE_UNIX_FILE_FD_FILE_H_
19761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
20761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include <fcntl.h>
21761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include <string>
22761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/random_access_file.h"
23761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
24761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
25761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughesnamespace unix_file {
26761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
27761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// A RandomAccessFile implementation backed by a file descriptor.
28761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes//
29761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes// Not thread safe.
30761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughesclass FdFile : public RandomAccessFile {
31761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes public:
32761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  FdFile();
33761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // Creates an FdFile using the given file descriptor. Takes ownership of the
34761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // file descriptor. (Use DisableAutoClose to retain ownership.)
35761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  explicit FdFile(int fd);
36761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  explicit FdFile(int fd, const std::string& path);
37761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
38761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // Destroys an FdFile, closing the file descriptor if Close hasn't already
39761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // been called. (If you care about the return value of Close, call it
40761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // yourself; this is meant to handle failure cases and read-only accesses.
41761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // Note though that calling Close and checking its return value is still no
42761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // guarantee that data actually made it to stable storage.)
43761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual ~FdFile();
44761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
45761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // Opens file 'file_path' using 'flags' and 'mode'.
46761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  bool Open(const std::string& file_path, int flags);
47761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  bool Open(const std::string& file_path, int flags, mode_t mode);
48761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
49761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // RandomAccessFile API.
50761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual int Close();
51761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const;
52761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual int SetLength(int64_t new_length);
53761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual int64_t GetLength() const;
54761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset);
55761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  virtual int Flush();
56761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
57761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  // Bonus API.
58761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  int Fd() const;
59761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  bool IsOpened() const;
60761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  std::string GetPath() const;
61761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  void DisableAutoClose();
62761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  bool ReadFully(void* buffer, int64_t byte_count);
63761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  bool WriteFully(const void* buffer, int64_t byte_count);
64761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
65761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes private:
66761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  int fd_;
67761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  std::string file_path_;
68761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  bool auto_close_;
69761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
70761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(FdFile);
71761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes};
72761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
73761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes}  // namespace unix_file
74761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes
75761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#endif  // BASE_UNIX_FILE_FD_FILE_H_
76