<commit_before>// Copyright (c) 2017, WNProject Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.txt file.

#include "WNFileSystem/src/WNMappingWindows.h"
#include "WNCore/inc/assert.h"
#include "WNFileSystem/src/WNFileWindows.h"
#include "WNFileSystem/src/WNHelpers.h"
#include "WNFileSystem/src/WNSystemUtilities.h"

namespace wn {
namespace file_system {
namespace internal {

mapping_windows::~mapping_windows() {
  if (m_cleanup) {
    WCHAR unicode_buffer[MAX_PATH] = {0};
    DWORD unicode_buffer_size = MAX_PATH;
    bool result =
        convert_to_unicode(m_root_path, unicode_buffer, unicode_buffer_size);

    WN_RELEASE_ASSERT(
        result, "failed to convert to root path to native format");

    result = recursive_remove_directory(unicode_buffer, unicode_buffer_size);

    WN_RELEASE_ASSERT(result, "unable to cleanup mapping root directory");
  }
}

bool mapping_windows::exists_file(const containers::string_view _path) const {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    return false;
  }

  return internal::exists_file(path);
}

bool mapping_windows::exists_directory(
    const containers::string_view _path) const {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    return false;
  }

  return internal::exists_directory(path);
}

file_ptr mapping_windows::create_file(
    const containers::string_view _path, result& _result) {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    _result = result::fail;

    return nullptr;
  }

  WCHAR unicode_buffer[MAX_PATH] = {0};
  DWORD unicode_buffer_size = MAX_PATH;

  if (!convert_to_unicode(path, unicode_buffer, unicode_buffer_size)) {
    _result = result::fail;

    return nullptr;
  }

  utilities::internal::handle file_handle(::CreateFileW(unicode_buffer,
      GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr));

  if (!file_handle.is_valid()) {
    _result = result::fail;

    return nullptr;
  }

  _result = result::ok;

  return memory::make_intrusive<file_windows>(
      m_allocator, m_allocator, core::move(file_handle));
}

result mapping_windows::create_directory(const containers::string_view _path) {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    return result::fail;
  }

  WCHAR unicode_buffer[MAX_PATH] = {0};
  DWORD unicode_buffer_size = MAX_PATH;

  if (!convert_to_unicode(path, unicode_buffer, unicode_buffer_size)) {
    return result::fail;
  }

  if (::CreateDirectoryW(unicode_buffer, nullptr) != TRUE) {
    return result::fail;
  }

  return result::ok;
}

file_ptr mapping_windows::open_file(
    const containers::string_view _path, result& _result) {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    _result = result::fail;

    return nullptr;
  }

  WCHAR unicode_buffer[MAX_PATH] = {0};
  DWORD unicode_buffer_size = MAX_PATH;

  if (!convert_to_unicode(path, unicode_buffer, unicode_buffer_size)) {
    _result = result::fail;

    return nullptr;
  }

  utilities::internal::handle file_handle(::CreateFileW(unicode_buffer,
      GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr));

  if (!file_handle.is_valid()) {
    _result = result::fail;

    return nullptr;
  }

  LARGE_INTEGER file_size;

  if (!::GetFileSizeEx(file_handle.value(), &file_size)) {
    _result = result::fail;

    return nullptr;
  }

  if (static_cast<LONGLONG>(static_cast<size_t>(file_size.QuadPart)) !=<commit_msg>Relax the sharing mode on Windows.<commit_after>// Copyright (c) 2017, WNProject Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.txt file.

#include "WNFileSystem/src/WNMappingWindows.h"
#include "WNCore/inc/assert.h"
#include "WNFileSystem/src/WNFileWindows.h"
#include "WNFileSystem/src/WNHelpers.h"
#include "WNFileSystem/src/WNSystemUtilities.h"

namespace wn {
namespace file_system {
namespace internal {

mapping_windows::~mapping_windows() {
  if (m_cleanup) {
    WCHAR unicode_buffer[MAX_PATH] = {0};
    DWORD unicode_buffer_size = MAX_PATH;
    bool result =
        convert_to_unicode(m_root_path, unicode_buffer, unicode_buffer_size);

    WN_RELEASE_ASSERT(
        result, "failed to convert to root path to native format");

    result = recursive_remove_directory(unicode_buffer, unicode_buffer_size);

    WN_RELEASE_ASSERT(result, "unable to cleanup mapping root directory");
  }
}

bool mapping_windows::exists_file(const containers::string_view _path) const {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    return false;
  }

  return internal::exists_file(path);
}

bool mapping_windows::exists_directory(
    const containers::string_view _path) const {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    return false;
  }

  return internal::exists_directory(path);
}

file_ptr mapping_windows::create_file(
    const containers::string_view _path, result& _result) {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    _result = result::fail;

    return nullptr;
  }

  WCHAR unicode_buffer[MAX_PATH] = {0};
  DWORD unicode_buffer_size = MAX_PATH;

  if (!convert_to_unicode(path, unicode_buffer, unicode_buffer_size)) {
    _result = result::fail;

    return nullptr;
  }

  utilities::internal::handle file_handle(::CreateFileW(unicode_buffer,
      GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr));

  if (!file_handle.is_valid()) {
    _result = result::fail;

    return nullptr;
  }

  _result = result::ok;

  return memory::make_intrusive<file_windows>(
      m_allocator, m_allocator, core::move(file_handle));
}

result mapping_windows::create_directory(const containers::string_view _path) {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    return result::fail;
  }

  WCHAR unicode_buffer[MAX_PATH] = {0};
  DWORD unicode_buffer_size = MAX_PATH;

  if (!convert_to_unicode(path, unicode_buffer, unicode_buffer_size)) {
    return result::fail;
  }

  if (::CreateDirectoryW(unicode_buffer, nullptr) != TRUE) {
    return result::fail;
  }

  return result::ok;
}

file_ptr mapping_windows::open_file(
    const containers::string_view _path, result& _result) {
  containers::string path(m_allocator);

  if (!sanitize_and_validate_path(_path, path)) {
    _result = result::fail;

    return nullptr;
  }

  WCHAR unicode_buffer[MAX_PATH] = {0};
  DWORD unicode_buffer_size = MAX_PATH;

  if (!convert_to_unicode(path, unicode_buffer, unicode_buffer_size)) {
    _result = result::fail;

    return nullptr;
  }

  utilities::internal::handle file_handle(::CreateFileW(unicode_buffer,
      GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
      OPEN_EXISTING, 0, nullptr));

  if (!file_handle.is_valid()) {
    _result = result::fail;

    return nullptr;
  }

  LARGE_INTEGER file_size;

  if (!::GetFileSizeEx(file_handle.value(), &file_size)) {
    _result = result::fail;

    return nullptr;
  }

  if (static_cast<LONGLONG>(static_cast<size_t>(file_size.QuadPart)) !=
{'fraction_non_alphanumeric': 0.11327913279132791, 'fraction_numerical': 0.0031165311653116533, 'mean_word_length': 4.881274900398406, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2652472', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2402, 'n_words': 500}