blob_file_writer.h 1.9 KB

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