blob: b9b45e8c1a74e92c7421dc6e8f334cd96dda6f75 [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
adlr@google.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <unistd.h>
Chris Masone790e62e2010-08-12 10:41:18 -070012#include "base/logging.h"
Jay Srinivasan51dcf262012-09-13 17:24:32 -070013#include "update_engine/action_processor.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000014#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000015
16// FileWriter is a class that is used to (synchronously, for now) write to
17// a file. This file is a thin wrapper around open/write/close system calls,
18// but provides and interface that can be customized by subclasses that wish
19// to filter the data.
20
21namespace chromeos_update_engine {
22
23class FileWriter {
24 public:
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070025 FileWriter() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000026 virtual ~FileWriter() {}
27
28 // Wrapper around open. Returns 0 on success or -errno on error.
29 virtual int Open(const char* path, int flags, mode_t mode) = 0;
30
Don Garrette410e0f2011-11-10 15:39:01 -080031 // Wrapper around write. Returns true if all requested bytes
32 // were written, or false on any error, reguardless of progress.
33 virtual bool Write(const void* bytes, size_t count) = 0;
rspangler@google.com49fdf182009-10-10 00:57:34 +000034
Jay Srinivasan51dcf262012-09-13 17:24:32 -070035 // Same as the Write method above but returns a detailed |error| code
36 // in addition if the returned value is false. By default this method
37 // returns kActionExitDownloadWriteError as the error code, but subclasses
38 // can override if they wish to return more specific error codes.
39 virtual bool Write(const void* bytes,
40 size_t count,
41 ActionExitCode* error) {
42 *error = kActionCodeDownloadWriteError;
43 return Write(bytes, count);
44 }
45
rspangler@google.com49fdf182009-10-10 00:57:34 +000046 // Wrapper around close. Returns 0 on success or -errno on error.
47 virtual int Close() = 0;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070048
49 private:
50 DISALLOW_COPY_AND_ASSIGN(FileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000051};
52
53// Direct file writer is probably the simplest FileWriter implementation.
54// It calls the system calls directly.
55
56class DirectFileWriter : public FileWriter {
57 public:
58 DirectFileWriter() : fd_(-1) {}
59 virtual ~DirectFileWriter() {}
60
adlr@google.comc98a7ed2009-12-04 18:54:03 +000061 virtual int Open(const char* path, int flags, mode_t mode);
Don Garrette410e0f2011-11-10 15:39:01 -080062 virtual bool Write(const void* bytes, size_t count);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000063 virtual int Close();
rspangler@google.com49fdf182009-10-10 00:57:34 +000064
adlr@google.comc98a7ed2009-12-04 18:54:03 +000065 int fd() const { return fd_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000066
67 private:
68 int fd_;
Darin Petkove971f332010-09-22 16:57:25 -070069
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070070 DISALLOW_COPY_AND_ASSIGN(DirectFileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000071};
72
adlr@google.comc98a7ed2009-12-04 18:54:03 +000073class ScopedFileWriterCloser {
74 public:
75 explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {}
76 ~ScopedFileWriterCloser() {
77 int err = writer_->Close();
78 if (err)
79 LOG(ERROR) << "FileWriter::Close failed: "
80 << utils::ErrnoNumberAsString(-err);
81 }
82 private:
83 FileWriter* writer_;
Darin Petkove971f332010-09-22 16:57:25 -070084
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070085 DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000086};
87
rspangler@google.com49fdf182009-10-10 00:57:34 +000088} // namespace chromeos_update_engine
89
adlr@google.comc98a7ed2009-12-04 18:54:03 +000090#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__