Skip to content

Commit

Permalink
Merge pull request #11344 from customerio/issue_1537_release_7_3_fix
Browse files Browse the repository at this point in the history
Hex decode add-prefix and remove-prefix for fdbrestore.
  • Loading branch information
jzhou77 committed Apr 30, 2024
2 parents 8e9f859 + 146161a commit e3a38fa
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 59 deletions.
2 changes: 2 additions & 0 deletions fdbbackup/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(FDBBACKUP_SRCS
BackupTLSConfig.cpp
Decode.cpp
backup.actor.cpp)

add_flow_target(EXECUTABLE NAME fdbbackup SRCS ${FDBBACKUP_SRCS})
Expand All @@ -13,6 +14,7 @@ target_include_directories(fdbconvert PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inclu
target_link_libraries(fdbconvert PRIVATE fdbclient)

set(FDBDECODE_SRCS
Decode.cpp
BackupTLSConfig.cpp
FileDecoder.actor.cpp)
add_flow_target(EXECUTABLE NAME fdbdecode SRCS ${FDBDECODE_SRCS})
Expand Down
78 changes: 78 additions & 0 deletions fdbbackup/Decode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Decode.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "fdbbackup/Decode.h"

#include <iostream>

// Decode an ASCII string, e.g., "\x15\x1b\x19\x04\xaf\x0c\x28\x0a",
// into the binary string. Set "err" to true if the format is invalid.
// Note ',' '\' '," ';' are escaped by '\'. Normal characters can be
// unencoded into HEX, but not recommended.
std::string decode_hex_string(std::string line, bool& err) {
size_t i = 0;
std::string ret;

while (i <= line.length()) {
switch (line[i]) {
case '\\':
if (i + 2 > line.length()) {
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
switch (line[i + 1]) {
char ent, save;
case '"':
case '\\':
case ' ':
case ';':
line.erase(i, 1);
break;
case 'x':
if (i + 4 > line.length()) {
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
char* pEnd;
save = line[i + 4];
line[i + 4] = 0;
ent = char(strtoul(line.data() + i + 2, &pEnd, 16));
if (*pEnd) {
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
line[i + 4] = save;
line.replace(i, 4, 1, ent);
break;
default:
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
default:
i++;
}
}

return line.substr(0, i);
}
2 changes: 1 addition & 1 deletion fdbbackup/FileConverter.actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,4 @@ int main(int argc, char** argv) {
TraceEvent(SevError, "MainError").error(unknown_error()).detail("RootException", e.what());
return FDB_EXIT_MAIN_EXCEPTION;
}
}
}
55 changes: 1 addition & 54 deletions fdbbackup/FileDecoder.actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "fdbbackup/BackupTLSConfig.h"
#include "fdbclient/BuildFlags.h"
#include "fdbbackup/FileConverter.h"
#include "fdbbackup/Decode.h"
#include "fdbclient/BackupAgent.actor.h"
#include "fdbclient/BackupContainer.h"
#include "fdbclient/BackupContainerFileSystem.h"
Expand Down Expand Up @@ -247,60 +248,6 @@ struct DecodeParams : public ReferenceCounted<DecodeParams> {
}
};

// Decode an ASCII string, e.g., "\x15\x1b\x19\x04\xaf\x0c\x28\x0a",
// into the binary string. Set "err" to true if the format is invalid.
// Note ',' '\' '," ';' are escaped by '\'. Normal characters can be
// unencoded into HEX, but not recommended.
std::string decode_hex_string(std::string line, bool& err) {
size_t i = 0;
std::string ret;

while (i <= line.length()) {
switch (line[i]) {
case '\\':
if (i + 2 > line.length()) {
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
switch (line[i + 1]) {
char ent, save;
case '"':
case '\\':
case ' ':
case ';':
line.erase(i, 1);
break;
case 'x':
if (i + 4 > line.length()) {
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
char* pEnd;
save = line[i + 4];
line[i + 4] = 0;
ent = char(strtoul(line.data() + i + 2, &pEnd, 16));
if (*pEnd) {
std::cerr << "Invalid hex string at: " << i << "\n";
return ret;
}
line[i + 4] = save;
line.replace(i, 4, 1, ent);
break;
default:
std::cerr << "Invalid hex string at: " << i << "\n";
err = true;
return ret;
}
default:
i++;
}
}

return line.substr(0, i);
}

// Parses and returns a ";" separated HEX encoded strings. So the ";" in
// the string should be escaped as "\;".
// Sets "err" to true if there is any parsing error.
Expand Down
23 changes: 19 additions & 4 deletions fdbbackup/backup.actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "flow/ApiVersion.h"
#include "fmt/format.h"
#include "fdbbackup/BackupTLSConfig.h"
#include "fdbbackup/Decode.h"
#include "fdbclient/JsonBuilder.h"
#include "flow/Arena.h"
#include "flow/ArgParseUtil.h"
Expand Down Expand Up @@ -3825,12 +3826,26 @@ int main(int argc, char* argv[]) {
case OPT_DESCRIBE_TIMESTAMPS:
describeTimestamps = true;
break;
case OPT_PREFIX_ADD:
addPrefix = args->OptionArg();
case OPT_PREFIX_ADD: {
bool err = false;
addPrefix = decode_hex_string(args->OptionArg(), err);
if (err) {
fprintf(stderr, "ERROR: Could not parse add prefix\n");
printHelpTeaser(argv[0]);
return FDB_EXIT_ERROR;
}
break;
case OPT_PREFIX_REMOVE:
removePrefix = args->OptionArg();
}
case OPT_PREFIX_REMOVE: {
bool err = false;
removePrefix = decode_hex_string(args->OptionArg(), err);
if (err) {
fprintf(stderr, "ERROR: Could not parse remove prefix\n");
printHelpTeaser(argv[0]);
return FDB_EXIT_ERROR;
}
break;
}
case OPT_ERRORLIMIT: {
const char* a = args->OptionArg();
if (!sscanf(a, "%d", &maxErrors)) {
Expand Down
29 changes: 29 additions & 0 deletions fdbbackup/include/fdbbackup/Decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Decode.h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FDBBACKUP_DECODE_H
#define FDBBACKUP_DECODE_H
#pragma once

#include <string>

std::string decode_hex_string(std::string line, bool& err);

#endif // FDBBACKUP_DECODE_H

0 comments on commit e3a38fa

Please sign in to comment.