blob: f8006438bc394c47c3b8e8da0b2de4c66ec02319 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_FILE_WRITER_H_
6#define UPDATE_ENGINE_FILE_WRITER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <fcntl.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -07009#include <sys/stat.h>
10#include <sys/types.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000011#include <unistd.h>
Alex Deymob5ba9e42014-05-16 13:17:21 -070012
13#include <base/logging.h>
14
15#include "update_engine/error_code.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000016#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000017
18// FileWriter is a class that is used to (synchronously, for now) write to
19// a file. This file is a thin wrapper around open/write/close system calls,
20// but provides and interface that can be customized by subclasses that wish
21// to filter the data.
22
23namespace chromeos_update_engine {
24
25class FileWriter {
26 public:
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070027 FileWriter() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000028 virtual ~FileWriter() {}
29
30 // Wrapper around open. Returns 0 on success or -errno on error.
31 virtual int Open(const char* path, int flags, mode_t mode) = 0;
32
Don Garrette410e0f2011-11-10 15:39:01 -080033 // Wrapper around write. Returns true if all requested bytes
Alex Vakulenko072359c2014-07-18 11:41:07 -070034 // were written, or false on any error, regardless of progress.
Don Garrette410e0f2011-11-10 15:39:01 -080035 virtual bool Write(const void* bytes, size_t count) = 0;
rspangler@google.com49fdf182009-10-10 00:57:34 +000036
Jay Srinivasan51dcf262012-09-13 17:24:32 -070037 // Same as the Write method above but returns a detailed |error| code
38 // in addition if the returned value is false. By default this method
39 // returns kActionExitDownloadWriteError as the error code, but subclasses
40 // can override if they wish to return more specific error codes.
41 virtual bool Write(const void* bytes,
42 size_t count,
David Zeuthena99981f2013-04-29 13:42:47 -070043 ErrorCode* error) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070044 *error = ErrorCode::kDownloadWriteError;
Jay Srinivasan51dcf262012-09-13 17:24:32 -070045 return Write(bytes, count);
46 }
47
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 // Wrapper around close. Returns 0 on success or -errno on error.
49 virtual int Close() = 0;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070050
51 private:
52 DISALLOW_COPY_AND_ASSIGN(FileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000053};
54
55// Direct file writer is probably the simplest FileWriter implementation.
56// It calls the system calls directly.
57
58class DirectFileWriter : public FileWriter {
59 public:
60 DirectFileWriter() : fd_(-1) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000061
Alex Deymo610277e2014-11-11 21:18:11 -080062 int Open(const char* path, int flags, mode_t mode) override;
63 bool Write(const void* bytes, size_t count) override;
64 int Close() override;
rspangler@google.com49fdf182009-10-10 00:57:34 +000065
adlr@google.comc98a7ed2009-12-04 18:54:03 +000066 int fd() const { return fd_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000067
68 private:
69 int fd_;
Darin Petkove971f332010-09-22 16:57:25 -070070
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070071 DISALLOW_COPY_AND_ASSIGN(DirectFileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000072};
73
adlr@google.comc98a7ed2009-12-04 18:54:03 +000074class ScopedFileWriterCloser {
75 public:
76 explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {}
77 ~ScopedFileWriterCloser() {
78 int err = writer_->Close();
79 if (err)
80 LOG(ERROR) << "FileWriter::Close failed: "
81 << utils::ErrnoNumberAsString(-err);
82 }
83 private:
84 FileWriter* writer_;
Darin Petkove971f332010-09-22 16:57:25 -070085
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070086 DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000087};
88
rspangler@google.com49fdf182009-10-10 00:57:34 +000089} // namespace chromeos_update_engine
90
Gilad Arnoldcf175a02014-07-10 16:48:47 -070091#endif // UPDATE_ENGINE_FILE_WRITER_H_