*** src/backend/commands/copy.c.orig Wed Apr 1 07:12:46 2009 --- src/backend/commands/copy.c Tue Apr 7 12:16:38 2009 *************** *** 1233,1239 **** --- 1233,1244 ---- { mode_t oumask; /* Pre-existing umask value */ struct stat st; + #ifdef WIN32 + char *local_filename, *convstr, *convutf16; + int encoding, result, nSize; + local_filename = convutf16 = NULL; + #endif /* * Prevent write to relative path ... too easy to shoot oneself in the * foot by overwriting a database file ... *************** *** 1244,1250 **** --- 1249,1286 ---- errmsg("relative path not allowed for COPY to file"))); oumask = umask((mode_t) 022); + #ifdef WIN32 + /* localization name of a machine is required. */ + encoding = GetDatabaseEncoding(); + if (encoding != PG_UTF8) + convstr = pg_do_encoding_conversion(cstate->filename, strlen(cstate->filename), encoding, PG_UTF8); + else + convstr = cstate->filename; + + result = MultiByteToWideChar(CP_UTF8, 0, convstr, -1, NULL, 0); + nSize = result * 2 + 2; + convutf16 = palloc(nSize); + result = MultiByteToWideChar(CP_UTF8, 0, convstr, -1, (LPWSTR)convutf16, nSize); + if (result <= 0) + result = -1; + else + { + nSize = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)convutf16, -1, NULL, 0, NULL, NULL ); + local_filename = palloc(nSize); + result = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)convutf16, -1, local_filename, nSize, NULL, NULL); + + } + + pfree(convutf16); + + if (result > 0) + cstate->copy_file = AllocateFile(local_filename, PG_BINARY_W); + else + elog(ERROR, "could not convert string to UTF-8:error %lu", GetLastError()); + #else cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W); + #endif /* WIN32 */ + umask(oumask); if (cstate->copy_file == NULL) *************** *** 1739,1746 **** --- 1775,1816 ---- else { struct stat st; + #ifdef WIN32 + char *local_filename, *convstr, *convutf16; + int encoding, result, nSize; + + local_filename = convutf16 = NULL; + + /* localization name of a machine is required. */ + encoding = GetDatabaseEncoding(); + if (encoding != PG_UTF8) + convstr = pg_do_encoding_conversion(cstate->filename, strlen(cstate->filename), encoding, PG_UTF8); + else + convstr = cstate->filename; + + result = MultiByteToWideChar(CP_UTF8, 0, convstr, -1, NULL, 0); + nSize = result * 2 + 2; + convutf16 = palloc(nSize); + result = MultiByteToWideChar(CP_UTF8, 0, convstr, -1, (LPWSTR)convutf16, nSize); + if (result <= 0) + result = -1; + else + { + nSize = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)convutf16, -1, NULL, 0, NULL, NULL ); + local_filename = palloc(nSize); + result = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)convutf16, -1, local_filename, nSize, NULL, NULL); + } + + pfree(convutf16); + + if (result > 0) + cstate->copy_file = AllocateFile(local_filename, PG_BINARY_R); + else + elog(ERROR, "could not convert string to UTF-8:error %lu", GetLastError()); + #else cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R); + #endif /* WIN32 */ if (cstate->copy_file == NULL) ereport(ERROR,