blob: 508cae06205f76f0fec131944d6c24a422da007b [file] [log] [blame]
Andrew de los Reyes0cca4212010-04-29 14:00:58 -07001// Copyright (c) 2010 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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_SPLIT_FILE_WRITER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_SPLIT_FILE_WRITER_H__
7
8#include "update_engine/file_writer.h"
9
10// SplitFileWriter is an implementation of FileWriter suited to our
11// full autoupdate format. The first 8 bytes read are assumed to be a
12// big-endian number describing how many of the next following bytes
13// go to the first FileWriter. After that, the rest of the bytes all
14// go to the second FileWriter.
15
16namespace chromeos_update_engine {
17
18class SplitFileWriter : public FileWriter {
19 public:
20 SplitFileWriter(FileWriter* first_file_writer, FileWriter* second_file_writer)
21 : first_file_writer_(first_file_writer),
22 first_length_(0),
23 first_path_(NULL),
24 first_flags_(0),
25 first_mode_(0),
26 second_file_writer_(second_file_writer),
27 bytes_received_(0) {}
Chris Masone4dc2ada2010-09-23 12:43:03 -070028
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070029 void SetFirstOpenArgs(const char* path, int flags, mode_t mode) {
30 first_path_ = path;
31 first_flags_ = flags;
32 first_mode_ = mode;
33 }
Chris Masone4dc2ada2010-09-23 12:43:03 -070034
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070035 // If both succeed, returns the return value from the second Open() call.
36 // On error, both files will be left closed.
37 virtual int Open(const char* path, int flags, mode_t mode);
38
39 virtual ssize_t Write(const void* bytes, size_t count);
40
41 virtual int Close();
42
43 private:
44 // Data for the first file writer.
45 FileWriter* const first_file_writer_;
46 off_t first_length_;
47 const char* first_path_;
48 int first_flags_;
49 mode_t first_mode_;
Chris Masone4dc2ada2010-09-23 12:43:03 -070050
51 // The second file writer.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070052 FileWriter* const second_file_writer_;
53
Chris Masone4dc2ada2010-09-23 12:43:03 -070054 // Bytes written thus far.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070055 off_t bytes_received_;
56 char first_length_buf_[sizeof(uint64_t)];
57
58 DISALLOW_COPY_AND_ASSIGN(SplitFileWriter);
59};
60
61} // namespace chromeos_update_engine
62
63#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_SPLIT_FILE_WRITER_H__