Skip to content

Commit

Permalink
Added -b (--background) option
Browse files Browse the repository at this point in the history
Relates to #10
  • Loading branch information
maxsatula committed Apr 10, 2015
1 parent 9632e0d commit c0b864b
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 3 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
2015-04-10 Max Satula <[email protected]>

* Populate MODULE and ACTION columns of V$SESSION
* Added -b (--background) option

2015-04-09 Max Satula <[email protected]>

Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ ocp 0.1
* first alpha version of the tool
* added -i (--interactive) and -f (--force) options
* added --keep-partial and -c (--continue) options
* added -b (--background) option

----------------------------------------------------------------------------
178 changes: 175 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,172 @@ END;\
SetSessionAction(oraAllInOne, 0);
}

void SubmitCompressJob(struct ORACLEALLINONE *oraAllInOne, char* pDirectory, int compressionLevel,
char* pOriginalFileName, char* pCompressedFileName)
{
ub4 vCompressionLevel;
char vJobName[ORA_IDENTIFIER_SIZE + 1];

struct BINDVARIABLE oraBindsCompress[] =
{
{ 0, SQLT_STR, ":job_name", vJobName, ORA_IDENTIFIER_SIZE + 1 },
{ 0, SQLT_STR, ":directory", pDirectory, ORA_IDENTIFIER_SIZE + 1 },
{ 0, SQLT_INT, ":compression_level", &vCompressionLevel, sizeof(vCompressionLevel) },
{ 0, SQLT_STR, ":original_filename" , pOriginalFileName, MAX_FMT_SIZE },
{ 0, SQLT_STR, ":compressed_filename", pCompressedFileName, MAX_FMT_SIZE }
};

struct ORACLESTATEMENT oraStmtCompress = { "\
DECLARE\
safe_directory_ varchar2(60);\
safe_compression_level_ varchar2(1);\
safe_original_filename_ varchar2(512);\
safe_compressed_filename_ varchar2(512);\
BEGIN\
safe_directory_ := dbms_assert.enquote_literal(''''||replace(:directory, '''', '''''')||'''');\
safe_compression_level_ := to_char(:compression_level, 'TM', 'NLS_Numeric_Characters = ''.,''');\
safe_original_filename_ := dbms_assert.enquote_literal(''''||replace(:original_filename, '''', '''''')||'''');\
safe_compressed_filename_ := dbms_assert.enquote_literal(''''||replace(:compressed_filename, '''', '''''')||'''');\
\
:job_name := dbms_scheduler.generate_job_name('OCP_GZIP_');\
dbms_scheduler.create_job (\
job_name => :job_name,\
job_type => 'PLSQL_BLOCK',\
enabled => TRUE,\
comments => 'ocp compression job',\
job_action => '\
DECLARE\
f_handle UTL_FILE.FILE_TYPE;\
c_handle BINARY_INTEGER;\
raw_buffer RAW(32767);\
blob_buffer BLOB;\
actual_size NUMBER;\
pos BINARY_INTEGER;\
blobsize BINARY_INTEGER; \
BEGIN\
f_handle := UTL_FILE.FOPEN(' || safe_directory_ || ', ' || safe_original_filename_ || ', ''rb'');\
DBMS_LOB.CREATETEMPORARY(blob_buffer, TRUE, DBMS_LOB.CALL);\
' || case when :compression_level > 0 then \
'c_handle := UTL_COMPRESS.LZ_COMPRESS_OPEN(blob_buffer, ' || safe_compression_level_ || ');'\
else\
'c_handle := UTL_COMPRESS.LZ_COMPRESS_OPEN(blob_buffer);'\
end || '\
LOOP\
BEGIN\
UTL_FILE.GET_RAW(f_handle, raw_buffer, 16384);\
UTL_COMPRESS.LZ_COMPRESS_ADD(c_handle, blob_buffer, raw_buffer);\
EXCEPTION\
WHEN NO_DATA_FOUND THEN\
EXIT;\
END;\
END LOOP;\
UTL_COMPRESS.LZ_COMPRESS_CLOSE(c_handle, blob_buffer);\
UTL_FILE.FCLOSE(f_handle);\
\
f_handle := UTL_FILE.FOPEN(' || safe_directory_ || ', ' || safe_compressed_filename_ || ', ''wb'');\
pos := 0;\
blobsize := DBMS_LOB.GETLENGTH(blob_buffer);\
WHILE pos < blobsize LOOP\
actual_size := LEAST(blobsize - pos, 16384);\
DBMS_LOB.READ(blob_buffer, actual_size, pos + 1, raw_buffer);\
UTL_FILE.PUT_RAW(f_handle, raw_buffer);\
pos := pos + actual_size;\
END LOOP;\
DBMS_LOB.FREETEMPORARY(blob_buffer);\
UTL_FILE.FCLOSE(f_handle);\
END;\
');\
END;",
0, oraBindsCompress, sizeof(oraBindsCompress)/sizeof(struct BINDVARIABLE), 0, 0 };

vCompressionLevel = compressionLevel;

PrepareStmtAndBind(oraAllInOne, &oraStmtCompress);

if (ExecuteStmt(oraAllInOne))
ExitWithError(oraAllInOne, 4, ERROR_OCI, "Failed to submit a compression job\n");

printf("Submitted a job %s\n", vJobName);
ReleaseStmt(oraAllInOne);
}

void SubmitUncompressJob(struct ORACLEALLINONE *oraAllInOne, char* pDirectory,
char* pOriginalFileName, char* pUncompressedFileName)
{
char vJobName[ORA_IDENTIFIER_SIZE + 1];

struct BINDVARIABLE oraBindsUncompress[] =
{
{ 0, SQLT_STR, ":job_name", vJobName, ORA_IDENTIFIER_SIZE + 1 },
{ 0, SQLT_STR, ":directory", pDirectory, ORA_IDENTIFIER_SIZE + 1 },
{ 0, SQLT_STR, ":original_filename" , pOriginalFileName, MAX_FMT_SIZE },
{ 0, SQLT_STR, ":uncompressed_filename", pUncompressedFileName, MAX_FMT_SIZE }
};

struct ORACLESTATEMENT oraStmtUncompress = { "\
DECLARE\
safe_directory_ varchar2(60);\
safe_original_filename_ varchar2(512);\
safe_uncompressed_filename_ varchar2(512);\
BEGIN\
safe_directory_ := dbms_assert.enquote_literal(''''||replace(:directory, '''', '''''')||'''');\
safe_original_filename_ := dbms_assert.enquote_literal(''''||replace(:original_filename, '''', '''''')||'''');\
safe_uncompressed_filename_ := dbms_assert.enquote_literal(''''||replace(:uncompressed_filename, '''', '''''')||'''');\
\
:job_name := dbms_scheduler.generate_job_name('OCP_GUNZIP_');\
dbms_scheduler.create_job (\
job_name => :job_name,\
job_type => 'PLSQL_BLOCK',\
enabled => TRUE,\
comments => 'ocp decompression job',\
job_action => '\
DECLARE\
f_handle UTL_FILE.FILE_TYPE;\
c_handle BINARY_INTEGER;\
raw_buffer RAW(32767);\
blob_buffer BLOB; \
BEGIN\
DBMS_LOB.CREATETEMPORARY(blob_buffer, TRUE, DBMS_LOB.CALL);\
f_handle := UTL_FILE.FOPEN(' || safe_directory_ || ', ' || safe_original_filename_ || ', ''rb'');\
LOOP\
BEGIN\
UTL_FILE.GET_RAW(f_handle, raw_buffer, 16384);\
DBMS_LOB.WRITEAPPEND(blob_buffer, UTL_RAW.LENGTH(raw_buffer), raw_buffer);\
EXCEPTION\
WHEN NO_DATA_FOUND THEN\
EXIT;\
END;\
END LOOP;\
UTL_FILE.FCLOSE(f_handle);\
\
c_handle := UTL_COMPRESS.LZ_UNCOMPRESS_OPEN(blob_buffer);\
f_handle := UTL_FILE.FOPEN(' || safe_directory_ || ', ' || safe_uncompressed_filename_ || ', ''wb'');\
LOOP\
BEGIN\
UTL_COMPRESS.LZ_UNCOMPRESS_EXTRACT(c_handle, raw_buffer);\
UTL_FILE.PUT_RAW(f_handle, raw_buffer);\
EXCEPTION\
WHEN NO_DATA_FOUND THEN\
EXIT;\
END;\
END LOOP;\
UTL_FILE.FCLOSE(f_handle);\
UTL_COMPRESS.LZ_UNCOMPRESS_CLOSE(c_handle);\
DBMS_LOB.FREETEMPORARY(blob_buffer);\
END;\
');\
END;",
0, oraBindsUncompress, sizeof(oraBindsUncompress)/sizeof(struct BINDVARIABLE), 0, 0 };

PrepareStmtAndBind(oraAllInOne, &oraStmtUncompress);

if (ExecuteStmt(oraAllInOne))
ExitWithError(oraAllInOne, 4, ERROR_OCI, "Failed to submit a decompression job\n");

printf("Submitted a job %s\n", vJobName);
ReleaseStmt(oraAllInOne);
}

void DownloadFileWithCompression(struct ORACLEALLINONE *oraAllInOne, char* pDirectory,
int compressionLevel, char* pRemoteFile, char* pLocalFile,
int isKeepPartial, int isResume)
Expand Down Expand Up @@ -1179,7 +1345,7 @@ SELECT t.file_name,\
{ NULL, '7', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, 0, 0x87 },
{ NULL, '8', POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, 0, 0x88 },
{ "best", '9', POPT_ARG_NONE, 0, 0x89, "Best compression method" },
{ "background", 'b', POPT_ARG_VAL, &programOptions.isBackground, 1, "Submit Oracle Scheduler job and exit immediately" },
{ "background", 'b', POPT_ARG_VAL, &programOptions.isBackground, 1, "Submit an Oracle Scheduler job and exit immediately" },
POPT_TABLEEND
};

Expand Down Expand Up @@ -1493,13 +1659,19 @@ SELECT t.file_name,\
GetOracleFileAttr(&oraAllInOne, vDirectory, vLocalFile, &oracleFileAttr);
if (oracleFileAttr.bExists)
ConfirmOverwrite(&oraAllInOne, &programOptions, vLocalFile);
Compress(&oraAllInOne, vDirectory, programOptions.compressionLevel, vRemoteFile, vLocalFile);
if (programOptions.isBackground)
SubmitCompressJob(&oraAllInOne, vDirectory, programOptions.compressionLevel, vRemoteFile, vLocalFile);
else
Compress(&oraAllInOne, vDirectory, programOptions.compressionLevel, vRemoteFile, vLocalFile);
break;
case ACTION_GUNZIP:
GetOracleFileAttr(&oraAllInOne, vDirectory, vLocalFile, &oracleFileAttr);
if (oracleFileAttr.bExists)
ConfirmOverwrite(&oraAllInOne, &programOptions, vLocalFile);
Uncompress(&oraAllInOne, vDirectory, vRemoteFile, vLocalFile);
if (programOptions.isBackground)
SubmitUncompressJob(&oraAllInOne, vDirectory, vRemoteFile, vLocalFile);
else
Uncompress(&oraAllInOne, vDirectory, vRemoteFile, vLocalFile);
break;
default:
/* TODO: remove when everything is implemented */
Expand Down

0 comments on commit c0b864b

Please sign in to comment.