<commit_before>// Copyright 2015 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "util/test/scoped_temp_dir.h"

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>

#include "base/posix/eintr_wrapper.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "util/test/errors.h"

#if defined(OS_POSIX)
#include <unistd.h>
#elif defined(OS_WIN)
#include <direct.h>
#include <io.h>
#endif  // OS_POSIX

namespace crashpad {
namespace test {
namespace {

bool FileExists(const base::FilePath& path) {
#if defined(OS_POSIX)
  struct stat st;
  int rv = lstat(path.value().c_str(), &st);
#elif defined(OS_WIN)
  struct _stat st;
  int rv = _wstat(path.value().c_str(), &st);
#else
#error "Not implemented"
#endif
  if (rv < 0) {
    EXPECT_EQ(ENOENT, errno) << ErrnoMessage("lstat") << " " << path.value();
    return false;
  }
  return true;
}

void CreateFile(const base::FilePath& path) {
#if defined(OS_POSIX)
  int fd = HANDLE_EINTR(creat(path.value().c_str(), 0644));
  ASSERT_GE(fd, 0) << ErrnoMessage("creat") << " " << path.value();
  ASSERT_EQ(0, IGNORE_EINTR(close(fd)))
      << ErrnoMessage("close") << " " << path.value();
#elif defined(OS_WIN)
  int fd = _wcreat(path.value().c_str(), 0644);
  ASSERT_GE(fd, 0) << ErrnoMessage("_wcreat") << " " << path.value();
  ASSERT_EQ(0, _close(fd)) << ErrnoMessage("_close") << " " << path.value();
#else
#error "Not implemented"
#endif
  EXPECT_TRUE(FileExists(path));
}

void CreateDirectory(const base::FilePath& path) {
#if defined(OS_POSIX)
  ASSERT_EQ(0, mkdir(path.value().c_str(), 0755))
      << ErrnoMessage("mkdir") << " " << path.value();
#elif defined(OS_WIN)
  ASSERT_EQ(0, _wmkdir(path.value().c_str()))
      << ErrnoMessage("_wmkdir") << " " << path.value();
#else
#error "Not implemented"
#endif<commit_msg>Fix function name in error message of scoped_temp_dir_test<commit_after>// Copyright 2015 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "util/test/scoped_temp_dir.h"

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>

#include "base/posix/eintr_wrapper.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#include "util/test/errors.h"

#if defined(OS_POSIX)
#include <unistd.h>
#elif defined(OS_WIN)
#include <direct.h>
#include <io.h>
#endif  // OS_POSIX

namespace crashpad {
namespace test {
namespace {

bool FileExists(const base::FilePath& path) {
#if defined(OS_POSIX)
  struct stat st;
  int rv = lstat(path.value().c_str(), &st);
  const char stat_function[] = "lstat";
#elif defined(OS_WIN)
  struct _stat st;
  int rv = _wstat(path.value().c_str(), &st);
  const char stat_function[] = "_wstat";
#else
#error "Not implemented"
#endif
  if (rv < 0) {
    EXPECT_EQ(ENOENT, errno) << ErrnoMessage(stat_function) << " "
                             << path.value();
    return false;
  }
  return true;
}

void CreateFile(const base::FilePath& path) {
#if defined(OS_POSIX)
  int fd = HANDLE_EINTR(creat(path.value().c_str(), 0644));
  ASSERT_GE(fd, 0) << ErrnoMessage("creat") << " " << path.value();
  ASSERT_EQ(0, IGNORE_EINTR(close(fd)))
      << ErrnoMessage("close") << " " << path.value();
#elif defined(OS_WIN)
  int fd = _wcreat(path.value().c_str(), 0644);
  ASSERT_GE(fd, 0) << ErrnoMessage("_wcreat") << " " << path.value();
  ASSERT_EQ(0, _close(fd)) << ErrnoMessage("_close") << " " << path.value();
#else
#error "Not implemented"
#endif
  EXPECT_TRUE(FileExists(path));
}

void CreateDirectory(const base::FilePath& path) {
#if defined(OS_POSIX)
  ASSERT_EQ(0, mkdir(path.value().c_str(), 0755))
      << ErrnoMessage("mkdir") << " " << path.value();
#elif defined(OS_WIN)
  ASSERT_EQ(0, _wmkdir(path.value().c_str()))
      << ErrnoMessage("_wmkdir") << " " << path.value();
#else
#error "Not implemented"
#endif
{'fraction_non_alphanumeric': 0.16270083384177345, 'fraction_numerical': 0.010982306284319707, 'mean_word_length': 4.339847991313789, 'pattern_counts': {'":': 0, '<': 103, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1345102', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1690, 'n_words': 454}