Fix files past MAX_PATH not being claimed or decoded
This commit is contained in:
1 parent
89508a372a
commit
ea30786711
8 files changed
+286
-42
No files matched your search
+2
-11
@@ -8,20 +8,11 @@
|
||||
#include <vector>
|
||||
|
||||
#include "log.h"
|
||||
#include "win_path.h"
|
||||
|
||||
namespace joc_container {
|
||||
namespace {
|
||||
|
||||
std::wstring utf8_to_wide(const std::string& text) {
|
||||
if (text.empty()) return {};
|
||||
const int needed = MultiByteToWideChar(CP_UTF8, 0, text.c_str(),
|
||||
static_cast<int>(text.size()), nullptr, 0);
|
||||
std::wstring out(static_cast<std::size_t>(needed), L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()), out.data(),
|
||||
needed);
|
||||
return out;
|
||||
}
|
||||
|
||||
// A library scan probes every container in the library, and most of them are
|
||||
// declined; they also mostly share a directory, so the path would spend the log's
|
||||
// byte budget repeating the same prefix once per file. The file name is what
|
||||
@@ -37,7 +28,7 @@ std::string file_name_of(const std::string& path) {
|
||||
class Window {
|
||||
public:
|
||||
bool open(const std::string& path) {
|
||||
const std::wstring wide = utf8_to_wide(path);
|
||||
const std::wstring wide = joc_path::to_wide_extended(path);
|
||||
handle_ = CreateFileW(wide.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (handle_ == INVALID_HANDLE_VALUE) {
|
||||
|
||||
+7
-16
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "eac3_scan.h"
|
||||
#include "log.h"
|
||||
#include "win_path.h"
|
||||
|
||||
namespace joc_decode {
|
||||
namespace {
|
||||
@@ -78,18 +79,8 @@ struct CoreApi {
|
||||
const char*(JOC_CALL* error_name)(joc_error) = joc_error_name;
|
||||
};
|
||||
|
||||
std::wstring utf8_to_wide(const std::string& text) {
|
||||
if (text.empty()) return {};
|
||||
const int needed = MultiByteToWideChar(CP_UTF8, 0, text.c_str(),
|
||||
static_cast<int>(text.size()), nullptr, 0);
|
||||
std::wstring out(static_cast<std::size_t>(needed), L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()), out.data(),
|
||||
needed);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool file_exists(const std::string& path) {
|
||||
const std::wstring wide = utf8_to_wide(path);
|
||||
const std::wstring wide = joc_path::to_wide_extended(path);
|
||||
if (wide.empty()) return false;
|
||||
const DWORD attributes = GetFileAttributesW(wide.c_str());
|
||||
return attributes != INVALID_FILE_ATTRIBUTES &&
|
||||
@@ -147,11 +138,11 @@ public:
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, &attributes,
|
||||
CREATE_ALWAYS, 0, nullptr);
|
||||
|
||||
std::wstring command = L"\"" + utf8_to_wide(ffmpeg_path) + L"\"";
|
||||
std::wstring command = L"\"" + joc_path::to_wide_extended(ffmpeg_path) + L"\"";
|
||||
command += L" -hide_banner -loglevel error -nostdin -y ";
|
||||
command += input_arguments; // input options must precede -i
|
||||
command += L" -i \"";
|
||||
command += utf8_to_wide(input_path);
|
||||
command += joc_path::to_wide_extended(input_path);
|
||||
command += L"\" ";
|
||||
command += output_arguments;
|
||||
|
||||
@@ -223,7 +214,7 @@ class InputFile {
|
||||
public:
|
||||
~InputFile() { close(); }
|
||||
bool open(const std::string& path) {
|
||||
handle_ = CreateFileW(utf8_to_wide(path).c_str(), GENERIC_READ,
|
||||
handle_ = CreateFileW(joc_path::to_wide_extended(path).c_str(), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
|
||||
return handle_ != INVALID_HANDLE_VALUE;
|
||||
@@ -448,8 +439,8 @@ bool probe_container_joc(const std::string& ffmpeg_path, const std::string& path
|
||||
std::uint64_t modified = 0;
|
||||
{
|
||||
WIN32_FILE_ATTRIBUTE_DATA data{};
|
||||
if (GetFileAttributesExW(utf8_to_wide(path).c_str(), GetFileExInfoStandard, &data) !=
|
||||
FALSE) {
|
||||
if (GetFileAttributesExW(joc_path::to_wide_extended(path).c_str(), GetFileExInfoStandard,
|
||||
&data) != FALSE) {
|
||||
size = (static_cast<std::uint64_t>(data.nFileSizeHigh) << 32) | data.nFileSizeLow;
|
||||
modified = (static_cast<std::uint64_t>(data.ftLastWriteTime.dwHighDateTime) << 32) |
|
||||
data.ftLastWriteTime.dwLowDateTime;
|
||||
|
||||
+9
-14
@@ -8,6 +8,8 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "win_path.h"
|
||||
|
||||
namespace joc_log {
|
||||
namespace {
|
||||
|
||||
@@ -47,33 +49,26 @@ std::wstring module_directory() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
std::string to_utf8(const std::wstring& text) {
|
||||
if (text.empty()) return {};
|
||||
const int needed = WideCharToMultiByte(CP_UTF8, 0, text.c_str(),
|
||||
static_cast<int>(text.size()), nullptr, 0,
|
||||
nullptr, nullptr);
|
||||
std::string out(static_cast<std::string::size_type>(needed), '\0');
|
||||
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), static_cast<int>(text.size()),
|
||||
out.data(), needed, nullptr, nullptr);
|
||||
return out;
|
||||
}
|
||||
|
||||
void open_locked() {
|
||||
g_opened = true;
|
||||
|
||||
wchar_t from_env[4096] = {};
|
||||
const DWORD env_length = GetEnvironmentVariableW(L"JOC_LOG", from_env,
|
||||
static_cast<DWORD>(4096));
|
||||
std::wstring plain;
|
||||
if (env_length > 0 && env_length < 4096) {
|
||||
g_wide_path.assign(from_env, env_length);
|
||||
plain.assign(from_env, env_length);
|
||||
} else {
|
||||
const std::wstring dir = module_directory();
|
||||
if (dir.empty()) return;
|
||||
g_wide_path = dir + L"\\joc_decoder.log";
|
||||
plain = dir + L"\\joc_decoder.log";
|
||||
}
|
||||
|
||||
// g_path is the one the component reports and a user reads; the handles work on
|
||||
// the extended form, because a portable install can sit deeper than MAX_PATH.
|
||||
g_path = joc_path::to_utf8(plain);
|
||||
g_wide_path = joc_path::to_wide_extended(plain);
|
||||
g_wide_rolled = g_wide_path + L".1";
|
||||
g_path = to_utf8(g_wide_path);
|
||||
g_file = _wfopen(g_wide_path.c_str(), L"wb");
|
||||
if (g_file == nullptr) {
|
||||
g_path.clear();
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// Paths handed to the Win32 file APIs.
|
||||
//
|
||||
// Windows refuses a path of MAX_PATH characters or more to any call that does not
|
||||
// carry the \\?\ prefix. The prefix is a property of the path, not of the process:
|
||||
// a component cannot inherit it from the host's manifest, so whoever calls the API
|
||||
// has to add it. A file that plays from the desktop therefore stops being found
|
||||
// once its folder tree is deep enough, which is what this header exists to prevent.
|
||||
//
|
||||
// Everything in this component that hands a path to Win32 -- or puts one on an
|
||||
// ffmpeg command line, which is the same call made by the child process -- goes
|
||||
// through to_wide_extended(). What is deliberately not routed through it is the
|
||||
// path the component keeps for itself: the one it logs, and the one it uses as the
|
||||
// container-probe cache key. Those stay in their plain form, so a log line reads
|
||||
// like a path a user can paste into Explorer.
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace joc_path {
|
||||
|
||||
// UTF-8 -> UTF-16, for the W APIs. Every path crosses into the OS through here:
|
||||
// the narrow CRT would convert it through the ANSI code page, which is what breaks
|
||||
// names like "Les Fêtes d'Hébé".
|
||||
inline std::wstring to_wide(const std::string& utf8) {
|
||||
if (utf8.empty()) return {};
|
||||
const int needed = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(),
|
||||
static_cast<int>(utf8.size()), nullptr, 0);
|
||||
if (needed <= 0) return {};
|
||||
std::wstring out(static_cast<std::size_t>(needed), L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.size()), out.data(),
|
||||
needed);
|
||||
return out;
|
||||
}
|
||||
|
||||
// The same path in the form the Win32 file APIs need. Three kinds of path are left
|
||||
// exactly as they came in, and each of them would be broken by a prefix:
|
||||
//
|
||||
// * one that already carries \\?\, which makes this idempotent;
|
||||
// * one that is not fully qualified -- a relative path, or the ffmpeg setting
|
||||
// when it is a bare executable name that PATH resolves. \\?\ is undefined for
|
||||
// those;
|
||||
// * one with a "." or ".." segment, because \\?\ switches off the normalization
|
||||
// that would resolve it.
|
||||
//
|
||||
// The separators are settled first for the same reason: once the prefix is on,
|
||||
// nothing turns a forward slash into a backslash any more, and both of the checks
|
||||
// above have to see the path the way the file system will.
|
||||
inline std::wstring to_wide_extended(const std::wstring& path) {
|
||||
if (path.empty()) return path;
|
||||
if (path.compare(0, 4, L"\\\\?\\") == 0) return path;
|
||||
|
||||
std::wstring plain = path;
|
||||
for (wchar_t& character : plain) {
|
||||
if (character == L'/') character = L'\\';
|
||||
}
|
||||
|
||||
const bool unc = plain.size() >= 2 && plain[0] == L'\\' && plain[1] == L'\\';
|
||||
const bool drive = plain.size() >= 3 && plain[1] == L':' && plain[2] == L'\\';
|
||||
if (!unc && !drive) return path;
|
||||
if (plain.find(L"\\.\\") != std::wstring::npos ||
|
||||
plain.find(L"\\..\\") != std::wstring::npos) {
|
||||
return path;
|
||||
}
|
||||
|
||||
// A UNC path drops its two leading separators; everything else is copied whole.
|
||||
std::wstring out = unc ? L"\\\\?\\UNC\\" : L"\\\\?\\";
|
||||
out.append(plain, unc ? 2 : 0, std::wstring::npos);
|
||||
return out;
|
||||
}
|
||||
|
||||
inline std::wstring to_wide_extended(const std::string& utf8) {
|
||||
return to_wide_extended(to_wide(utf8));
|
||||
}
|
||||
|
||||
// UTF-16 -> UTF-8, the direction the component reports paths in.
|
||||
inline std::string to_utf8(const std::wstring& wide) {
|
||||
if (wide.empty()) return {};
|
||||
const int needed = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(),
|
||||
static_cast<int>(wide.size()), nullptr, 0, nullptr,
|
||||
nullptr);
|
||||
if (needed <= 0) return {};
|
||||
std::string out(static_cast<std::string::size_type>(needed), '\0');
|
||||
WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), static_cast<int>(wide.size()), out.data(),
|
||||
needed, nullptr, nullptr);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace joc_path
|
||||
Reference in new issue
Block a user