blob: 3652b86f4b53d72c6898a324e6562368e527fc90 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_FILE_WRITER_H_
18#define UPDATE_ENGINE_FILE_WRITER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +000019
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <fcntl.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070021#include <sys/stat.h>
22#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000023#include <unistd.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070024
25#include <base/logging.h>
26
27#include "update_engine/error_code.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000028#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
30// FileWriter is a class that is used to (synchronously, for now) write to
31// a file. This file is a thin wrapper around open/write/close system calls,
32// but provides and interface that can be customized by subclasses that wish
33// to filter the data.
34
35namespace chromeos_update_engine {
36
37class FileWriter {
38 public:
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070039 FileWriter() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000040 virtual ~FileWriter() {}
41
42 // Wrapper around open. Returns 0 on success or -errno on error.
43 virtual int Open(const char* path, int flags, mode_t mode) = 0;
44
Don Garrette410e0f2011-11-10 15:39:01 -080045 // Wrapper around write. Returns true if all requested bytes
Alex Vakulenko072359c2014-07-18 11:41:07 -070046 // were written, or false on any error, regardless of progress.
Don Garrette410e0f2011-11-10 15:39:01 -080047 virtual bool Write(const void* bytes, size_t count) = 0;
rspangler@google.com49fdf182009-10-10 00:57:34 +000048
Jay Srinivasan51dcf262012-09-13 17:24:32 -070049 // Same as the Write method above but returns a detailed |error| code
50 // in addition if the returned value is false. By default this method
51 // returns kActionExitDownloadWriteError as the error code, but subclasses
52 // can override if they wish to return more specific error codes.
53 virtual bool Write(const void* bytes,
54 size_t count,
David Zeuthena99981f2013-04-29 13:42:47 -070055 ErrorCode* error) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070056 *error = ErrorCode::kDownloadWriteError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -070057 return Write(bytes, count);
58 }
59
rspangler@google.com49fdf182009-10-10 00:57:34 +000060 // Wrapper around close. Returns 0 on success or -errno on error.
61 virtual int Close() = 0;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070062
63 private:
64 DISALLOW_COPY_AND_ASSIGN(FileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000065};
66
67// Direct file writer is probably the simplest FileWriter implementation.
68// It calls the system calls directly.
69
70class DirectFileWriter : public FileWriter {
71 public:
72 DirectFileWriter() : fd_(-1) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000073
Alex Deymo610277e2014-11-11 21:18:11 -080074 int Open(const char* path, int flags, mode_t mode) override;
75 bool Write(const void* bytes, size_t count) override;
76 int Close() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000077
adlr@google.comc98a7ed2009-12-04 18:54:03 +000078 int fd() const { return fd_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000079
80 private:
81 int fd_;
Darin Petkove971f332010-09-22 16:57:25 -070082
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070083 DISALLOW_COPY_AND_ASSIGN(DirectFileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000084};
85
adlr@google.comc98a7ed2009-12-04 18:54:03 +000086class ScopedFileWriterCloser {
87 public:
88 explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {}
89 ~ScopedFileWriterCloser() {
90 int err = writer_->Close();
91 if (err)
92 LOG(ERROR) << "FileWriter::Close failed: "
93 << utils::ErrnoNumberAsString(-err);
94 }
95 private:
96 FileWriter* writer_;
Darin Petkove971f332010-09-22 16:57:25 -070097
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070098 DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000099};
100
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101} // namespace chromeos_update_engine
102
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700103#endif // UPDATE_ENGINE_FILE_WRITER_H_