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),
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 brillo::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 or set to 0, logging will be
41   // skipped. This function will also reset the number of stored blobs to 0.
42   void SetTotalBlobs(size_t total_blobs);
43 
44  private:
45   size_t total_blobs_{0};
46   size_t stored_blobs_{0};
47 
48   // The file and its size are protected with the |blob_mutex_|.
49   int blob_fd_;
50   off_t* blob_file_size_;
51 
52   base::Lock blob_mutex_;
53 
54   DISALLOW_COPY_AND_ASSIGN(BlobFileWriter);
55 };
56 
57 }  // namespace chromeos_update_engine
58 
59 #endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_BLOB_FILE_WRITER_H_
60