blob: 4d6e970b408ea0f8ca5909a7b22ece99f26d2efe [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 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//
Gilad Arnold11c066f2012-05-10 14:37:25 -070016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_FILE_DESCRIPTOR_H_
18#define UPDATE_ENGINE_FILE_DESCRIPTOR_H_
Gilad Arnold11c066f2012-05-10 14:37:25 -070019
20#include <errno.h>
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080021#include <memory>
Gilad Arnold11c066f2012-05-10 14:37:25 -070022#include <sys/types.h>
23
Alex Deymo8427b4a2014-11-05 14:00:32 -080024#include <base/logging.h>
Gilad Arnold11c066f2012-05-10 14:37:25 -070025
Gilad Arnold11c066f2012-05-10 14:37:25 -070026// Abstraction for managing opening, reading, writing and closing of file
27// descriptors. This includes an abstract class and one standard implementation
28// based on POSIX system calls.
29//
30// TODO(garnold) this class is modeled after (and augments the functionality of)
31// the FileWriter class; ultimately, the latter should be replaced by the former
32// throughout the codebase. A few deviations from the original FileWriter:
33//
34// * Providing two flavors of Open()
35//
36// * A FileDescriptor is reusable and can be used to read/write multiple files
37// as long as open/close preconditions are respected.
38//
39// * Write() returns the number of bytes written: this appears to be more useful
40// for clients, who may wish to retry or otherwise do something useful with
41// the remaining data that was not written.
Gilad Arnold6eccc532012-05-17 15:44:22 -070042//
43// * Provides a Reset() method, which will force to abandon a currently open
44// file descriptor and allow opening another file, without necessarily
45// properly closing the old one. This may be useful in cases where a "closer"
46// class does not care whether Close() was successful, but may need to reuse
47// the same file descriptor again.
Gilad Arnold11c066f2012-05-10 14:37:25 -070048
49namespace chromeos_update_engine {
50
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080051class FileDescriptor;
52using FileDescriptorPtr = std::shared_ptr<FileDescriptor>;
53
Gilad Arnold11c066f2012-05-10 14:37:25 -070054// An abstract class defining the file descriptor API.
55class FileDescriptor {
56 public:
57 FileDescriptor() {}
58 virtual ~FileDescriptor() {}
59
60 // Opens a file descriptor. The descriptor must be in the closed state prior
61 // to this call. Returns true on success, false otherwise. Specific
62 // implementations may set errno accordingly.
63 virtual bool Open(const char* path, int flags, mode_t mode) = 0;
64 virtual bool Open(const char* path, int flags) = 0;
65
66 // Reads from a file descriptor up to a given count. The descriptor must be
67 // open prior to this call. Returns the number of bytes read, or -1 on error.
68 // Specific implementations may set errno accordingly.
69 virtual ssize_t Read(void* buf, size_t count) = 0;
70
71 // Writes to a file descriptor. The descriptor must be open prior to this
72 // call. Returns the number of bytes written, or -1 if an error occurred and
73 // no bytes were written. Specific implementations may set errno accordingly.
74 virtual ssize_t Write(const void* buf, size_t count) = 0;
75
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080076 // Seeks to an offset. Returns the resulting offset location as measured in
77 // bytes from the beginning. On error, return -1. Specific implementations
78 // may set errno accordingly.
79 virtual off64_t Seek(off64_t offset, int whence) = 0;
80
Gilad Arnold6eccc532012-05-17 15:44:22 -070081 // Closes a file descriptor. The descriptor must be open prior to this call.
Gilad Arnold11c066f2012-05-10 14:37:25 -070082 // Returns true on success, false otherwise. Specific implementations may set
83 // errno accordingly.
84 virtual bool Close() = 0;
85
Gilad Arnold6eccc532012-05-17 15:44:22 -070086 // Resets the file descriptor, abandoning a currently open file and returning
87 // the descriptor to the closed state.
88 virtual void Reset() = 0;
89
Gilad Arnold11c066f2012-05-10 14:37:25 -070090 // Indicates whether or not an implementation sets meaningful errno.
91 virtual bool IsSettingErrno() = 0;
92
Gilad Arnold6eccc532012-05-17 15:44:22 -070093 // Indicates whether the descriptor is currently open.
94 virtual bool IsOpen() = 0;
95
Gilad Arnold11c066f2012-05-10 14:37:25 -070096 private:
97 DISALLOW_COPY_AND_ASSIGN(FileDescriptor);
98};
99
100// A simple EINTR-immune wrapper implementation around standard system calls.
101class EintrSafeFileDescriptor : public FileDescriptor {
102 public:
103 EintrSafeFileDescriptor() : fd_(-1) {}
Gilad Arnold11c066f2012-05-10 14:37:25 -0700104
105 // Interface methods.
Alex Deymo610277e2014-11-11 21:18:11 -0800106 bool Open(const char* path, int flags, mode_t mode) override;
107 bool Open(const char* path, int flags) override;
108 ssize_t Read(void* buf, size_t count) override;
109 ssize_t Write(const void* buf, size_t count) override;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800110 off64_t Seek(off64_t offset, int whence) override;
Alex Deymo610277e2014-11-11 21:18:11 -0800111 bool Close() override;
112 void Reset() override;
113 bool IsSettingErrno() override {
Gilad Arnold11c066f2012-05-10 14:37:25 -0700114 return true;
115 }
Alex Deymo610277e2014-11-11 21:18:11 -0800116 bool IsOpen() override {
Gilad Arnold6eccc532012-05-17 15:44:22 -0700117 return (fd_ >= 0);
118 }
Gilad Arnold11c066f2012-05-10 14:37:25 -0700119
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800120 protected:
Gilad Arnold11c066f2012-05-10 14:37:25 -0700121 int fd_;
122};
123
Gilad Arnold11c066f2012-05-10 14:37:25 -0700124} // namespace chromeos_update_engine
125
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700126#endif // UPDATE_ENGINE_FILE_DESCRIPTOR_H_