Home | History | Annotate | Download | only in unix_file
      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_FD_FILE_H_
     18 #define ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_
     19 
     20 #include <fcntl.h>
     21 #include <string>
     22 #include "base/unix_file/random_access_file.h"
     23 #include "base/macros.h"
     24 
     25 namespace unix_file {
     26 
     27 // A RandomAccessFile implementation backed by a file descriptor.
     28 //
     29 // Not thread safe.
     30 class FdFile : public RandomAccessFile {
     31  public:
     32   FdFile();
     33   // Creates an FdFile using the given file descriptor. Takes ownership of the
     34   // file descriptor. (Use DisableAutoClose to retain ownership.)
     35   explicit FdFile(int fd);
     36   explicit FdFile(int fd, const std::string& path);
     37 
     38   // Destroys an FdFile, closing the file descriptor if Close hasn't already
     39   // been called. (If you care about the return value of Close, call it
     40   // yourself; this is meant to handle failure cases and read-only accesses.
     41   // Note though that calling Close and checking its return value is still no
     42   // guarantee that data actually made it to stable storage.)
     43   virtual ~FdFile();
     44 
     45   // Opens file 'file_path' using 'flags' and 'mode'.
     46   bool Open(const std::string& file_path, int flags);
     47   bool Open(const std::string& file_path, int flags, mode_t mode);
     48 
     49   // RandomAccessFile API.
     50   virtual int Close();
     51   virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const;
     52   virtual int SetLength(int64_t new_length);
     53   virtual int64_t GetLength() const;
     54   virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset);
     55   virtual int Flush();
     56 
     57   // Bonus API.
     58   int Fd() const;
     59   bool IsOpened() const;
     60   std::string GetPath() const;
     61   void DisableAutoClose();
     62   bool ReadFully(void* buffer, int64_t byte_count);
     63   bool WriteFully(const void* buffer, int64_t byte_count);
     64 
     65  private:
     66   int fd_;
     67   std::string file_path_;
     68   bool auto_close_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(FdFile);
     71 };
     72 
     73 }  // namespace unix_file
     74 
     75 #endif  // ART_RUNTIME_BASE_UNIX_FILE_FD_FILE_H_
     76