mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-01 11:32:48 -05:00
Some support for wide-character filenames in Windows.
The Windows version of the Filename structure now contains three
versions of the pathname, in UTF-16, UTF-8 and the system code page.
Callers can use whichever is most convenient.
All uses of filenames for actually opening files now use the UTF-16
version, which means they can tolerate 'exotic' filenames, by which I
mean those including Unicode characters outside the host system's
CP_ACP default code page.
Other uses of Filename structures inside the 'windows' subdirectory do
something appropriate, e.g. when printing a filename inside a message
box or a console message, we use the UTF-8 version of the filename
with the UTF-8 version of the appropriate API.
There are three remaining pieces to full Unicode filename support:
One is that the cross-platform code has many calls to
filename_to_str(), embodying the assumption that a file name can be
reliably converted into the unspecified current character set; those
will all need changing in some way.
Another is that write_setting_filename(), in windows/storage.c, still
saves filenames to the Registry as an ordinary REG_SZ in the system
code page. So even if an exotic filename were stored in a Conf, that
Conf couldn't round-trip via the Registry and back without corrupting
that filename by coercing it back to a string that fits in CP_ACP and
therefore doesn't represent the same file. This can't be fixed without
a compatibility break in the storage format, and I don't want to make
a minimal change in that area: if we're going to break compatibility,
then we should break it good and hard (the Nanny Ogg principle), and
devise a completely fresh storage representation that fixes as many
other legacy problems as possible at the same time. So that's my plan,
not yet started.
The final point, much more obviously, is that we're still short of
methods to _construct_ any Filename structures using a Unicode input
string! It should now work to enter one in the GUI configurer (either
by manual text input or via the file selector), but it won't
round-trip through a save and load (as discussed above), and there's
still no way to specify one on the command line (the groundwork is
laid by commit 10e1ac7752
but not yet linked up).
But this is a start.
This commit is contained in:
@ -2,48 +2,79 @@
|
||||
* Implementation of Filename for Windows.
|
||||
*/
|
||||
|
||||
#include <wchar.h>
|
||||
|
||||
#include "putty.h"
|
||||
|
||||
Filename *filename_from_str(const char *str)
|
||||
{
|
||||
Filename *fn = snew(Filename);
|
||||
fn->path = dupstr(str);
|
||||
fn->cpath = dupstr(str);
|
||||
fn->wpath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, fn->cpath);
|
||||
fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
|
||||
return fn;
|
||||
}
|
||||
|
||||
Filename *filename_from_wstr(const wchar_t *str)
|
||||
{
|
||||
Filename *fn = snew(Filename);
|
||||
fn->wpath = dupwcs(str);
|
||||
fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, 0, fn->wpath, "?");
|
||||
fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
|
||||
return fn;
|
||||
}
|
||||
|
||||
Filename *filename_from_utf8(const char *ustr)
|
||||
{
|
||||
Filename *fn = snew(Filename);
|
||||
fn->utf8path = dupstr(ustr);
|
||||
fn->wpath = decode_utf8_to_wide_string(fn->utf8path);
|
||||
fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, 0, fn->wpath, "?");
|
||||
return fn;
|
||||
}
|
||||
|
||||
Filename *filename_copy(const Filename *fn)
|
||||
{
|
||||
return filename_from_str(fn->path);
|
||||
Filename *newfn = snew(Filename);
|
||||
newfn->cpath = dupstr(fn->cpath);
|
||||
newfn->wpath = dupwcs(fn->wpath);
|
||||
newfn->utf8path = dupstr(fn->utf8path);
|
||||
return newfn;
|
||||
}
|
||||
|
||||
const char *filename_to_str(const Filename *fn)
|
||||
{
|
||||
return fn->path;
|
||||
return fn->cpath; /* FIXME */
|
||||
}
|
||||
|
||||
bool filename_equal(const Filename *f1, const Filename *f2)
|
||||
{
|
||||
return !strcmp(f1->path, f2->path);
|
||||
/* wpath is primary: two filenames refer to the same file if they
|
||||
* have the same wpath */
|
||||
return !wcscmp(f1->wpath, f2->wpath);
|
||||
}
|
||||
|
||||
bool filename_is_null(const Filename *fn)
|
||||
{
|
||||
return !*fn->path;
|
||||
return !*fn->wpath;
|
||||
}
|
||||
|
||||
void filename_free(Filename *fn)
|
||||
{
|
||||
sfree(fn->path);
|
||||
sfree(fn->wpath);
|
||||
sfree(fn->cpath);
|
||||
sfree(fn->utf8path);
|
||||
sfree(fn);
|
||||
}
|
||||
|
||||
void filename_serialise(BinarySink *bs, const Filename *f)
|
||||
{
|
||||
put_asciz(bs, f->path);
|
||||
put_asciz(bs, f->utf8path);
|
||||
}
|
||||
Filename *filename_deserialise(BinarySource *src)
|
||||
{
|
||||
return filename_from_str(get_asciz(src));
|
||||
const char *utf8 = get_asciz(src);
|
||||
return filename_from_utf8(utf8);
|
||||
}
|
||||
|
||||
char filename_char_sanitise(char c)
|
||||
@ -52,3 +83,10 @@ char filename_char_sanitise(char c)
|
||||
return '.';
|
||||
return c;
|
||||
}
|
||||
|
||||
FILE *f_open(const Filename *fn, const char *mode, bool isprivate)
|
||||
{
|
||||
wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, mode);
|
||||
return _wfopen(fn->wpath, wmode);
|
||||
sfree(wmode);
|
||||
}
|
||||
|
@ -39,17 +39,17 @@ static inline bool open_for_write_would_lose_data_impl(
|
||||
bool open_for_write_would_lose_data(const Filename *fn)
|
||||
{
|
||||
static HMODULE kernel32_module;
|
||||
DECL_WINDOWS_FUNCTION(static, BOOL, GetFileAttributesExA,
|
||||
(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID));
|
||||
DECL_WINDOWS_FUNCTION(static, BOOL, GetFileAttributesExW,
|
||||
(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID));
|
||||
|
||||
if (!kernel32_module) {
|
||||
kernel32_module = load_system32_dll("kernel32.dll");
|
||||
GET_WINDOWS_FUNCTION(kernel32_module, GetFileAttributesExA);
|
||||
GET_WINDOWS_FUNCTION(kernel32_module, GetFileAttributesExW);
|
||||
}
|
||||
|
||||
if (p_GetFileAttributesExA) {
|
||||
if (p_GetFileAttributesExW) {
|
||||
WIN32_FILE_ATTRIBUTE_DATA attrs;
|
||||
if (!p_GetFileAttributesExA(fn->path, GetFileExInfoStandard, &attrs)) {
|
||||
if (!p_GetFileAttributesExW(fn->wpath, GetFileExInfoStandard, &attrs)) {
|
||||
/*
|
||||
* Generally, if we don't identify a specific reason why we
|
||||
* should return true from this function, we return false, and
|
||||
@ -61,8 +61,8 @@ bool open_for_write_would_lose_data(const Filename *fn)
|
||||
return open_for_write_would_lose_data_impl(
|
||||
attrs.dwFileAttributes, attrs.nFileSizeHigh, attrs.nFileSizeLow);
|
||||
} else {
|
||||
WIN32_FIND_DATA fd;
|
||||
HANDLE h = FindFirstFile(fn->path, &fd);
|
||||
WIN32_FIND_DATAW fd;
|
||||
HANDLE h = FindFirstFileW(fn->wpath, &fd);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
/*
|
||||
* As above, if we can't find the file at all, return false.
|
||||
|
Reference in New Issue
Block a user