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 <brillo/secure_blob.h>
24 
25 namespace chromeos_update_engine {
26 
27 class BlobFileWriter {
28  public:
29   // Create the BlobFileWriter object that will manage the blobs stored to
30   // |blob_fd| in a thread safe way.
BlobFileWriter(int blob_fd,off_t * blob_file_size)31   BlobFileWriter(int blob_fd, off_t* blob_file_size)
32       : blob_fd_(blob_fd), blob_file_size_(blob_file_size) {}
33 
34   // Store the passed |blob| in the blob file. Returns the offset at which it
35   // was stored, or -1 in case of failure.
36   off_t StoreBlob(const brillo::Blob& blob);
37 
38   // The number of |total_blobs| is the number of blobs that will be stored but
39   // is only used for logging purposes. If not set or set to 0, logging will be
40   // skipped. This function will also reset the number of stored blobs to 0.
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