blob_file_writer.h revision aea4c1cea20dda7ae7e85fc8924a2d784f70d806
1//
2// Copyright (C) 2015 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//
16
17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOB_FILE_WRITER_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOB_FILE_WRITER_H_
19
20#include <base/macros.h>
21
22#include <base/synchronization/lock.h>
23#include <chromeos/secure_blob.h>
24
25namespace chromeos_update_engine {
26
27class BlobFileWriter {
28 public:
29  // Create the BlobFileWriter object that will manage the blobs stored to
30  // |blob_fd| in a thread safe way.
31  BlobFileWriter(int blob_fd, off_t* blob_file_size)
32    : blob_fd_(blob_fd),
33      blob_file_size_(blob_file_size) {}
34
35  // Store the passed |blob| in the blob file. Returns the offset at which it
36  // was stored, or -1 in case of failure.
37  off_t StoreBlob(const chromeos::Blob& blob);
38
39  // The number of |total_blobs| is the number of blobs that will be stored but
40  // is only used for logging purposes. If not set, logging will be skipped.
41  void SetTotalBlobs(size_t total_blobs);
42
43 private:
44  size_t total_blobs_{0};
45  size_t stored_blobs_{0};
46
47  // The file and its size are protected with the |blob_mutex_|.
48  int blob_fd_;
49  off_t* blob_file_size_;
50
51  base::Lock blob_mutex_;
52
53  DISALLOW_COPY_AND_ASSIGN(BlobFileWriter);
54};
55
56}  // namespace chromeos_update_engine
57
58#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOB_FILE_WRITER_H_
59