scoped_temporary_file.cc 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2014 The Chromium 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. #include "sandbox/linux/tests/scoped_temporary_file.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include "base/check_op.h"
  9. #include "base/posix/eintr_wrapper.h"
  10. #include "build/build_config.h"
  11. namespace sandbox {
  12. ScopedTemporaryFile::ScopedTemporaryFile() : fd_(-1) {
  13. #if BUILDFLAG(IS_ANDROID)
  14. static const char file_template[] = "/data/local/tmp/ScopedTempFileXXXXXX";
  15. #else
  16. static const char file_template[] = "/tmp/ScopedTempFileXXXXXX";
  17. #endif // BUILDFLAG(IS_ANDROID)
  18. static_assert(sizeof(full_file_name_) >= sizeof(file_template),
  19. "full_file_name is not large enough");
  20. memcpy(full_file_name_, file_template, sizeof(file_template));
  21. fd_ = mkstemp(full_file_name_);
  22. CHECK_LE(0, fd_);
  23. }
  24. ScopedTemporaryFile::~ScopedTemporaryFile() {
  25. CHECK_EQ(0, unlink(full_file_name_));
  26. CHECK_EQ(0, IGNORE_EINTR(close(fd_)));
  27. }
  28. } // namespace sandbox