blob: a534b967b15ba81d0ca35477f836812d01d187bc [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"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000013#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000014
15// FileWriter is a class that is used to (synchronously, for now) write to
16// a file. This file is a thin wrapper around open/write/close system calls,
17// but provides and interface that can be customized by subclasses that wish
18// to filter the data.
19
20namespace chromeos_update_engine {
21
22class FileWriter {
23 public:
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070024 FileWriter() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000025 virtual ~FileWriter() {}
26
27 // Wrapper around open. Returns 0 on success or -errno on error.
28 virtual int Open(const char* path, int flags, mode_t mode) = 0;
29
30 // Wrapper around write. Returns bytes written on success or
31 // -errno on error.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070032 virtual ssize_t Write(const void* bytes, size_t count) = 0;
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
34 // Wrapper around close. Returns 0 on success or -errno on error.
35 virtual int Close() = 0;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070036
37 private:
38 DISALLOW_COPY_AND_ASSIGN(FileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000039};
40
41// Direct file writer is probably the simplest FileWriter implementation.
42// It calls the system calls directly.
43
44class DirectFileWriter : public FileWriter {
45 public:
46 DirectFileWriter() : fd_(-1) {}
47 virtual ~DirectFileWriter() {}
48
adlr@google.comc98a7ed2009-12-04 18:54:03 +000049 virtual int Open(const char* path, int flags, mode_t mode);
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070050 virtual ssize_t Write(const void* bytes, size_t count);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000051 virtual int Close();
rspangler@google.com49fdf182009-10-10 00:57:34 +000052
adlr@google.comc98a7ed2009-12-04 18:54:03 +000053 int fd() const { return fd_; }
rspangler@google.com49fdf182009-10-10 00:57:34 +000054
55 private:
56 int fd_;
Darin Petkove971f332010-09-22 16:57:25 -070057
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070058 DISALLOW_COPY_AND_ASSIGN(DirectFileWriter);
rspangler@google.com49fdf182009-10-10 00:57:34 +000059};
60
adlr@google.comc98a7ed2009-12-04 18:54:03 +000061class ScopedFileWriterCloser {
62 public:
63 explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {}
64 ~ScopedFileWriterCloser() {
65 int err = writer_->Close();
66 if (err)
67 LOG(ERROR) << "FileWriter::Close failed: "
68 << utils::ErrnoNumberAsString(-err);
69 }
70 private:
71 FileWriter* writer_;
Darin Petkove971f332010-09-22 16:57:25 -070072
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070073 DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser);
adlr@google.comc98a7ed2009-12-04 18:54:03 +000074};
75
rspangler@google.com49fdf182009-10-10 00:57:34 +000076} // namespace chromeos_update_engine
77
adlr@google.comc98a7ed2009-12-04 18:54:03 +000078#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__