<commit_before>/*

Copyright (c) 2008, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/create_torrent.hpp"
#include "libtorrent/file_pool.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/escape_string.hpp"

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/bind.hpp>

#include <sys/types.h>
#include <sys/stat.h>

#define MAX_SYMLINK_PATH 200

namespace gr = boost::gregorian;

namespace libtorrent
{
	// defined in torrent_info.cpp
	int merkle_num_leafs(int);
	int merkle_num_nodes(int);
	int merkle_get_parent(int);
	int merkle_get_sibling(int);

	namespace detail
	{
		int TORRENT_EXPORT get_file_attributes(boost::filesystem::path const& p)
		{
#ifdef TORRENT_WINDOWS

#if TORRENT_USE_WPATH
			std::wstring path = convert_to_wstring(p.external_file_string());
			DWORD attr = GetFileAttributesW(path.c_str());
#else
			std::string path = convert_to_native(p.external_file_string());
			DWORD attr = GetFileAttributesA(path.c_str());
#endif
			if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
			return 0;
#else
			struct stat s;
			if (lstat(convert_to_native(p.external_file_string()).c_str(), &s) < 0) return 0;
			int file_attr = 0;
			if (s.st_mode & S_IXUSR) 
				file_attr += file_storage::attribute_executable;
			if(S_ISLNK(s.st_mode))
				file_attr += file_storage::attribute_symlink;
			return file_attr;
#endif
		}
	
		int TORRENT_EXPORT get_file_attributes(boost::filesystem::wpath const& p)
		{
#ifdef TORRENT_WINDOWS
			std::wstring const& path = p.external_file_string();
			DWORD attr = GetFileAttributesW(path.c_str());
			if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
			return 0;
#else
			std::string native;
			wchar_utf8(p.string(), native);
			native = convert_to_native(native);

			struct stat s;
			if (lstat(native.c_str(), &s) < 0) return 0;
			int file_attr = 0;
			if (s.st_mode & S_IXUSR) 
				file_attr += file_storage::attribute_executable;
			if (S_ISLNK(s.st_mode))
				file_attr += file_storage::attribute_symlink;
			return file_attr;
#endif
		}

		std::time_t get_file_mtime(char const* path)
		{
#ifdef TORRENT_WINDOWS
			struct _stat s;
			if (::_stat(path, &s) < 0) return 0;
#else
			struct stat s;
			if (lstat(path, &s) < 0) return 0;
#endif
			return s.st_mtime;
		}

		std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::path const& p)
		{
#if defined TORRENT_WINDOWS && TORRENT_USE_WPATH
			std::wstring path = convert_to_wstring(p.external_file_string());
			struct _stat s;
			if (::_wstat(path.c_str(), &s) < 0) return 0;
			return s.st_mtime;
#else
			std::string path = convert_to_native(p.external_file_string());
			return get_file_mtime(p.string().c_str());
#endif
		}

		std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::wpath const& p)
		{
#ifdef TORRENT_WINDOWS
			struct _stat s;
			if (::_wstat(p.string().c_str(), &s) < 0) return 0;
			return s.st_mtime;
#else
			std::string utf8;
			wchar_utf8(p.string(), utf8);
			utf8 = convert_to_native(utf8);
			return get_file_mtime(utf8.c_str());
#endif
		}

		boost::filesystem::path get_symlink_path(char const* path)
		{
#ifdef TORRENT_WINDOWS
			return "";
#else
			char buf[MAX_SYMLINK_PATH];
			int char_read = readlink(path,buf,MAX_SYMLINK_PATH);
			if (char_read < 0) return "";
			if (char_read < MAX_SYMLINK_PATH) buf[char_read] = 0;
			else buf[0] = 0;
			return buf;
#endif
		}

		boost::filesystem::path TORRENT_EXPORT get_symlink_path(boost::filesystem::path const& p)
		{
#if defined TORRENT_WINDOWS && TORRENT_USE_WPATH
			std::wstring path = convert_to_wstring(p.external_file_string());
			return get_symlink_path(path.c_str());
#else
			std::string path = convert_to_native(p.external_file_string());
			return get_symlink_path(p.string().c_str());
#endif
		}

		boost::filesystem::path TORRENT_EXPORT get_symlink_path(boost::filesystem::wpath const& p)
		{
#ifdef TORRENT_WINDOWS
			return get_symlink_path(p.string().c_str());
#else
			std::string utf8;
			wchar_utf8(p.string(), utf8);
			utf8 = convert_to_native(utf8);
			return get_symlink_path(utf8.c_str());
#endif
		}
<commit_msg>fox windows code for get_symlink_path() (the previous implementation caused infinite recursion on windows)<commit_after>/*

Copyright (c) 2008, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#include "libtorrent/create_torrent.hpp"
#include "libtorrent/file_pool.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/escape_string.hpp"

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/bind.hpp>

#include <sys/types.h>
#include <sys/stat.h>

#define MAX_SYMLINK_PATH 200

namespace gr = boost::gregorian;

namespace libtorrent
{
	// defined in torrent_info.cpp
	int merkle_num_leafs(int);
	int merkle_num_nodes(int);
	int merkle_get_parent(int);
	int merkle_get_sibling(int);

	namespace detail
	{
		int TORRENT_EXPORT get_file_attributes(boost::filesystem::path const& p)
		{
#ifdef TORRENT_WINDOWS

#if TORRENT_USE_WPATH
			std::wstring path = convert_to_wstring(p.external_file_string());
			DWORD attr = GetFileAttributesW(path.c_str());
#else
			std::string path = convert_to_native(p.external_file_string());
			DWORD attr = GetFileAttributesA(path.c_str());
#endif
			if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
			return 0;
#else
			struct stat s;
			if (lstat(convert_to_native(p.external_file_string()).c_str(), &s) < 0) return 0;
			int file_attr = 0;
			if (s.st_mode & S_IXUSR) 
				file_attr += file_storage::attribute_executable;
			if(S_ISLNK(s.st_mode))
				file_attr += file_storage::attribute_symlink;
			return file_attr;
#endif
		}
	
		int TORRENT_EXPORT get_file_attributes(boost::filesystem::wpath const& p)
		{
#ifdef TORRENT_WINDOWS
			std::wstring const& path = p.external_file_string();
			DWORD attr = GetFileAttributesW(path.c_str());
			if (attr & FILE_ATTRIBUTE_HIDDEN) return file_storage::attribute_hidden;
			return 0;
#else
			std::string native;
			wchar_utf8(p.string(), native);
			native = convert_to_native(native);

			struct stat s;
			if (lstat(native.c_str(), &s) < 0) return 0;
			int file_attr = 0;
			if (s.st_mode & S_IXUSR) 
				file_attr += file_storage::attribute_executable;
			if (S_ISLNK(s.st_mode))
				file_attr += file_storage::attribute_symlink;
			return file_attr;
#endif
		}

		std::time_t get_file_mtime(char const* path)
		{
#ifdef TORRENT_WINDOWS
			struct _stat s;
			if (::_stat(path, &s) < 0) return 0;
#else
			struct stat s;
			if (lstat(path, &s) < 0) return 0;
#endif
			return s.st_mtime;
		}

		std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::path const& p)
		{
#if defined TORRENT_WINDOWS && TORRENT_USE_WPATH
			std::wstring path = convert_to_wstring(p.external_file_string());
			struct _stat s;
			if (::_wstat(path.c_str(), &s) < 0) return 0;
			return s.st_mtime;
#else
			std::string path = convert_to_native(p.external_file_string());
			return get_file_mtime(p.string().c_str());
#endif
		}

		std::time_t TORRENT_EXPORT get_file_mtime(boost::filesystem::wpath const& p)
		{
#ifdef TORRENT_WINDOWS
			struct _stat s;
			if (::_wstat(p.string().c_str(), &s) < 0) return 0;
			return s.st_mtime;
#else
			std::string utf8;
			wchar_utf8(p.string(), utf8);
			utf8 = convert_to_native(utf8);
			return get_file_mtime(utf8.c_str());
#endif
		}

#ifndef TORRENT_WINDOWS
		boost::filesystem::path get_symlink_path_impl(char const* path)
		{
			char buf[MAX_SYMLINK_PATH];
			int char_read = readlink(path,buf,MAX_SYMLINK_PATH);
			if (char_read < 0) return "";
			if (char_read < MAX_SYMLINK_PATH) buf[char_read] = 0;
			else buf[0] = 0;
			return buf;
		}
#endif

		boost::filesystem::path TORRENT_EXPORT get_symlink_path(boost::filesystem::path const& p)
		{
#if defined TORRENT_WINDOWS && TORRENT_USE_WPATH
			return "";
#else
			std::string path = convert_to_native(p.external_file_string());
			return get_symlink_path_impl(p.string().c_str());
#endif
		}

		boost::filesystem::path TORRENT_EXPORT get_symlink_path(boost::filesystem::wpath const& p)
		{
#ifdef TORRENT_WINDOWS
			return "";
#else
			std::string utf8;
			wchar_utf8(p.string(), utf8);
			utf8 = convert_to_native(utf8);
			return get_symlink_path_impl(utf8.c_str());
#endif
		}

{'fraction_non_alphanumeric': 0.10165864098448368, 'fraction_numerical': 0.0071339397182093815, 'mean_word_length': 4.829002079002079, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, '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': '5180114', 'n_tokens_mistral': 4276, 'n_tokens_neox': 3796, 'n_words': 1043}