#include "dbms_file_transfer.hpp"
|
|
#include <cstring>
|
#include <string>
|
#include <vector>
|
|
extern "C" {
|
#include <errno.h>
|
#include <fcntl.h>
|
#include <unistd.h>
|
|
#include "executor/spi.h"
|
#include "miscadmin.h"
|
#include "storage/fd.h"
|
#include "utils/builtins.h"
|
#include "utils/bytea.h"
|
|
PG_FUNCTION_INFO_V1(dbms_file_transfer_copy_file);
|
PG_FUNCTION_INFO_V1(dbms_file_transfer_is_safe_filename);
|
PG_FUNCTION_INFO_V1(dbms_file_transfer_get_file);
|
PG_FUNCTION_INFO_V1(dbms_file_transfer_put_file);
|
PG_FUNCTION_INFO_V1(read_file_chunk);
|
PG_FUNCTION_INFO_V1(write_file_chunk);
|
PG_FUNCTION_INFO_V1(pg_file_unlink_custom);
|
PG_FUNCTION_INFO_V1(dbms_file_transfer_get_version);
|
|
/// @brief Версия RUDB пакета DBMS_FILE_TRANSFER (Oracle DBMS_FILE_TRANSFER пакет)
|
#define RUDB_DBMS_FILE_TRANSFER_VERSION "1.0.0"
|
}
|
|
namespace {
|
|
constexpr std::size_t kCopyBufferSize = 64 * 1024;
|
constexpr int32 kRemoteReadChunkSize = 128 * 1024;
|
constexpr std::size_t kRemoteWriteChunkSize = 8 * 1024;
|
|
bool
|
isSafeFilename(const char* const filename)
|
{
|
if (filename == nullptr || filename[0] == '\0') {
|
return false;
|
}
|
|
if (filename[0] == '/' || filename[0] == '\\') {
|
return false;
|
}
|
|
if (std::strcmp(filename, ".") == 0 || std::strcmp(filename, "..") == 0) {
|
return false;
|
}
|
|
for (const char* ptr = filename; *ptr != '\0'; ++ptr) {
|
if (*ptr == '/' || *ptr == '\\') {
|
return false;
|
}
|
}
|
|
return std::strstr(filename, "..") == nullptr;
|
}
|
|
std::string
|
joinPath(const char* const dir, const char* const file)
|
{
|
return std::string(dir) + "/" + file;
|
}
|
|
void
|
closeTransientFileIfOpened(int& fd)
|
{
|
if (fd >= 0) {
|
CloseTransientFile(fd);
|
fd = -1;
|
}
|
}
|
|
} // namespace
|
|
/*
|
* @qdb_func: dbms_file_transfer_copy_file(text, text, text, text)
|
* @ora_func: DBMS_FILE_TRANSFER.COPY_FILE(VARCHAR2, VARCHAR2, VARCHAR2, VARCHAR2)
|
*/
|
extern "C" Datum
|
dbms_file_transfer_copy_file(PG_FUNCTION_ARGS)
|
{
|
const char* const srcDir = TextDatumGetCString(PG_GETARG_DATUM(0));
|
const char* const srcFile = TextDatumGetCString(PG_GETARG_DATUM(1));
|
const char* const dstDir = TextDatumGetCString(PG_GETARG_DATUM(2));
|
const char* const dstFile = TextDatumGetCString(PG_GETARG_DATUM(3));
|
|
const bool srcSafe = DatumGetBool(
|
DirectFunctionCall1(dbms_file_transfer_is_safe_filename,
|
CStringGetTextDatum(srcFile)));
|
const bool dstSafe = DatumGetBool(
|
DirectFunctionCall1(dbms_file_transfer_is_safe_filename,
|
CStringGetTextDatum(dstFile)));
|
|
if (!srcSafe || !dstSafe) {
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("DBMS_FILE_TRANSFER: unsafe file name")));
|
}
|
|
const std::string fullSrc = joinPath(srcDir, srcFile);
|
const std::string fullDst = joinPath(dstDir, dstFile);
|
|
int srcFd = -1;
|
int dstFd = -1;
|
std::vector<char> buffer(kCopyBufferSize);
|
bool success = false;
|
|
PG_TRY();
|
{
|
srcFd = OpenTransientFile(fullSrc.c_str(), O_RDONLY | PG_BINARY);
|
if (srcFd < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not open source file \"%s\": %m", fullSrc.c_str())));
|
}
|
|
dstFd = OpenTransientFile(fullDst.c_str(), O_WRONLY | O_CREAT | O_TRUNC | PG_BINARY);
|
if (dstFd < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not create destination file \"%s\": %m", fullDst.c_str())));
|
}
|
|
ssize_t nread = 0;
|
while ((nread = read(srcFd, buffer.data(), buffer.size())) > 0) {
|
CHECK_FOR_INTERRUPTS();
|
|
if (write(dstFd, buffer.data(), nread) != nread) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not write to file \"%s\": %m", fullDst.c_str())));
|
}
|
}
|
|
if (nread < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not read from file \"%s\": %m", fullSrc.c_str())));
|
}
|
|
if (pg_fsync(dstFd) != 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not fsync file \"%s\": %m", fullDst.c_str())));
|
}
|
|
closeTransientFileIfOpened(srcFd);
|
closeTransientFileIfOpened(dstFd);
|
success = true;
|
}
|
PG_CATCH();
|
{
|
closeTransientFileIfOpened(srcFd);
|
closeTransientFileIfOpened(dstFd);
|
|
if (!success) {
|
unlink(fullDst.c_str());
|
}
|
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
PG_RETURN_VOID();
|
}
|
|
/*
|
* @qdb_func: dbms_file_transfer_is_safe_filename(text)
|
*/
|
extern "C" Datum
|
dbms_file_transfer_is_safe_filename(PG_FUNCTION_ARGS)
|
{
|
if (PG_ARGISNULL(0)) {
|
PG_RETURN_BOOL(false);
|
}
|
|
const char* const filename = TextDatumGetCString(PG_GETARG_DATUM(0));
|
PG_RETURN_BOOL(isSafeFilename(filename));
|
}
|
|
/*
|
* @qdb_func: dbms_file_transfer_get_file(text, text, text, text, text)
|
* @ora_func: DBMS_FILE_TRANSFER.GET_FILE(VARCHAR2, VARCHAR2, VARCHAR2, VARCHAR2, VARCHAR2)
|
*/
|
extern "C" Datum
|
dbms_file_transfer_get_file(PG_FUNCTION_ARGS)
|
{
|
const char* const srcDir = TextDatumGetCString(PG_GETARG_DATUM(0));
|
const char* const srcFile = TextDatumGetCString(PG_GETARG_DATUM(1));
|
const char* const connName = TextDatumGetCString(PG_GETARG_DATUM(2));
|
const char* const dstDir = TextDatumGetCString(PG_GETARG_DATUM(3));
|
const char* const dstFile = TextDatumGetCString(PG_GETARG_DATUM(4));
|
|
const bool srcSafe = DatumGetBool(
|
DirectFunctionCall1(dbms_file_transfer_is_safe_filename,
|
CStringGetTextDatum(srcFile)));
|
const bool dstSafe = DatumGetBool(
|
DirectFunctionCall1(dbms_file_transfer_is_safe_filename,
|
CStringGetTextDatum(dstFile)));
|
|
if (!srcSafe || !dstSafe) {
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("DBMS_FILE_TRANSFER: unsafe file name")));
|
}
|
|
const std::string fullDst = joinPath(dstDir, dstFile);
|
int dstFd = -1;
|
bool success = false;
|
|
if (SPI_connect() != SPI_OK_CONNECT) {
|
ereport(ERROR, (errmsg("could not connect to SPI")));
|
}
|
|
PG_TRY();
|
{
|
dstFd = OpenTransientFile(fullDst.c_str(), O_WRONLY | O_CREAT | O_TRUNC | PG_BINARY);
|
if (dstFd < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not open destination file \"%s\": %m", fullDst.c_str())));
|
}
|
|
int64 offset = 0;
|
bool eof = false;
|
|
while (!eof) {
|
CHECK_FOR_INTERRUPTS();
|
|
char* const sqlReadChunk = psprintf("SELECT read_file_chunk(%s, %s, %lld, %d)",
|
quote_literal_cstr(srcDir),
|
quote_literal_cstr(srcFile),
|
static_cast<long long>(offset),
|
kRemoteReadChunkSize);
|
|
char* const sql = psprintf(
|
"SELECT result FROM dblink(%s, %s) AS t(result bytea)",
|
quote_literal_cstr(connName),
|
quote_literal_cstr(sqlReadChunk));
|
|
const int res = SPI_execute(sql, true, 1);
|
pfree(sql);
|
pfree(sqlReadChunk);
|
|
if (res != SPI_OK_SELECT || SPI_processed == 0) {
|
ereport(ERROR, (errmsg("dblink call failed or returned no data")));
|
}
|
|
bool isNull = false;
|
const Datum binData = SPI_getbinval(SPI_tuptable->vals[0],
|
SPI_tuptable->tupdesc,
|
1,
|
&isNull);
|
|
if (isNull) {
|
eof = true;
|
} else {
|
const bytea* const chunk = DatumGetByteaP(binData);
|
const int len = VARSIZE(chunk) - VARHDRSZ;
|
|
if (len == 0) {
|
eof = true;
|
} else {
|
if (write(dstFd, VARDATA(chunk), len) != len) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("write to destination failed: %m")));
|
}
|
|
offset += len;
|
}
|
}
|
|
SPI_freetuptable(SPI_tuptable);
|
}
|
|
if (pg_fsync(dstFd) != 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not fsync file \"%s\": %m", fullDst.c_str())));
|
}
|
|
closeTransientFileIfOpened(dstFd);
|
success = true;
|
}
|
PG_CATCH();
|
{
|
closeTransientFileIfOpened(dstFd);
|
|
if (!success) {
|
unlink(fullDst.c_str());
|
}
|
|
SPI_finish();
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
SPI_finish();
|
PG_RETURN_VOID();
|
}
|
|
/*
|
* @qdb_func: dbms_file_transfer_put_file(text, text, text, text, text)
|
* @ora_func: DBMS_FILE_TRANSFER.PUT_FILE(VARCHAR2, VARCHAR2, VARCHAR2, VARCHAR2, VARCHAR2)
|
*/
|
extern "C" Datum
|
dbms_file_transfer_put_file(PG_FUNCTION_ARGS)
|
{
|
const char* const srcDir = TextDatumGetCString(PG_GETARG_DATUM(0));
|
const char* const srcFile = TextDatumGetCString(PG_GETARG_DATUM(1));
|
const char* const connName = TextDatumGetCString(PG_GETARG_DATUM(2));
|
const char* const dstDir = TextDatumGetCString(PG_GETARG_DATUM(3));
|
const char* const dstFile = TextDatumGetCString(PG_GETARG_DATUM(4));
|
|
const bool srcSafe = DatumGetBool(
|
DirectFunctionCall1(dbms_file_transfer_is_safe_filename,
|
CStringGetTextDatum(srcFile)));
|
const bool dstSafe = DatumGetBool(
|
DirectFunctionCall1(dbms_file_transfer_is_safe_filename,
|
CStringGetTextDatum(dstFile)));
|
|
if (!srcSafe || !dstSafe) {
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("DBMS_FILE_TRANSFER: unsafe file name")));
|
}
|
|
const std::string fullSrc = joinPath(srcDir, srcFile);
|
int srcFd = OpenTransientFile(fullSrc.c_str(), O_RDONLY | PG_BINARY);
|
bool success = false;
|
|
if (srcFd < 0) {
|
ereport(ERROR, (errmsg("could not open source file \"%s\": %m", fullSrc.c_str())));
|
}
|
|
if (SPI_connect() != SPI_OK_CONNECT) {
|
closeTransientFileIfOpened(srcFd);
|
ereport(ERROR, (errmsg("SPI connect failed")));
|
}
|
|
char buffer[kRemoteWriteChunkSize];
|
int64 offset = 0;
|
|
PG_TRY();
|
{
|
ssize_t nread = 0;
|
|
while ((nread = read(srcFd, buffer, sizeof(buffer))) > 0) {
|
CHECK_FOR_INTERRUPTS();
|
|
const int hexLen = static_cast<int>(nread) * 2;
|
char* const hexData = static_cast<char*>(palloc(hexLen + 1));
|
hex_encode(buffer, nread, hexData);
|
hexData[hexLen] = '\0';
|
|
char* const remoteSql = psprintf(
|
"DO $$ BEGIN PERFORM write_file_chunk(%s, %s, decode('%s', 'hex'), %lld); END $$",
|
quote_literal_cstr(dstDir),
|
quote_literal_cstr(dstFile),
|
hexData,
|
static_cast<long long>(offset));
|
|
char* const dblinkSql = psprintf("SELECT dblink_exec(%s, %s)",
|
quote_literal_cstr(connName),
|
quote_literal_cstr(remoteSql));
|
|
if (SPI_execute(dblinkSql, false, 0) != SPI_OK_SELECT) {
|
pfree(dblinkSql);
|
pfree(remoteSql);
|
pfree(hexData);
|
ereport(ERROR, (errmsg("remote write failed via dblink")));
|
}
|
|
offset += nread;
|
|
pfree(dblinkSql);
|
pfree(remoteSql);
|
pfree(hexData);
|
}
|
|
if (nread < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not read from file \"%s\": %m", fullSrc.c_str())));
|
}
|
|
closeTransientFileIfOpened(srcFd);
|
success = true;
|
}
|
PG_CATCH();
|
{
|
closeTransientFileIfOpened(srcFd);
|
|
if (!success) {
|
char* const rmSql = psprintf(
|
"DO $$ BEGIN PERFORM pg_file_unlink_custom(%s || '/' || %s); EXCEPTION WHEN OTHERS THEN NULL; END $$",
|
quote_literal_cstr(dstDir),
|
quote_literal_cstr(dstFile));
|
|
char* const dblinkRm = psprintf("SELECT dblink_exec(%s, %s)",
|
quote_literal_cstr(connName),
|
quote_literal_cstr(rmSql));
|
|
SPI_execute(dblinkRm, false, 0);
|
pfree(dblinkRm);
|
pfree(rmSql);
|
}
|
|
SPI_finish();
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
SPI_finish();
|
PG_RETURN_VOID();
|
}
|
|
extern "C" Datum
|
read_file_chunk(PG_FUNCTION_ARGS)
|
{
|
const char* const dir = TextDatumGetCString(PG_GETARG_DATUM(0));
|
const char* const filename = TextDatumGetCString(PG_GETARG_DATUM(1));
|
const int64 offset = PG_GETARG_INT64(2);
|
const int32 limit = PG_GETARG_INT32(3);
|
|
if (limit <= 0) {
|
PG_RETURN_NULL();
|
}
|
|
const std::string fullPath = joinPath(dir, filename);
|
bytea* result = nullptr;
|
int fd = -1;
|
|
PG_TRY();
|
{
|
fd = OpenTransientFile(fullPath.c_str(), O_RDONLY | PG_BINARY);
|
if (fd < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not open file \"%s\": %m", fullPath.c_str())));
|
}
|
|
if (lseek(fd, offset, SEEK_SET) < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not seek to offset %lld in file \"%s\": %m",
|
static_cast<long long>(offset),
|
fullPath.c_str())));
|
}
|
|
result = static_cast<bytea*>(palloc(limit + VARHDRSZ));
|
|
const ssize_t nread = read(fd, VARDATA(result), limit);
|
if (nread < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not read file \"%s\": %m", fullPath.c_str())));
|
}
|
|
SET_VARSIZE(result, static_cast<int>(nread) + VARHDRSZ);
|
closeTransientFileIfOpened(fd);
|
}
|
PG_CATCH();
|
{
|
closeTransientFileIfOpened(fd);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
PG_RETURN_BYTEA_P(result);
|
}
|
|
extern "C" Datum
|
write_file_chunk(PG_FUNCTION_ARGS)
|
{
|
const char* const dir = TextDatumGetCString(PG_GETARG_DATUM(0));
|
const char* const filename = TextDatumGetCString(PG_GETARG_DATUM(1));
|
const bytea* const data = PG_GETARG_BYTEA_PP(2);
|
const int64 offset = PG_GETARG_INT64(3);
|
|
const std::string fullPath = joinPath(dir, filename);
|
const int len = VARSIZE_ANY_EXHDR(data);
|
const char* const dataPtr = VARDATA_ANY(data);
|
int fd = -1;
|
|
PG_TRY();
|
{
|
int flags = O_WRONLY | PG_BINARY;
|
if (offset == 0) {
|
flags |= O_CREAT | O_TRUNC;
|
}
|
|
fd = OpenTransientFile(fullPath.c_str(), flags);
|
if (fd < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("could not open file \"%s\": %m", fullPath.c_str())));
|
}
|
|
if (lseek(fd, offset, SEEK_SET) < 0) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("seek failed in \"%s\": %m", fullPath.c_str())));
|
}
|
|
if (write(fd, dataPtr, len) != len) {
|
ereport(ERROR,
|
(errcode_for_file_access(),
|
errmsg("write failed in \"%s\": %m", fullPath.c_str())));
|
}
|
|
closeTransientFileIfOpened(fd);
|
}
|
PG_CATCH();
|
{
|
closeTransientFileIfOpened(fd);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
PG_RETURN_VOID();
|
}
|
|
extern "C" Datum
|
pg_file_unlink_custom(PG_FUNCTION_ARGS)
|
{
|
const char* const filename = TextDatumGetCString(PG_GETARG_DATUM(0));
|
|
if (unlink(filename) < 0 && errno != ENOENT) {
|
ereport(WARNING, (errmsg("could not remove file \"%s\": %m", filename)));
|
}
|
|
PG_RETURN_VOID();
|
}
|
|
extern "C" Datum
|
dbms_file_transfer_get_version(PG_FUNCTION_ARGS)
|
{
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_DBMS_FILE_TRANSFER_VERSION));
|
}
|