Skip to content

Commit 217d27b

Browse files
dwelch-spikeJesse S
andauthored
build: tools-2643 add make shared, builds asbackup as a dynamic lib (#66)
* build: add make shared, builds asbackup as a dynamic lib * feat: expose restore_run and backup_run for dynamic lib use * tools-2667 set s3 configs in run functions Co-authored-by: Jesse S <[email protected]> --------- Co-authored-by: Jesse S <[email protected]>
1 parent 5ec01ff commit 217d27b

File tree

8 files changed

+252
-130
lines changed

8 files changed

+252
-130
lines changed

Makefile

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ ifdef M1_HOME_BREW
4848
OPENSSL_PREFIX = /opt/homebrew/opt/openssl
4949
endif
5050

51+
ifeq ($(OS),Darwin)
52+
DYNAMIC_SUFFIX = dylib
53+
DYNAMIC_FLAG = -dynamiclib
54+
else
55+
DYNAMIC_SUFFIX = so
56+
DYNAMIC_FLAG = -shared
57+
endif
58+
DYNAMIC_OPTIONS =
59+
5160
CC ?= cc
5261

5362
DWARF := $(shell $(CC) -Wall -Wextra -O2 -o /tmp/asflags_$${$$} src/flags.c; \
@@ -279,6 +288,9 @@ BACKUP := $(DIR_BIN)/asbackup
279288
RESTORE := $(DIR_BIN)/asrestore
280289
TOML := $(DIR_TOML)/libtoml.a
281290

291+
BACKUP_DYNAMIC := $(DIR_BIN)/asbackup.$(DYNAMIC_SUFFIX)
292+
RESTORE_DYNAMIC := $(DIR_BIN)/asrestore.$(DYNAMIC_SUFFIX)
293+
282294
SRCS := $(BACKUP_SRC) $(RESTORE_SRC)
283295
OBJS := $(BACKUP_OBJ) $(RESTORE_OBJ)
284296
DEPS := $(BACKUP_DEP) $(RESTORE_DEP)
@@ -320,11 +332,24 @@ TEST_DEPS := $(sort $(TEST_DEPS))
320332
.PHONY: all
321333
all: $(BINS)
322334

335+
# used as a pre-requisite for make shared
336+
# this rule is not meant for manual use by a user
337+
.PHONY: _set_dynamic_options
338+
_set_dynamic_options: $(TOML)
339+
$(eval DYNAMIC_OPTIONS = -fPIC)
340+
341+
# builds asbackup and asrestore as shared libraries
342+
# asbackup is designed as a standalone exe, use at your own risk
343+
# run this with the same options you would use in a normal build
344+
.PHONY: shared
345+
shared: _set_dynamic_options $(BACKUP_DYNAMIC) $(RESTORE_DYNAMIC)
346+
$(eval DYNAMIC_OPTIONS =)
347+
323348
.PHONY: clean
324349
clean:
325350
$(MAKE) -C $(DIR_TOML) clean
326351
$(MAKE) -C $(DIR_C_CLIENT) clean
327-
rm -f $(DEPS) $(OBJS) $(BINS) $(TEST_OBJS) $(TEST_DEPS) $(TEST_BINS)
352+
rm -f $(DEPS) $(OBJS) $(BINS) $(TEST_OBJS) $(TEST_DEPS) $(TEST_BINS) $(BACKUP_DYNAMIC) $(RESTORE_DYNAMIC)
328353
if [ -d $(DIR_OBJ) ]; then rmdir $(DIR_OBJ); fi
329354
if [ -d $(DIR_BIN) ]; then rmdir $(DIR_BIN); fi
330355
if [ -d $(DIR_TEST_OBJ) ]; then rm -r $(DIR_TEST_OBJ); fi
@@ -367,17 +392,23 @@ $(DIR_BIN):
367392
mkdir $(DIR_BIN)
368393

369394
$(DIR_OBJ)/%_c.o: $(DIR_SRC)/%.c | $(DIR_OBJ)
370-
$(CC) $(CFLAGS) -MMD -o $@ -c $(INCLUDES) $<
395+
$(CC) $(DYNAMIC_OPTIONS) $(CFLAGS) -MMD -o $@ -c $(INCLUDES) $<
371396

372397
$(DIR_OBJ)/%_cc.o: $(DIR_SRC)/%.cc | $(DIR_OBJ)
373-
$(CXX) $(CXXFLAGS) -MMD -o $@ -c $(INCLUDES) $<
398+
$(CXX) $(DYNAMIC_OPTIONS) $(CXXFLAGS) -MMD -o $@ -c $(INCLUDES) $<
374399

375400
$(BACKUP): $(BACKUP_OBJ) $(TOML) $(C_CLIENT_LIB) | $(DIR_BIN)
376401
$(CXX) $(LDFLAGS) -o $(BACKUP) $(BACKUP_OBJ) $(LIBRARIES)
377402

378403
$(RESTORE): $(RESTORE_OBJ) $(TOML) $(C_CLIENT_LIB) | $(DIR_BIN)
379404
$(CXX) $(LDFLAGS) -o $(RESTORE) $(RESTORE_OBJ) $(LIBRARIES)
380405

406+
$(BACKUP_DYNAMIC): $(BACKUP_OBJ) $(TOML) $(C_CLIENT_LIB) | $(DIR_BIN)
407+
$(CXX) $(DYNAMIC_FLAG) $(LDFLAGS) -o $(BACKUP_DYNAMIC) $(BACKUP_OBJ) $(LIBRARIES)
408+
409+
$(RESTORE_DYNAMIC): $(RESTORE_OBJ) $(TOML) $(C_CLIENT_LIB) | $(DIR_BIN)
410+
$(CXX) $(DYNAMIC_FLAG) $(LDFLAGS) -o $(RESTORE_DYNAMIC) $(RESTORE_OBJ) $(LIBRARIES)
411+
381412
$(TOML):
382413
$(MAKE) -C $(DIR_TOML)
383414

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ apt-get update
4141
apt-get install build-essential libssl-dev libuv1-dev libcurl4-openssl-dev libzstd-dev
4242

4343
# for aws-sdk-cpp build
44-
apt-get install cmake
44+
apt-get install cmake pkg-config zlib1g-dev
4545

4646
# download aws sdk
4747
git clone https://github.com/aws/aws-sdk-cpp.git
@@ -96,7 +96,7 @@ mkdir build
9696
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_ONLY="s3" -DBUILD_SHARED_LIBS=ON -DENABLE_TESTING=OFF -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_INSTALL_LIBDIR=lib
9797
make -C build
9898

99-
# install aws static sdk
99+
# install aws dynamic sdk
100100
cd build
101101
make install
102102
cd ../..

include/backup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ extern "C" {
7171
// Estimate total backup file sizes with 99.9% confidence.
7272
#define BACKUP_FILE_ESTIMATE_CONFIDENCE_LEVEL 0.999
7373

74+
#define RUN_BACKUP_SUCCESS ((void*) 0)
75+
#define RUN_BACKUP_FAILURE ((void*) -1lu)
76+
7477
/*
7578
* The struct used to maintain state information about a backup file which was
7679
* not completely filled from a backup task
@@ -87,6 +90,7 @@ typedef struct queued_backup_fd {
8790
//
8891

8992
int32_t backup_main(int32_t argc, char **argv);
93+
backup_status_t* backup_run(backup_config_t* conf);
9094

9195
/*
9296
* Returns the backup config/status struct being used by the currently running

include/restore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ extern "C" {
6666

6767
// The interval for logging per-thread timing stats.
6868
#define STAT_INTERVAL 10
69+
#define RUN_RESTORE_FAILURE ((void*) -1lu)
6970

7071
/*
7172
* The backup file information pushed to the job queue and picked up by the restore threads.
@@ -142,6 +143,7 @@ typedef enum {
142143
//
143144

144145
int32_t restore_main(int32_t argc, char **argv);
146+
restore_status_t* restore_run(restore_config_t *conf);
145147

146148
#ifdef __cplusplus
147149
}

src/backup.c

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ typedef struct backup_globals {
5050
// of the vector).
5151
static as_vector g_globals;
5252

53-
#define RUN_BACKUP_SUCCESS ((void*) 0)
54-
#define RUN_BACKUP_FAILURE ((void*) -1lu)
55-
56-
5753
//==========================================================
5854
// Forward Declarations.
5955
//
6056

61-
static backup_status_t* run_backup(backup_config_t* conf);
57+
static backup_status_t* start_backup(backup_config_t* conf);
6258

6359
typedef struct distr_stats {
6460
uint64_t total;
@@ -195,6 +191,7 @@ static void show_estimate(FILE* mach_fd, uint64_t* samples, uint32_t n_samples,
195191
uint64_t rec_count_estimate, io_write_proxy_t* fd);
196192
static void sig_hand(int32_t sig);
197193
static void no_op(int32_t sig);
194+
static void set_s3_configs(const backup_config_t*);
198195

199196

200197
//==========================================================
@@ -220,7 +217,7 @@ backup_main(int32_t argc, char **argv)
220217
goto cleanup;
221218
}
222219

223-
backup_status_t* status = run_backup(&conf);
220+
backup_status_t* status = start_backup(&conf);
224221
if (status == RUN_BACKUP_SUCCESS) {
225222
res = EXIT_SUCCESS;
226223
}
@@ -234,14 +231,37 @@ backup_main(int32_t argc, char **argv)
234231

235232
cleanup:
236233
file_proxy_cloud_shutdown();
237-
238234
as_vector_destroy(&g_globals);
239-
240235
ver("Exiting with status code %d", res);
241-
242236
return res;
243237
}
244238

239+
/*
240+
* FOR USE WITH ASBACKUP AS A LIBRARY (Use at your own risk)
241+
*
242+
* Runs a backup job with the given configuration. This method is not thread
243+
* safe and should not be called multiple times in parallel, as it uses global
244+
* variables to handle signal interruption.
245+
*
246+
* The passed in backup config must be destroyed by the caller using backup_config_destroy()
247+
* To enable C client logging, call enable_client_log() before calling this function
248+
*
249+
* Returns the backup_status struct used during the run which must be freed by the
250+
* caller using backup_status_destroy(), then free().
251+
* Only free the return value if it is != RUN_BACKUP_FAILURE || != RUN_BACKUP_SUCCESS
252+
*/
253+
backup_status_t*
254+
backup_run(backup_config_t* conf) {
255+
as_vector_init(&g_globals, sizeof(backup_globals_t), 1);
256+
257+
backup_status_t* status = start_backup(conf);
258+
259+
file_proxy_cloud_shutdown();
260+
as_vector_destroy(&g_globals);
261+
262+
return status;
263+
}
264+
245265
backup_config_t*
246266
get_g_backup_conf(void)
247267
{
@@ -272,7 +292,7 @@ get_g_backup_status(void)
272292
* caller).
273293
*/
274294
static backup_status_t*
275-
run_backup(backup_config_t* conf)
295+
start_backup(backup_config_t* conf)
276296
{
277297
int32_t res = EXIT_FAILURE;
278298
bool do_backup_save_state = false;
@@ -281,6 +301,8 @@ run_backup(backup_config_t* conf)
281301

282302
push_backup_globals(conf, NULL);
283303

304+
set_s3_configs(conf);
305+
284306
if (conf->remove_artifacts) {
285307

286308
if (conf->output_file != NULL) {
@@ -491,14 +513,14 @@ run_backup(backup_config_t* conf)
491513

492514
bool cur_silent_val = g_silent;
493515
g_silent = true;
494-
backup_status_t* estimate_status = run_backup(estimate_conf);
516+
backup_status_t* estimate_status = start_backup(estimate_conf);
495517
g_silent = cur_silent_val;
496518

497519
backup_config_destroy(estimate_conf);
498520
cf_free(estimate_conf);
499521

500522
// re-enable signal handling, since it was disabled at the end of
501-
// the estimate run in run_backup.
523+
// the estimate run in start_backup.
502524
set_sigaction(sig_hand);
503525

504526
if (estimate_status == RUN_BACKUP_FAILURE) {
@@ -2558,3 +2580,23 @@ no_op(int32_t sig)
25582580
(void) sig;
25592581
}
25602582

2583+
static void
2584+
set_s3_configs(const backup_config_t* conf)
2585+
{
2586+
if (conf->s3_region != NULL) {
2587+
s3_set_region(conf->s3_region);
2588+
}
2589+
2590+
if (conf->s3_profile != NULL) {
2591+
s3_set_profile(conf->s3_profile);
2592+
}
2593+
2594+
if (conf->s3_endpoint_override != NULL) {
2595+
s3_set_endpoint(conf->s3_endpoint_override);
2596+
}
2597+
2598+
s3_set_max_async_downloads(conf->s3_max_async_downloads);
2599+
s3_set_max_async_uploads(conf->s3_max_async_uploads);
2600+
s3_set_connect_timeout_ms(conf->s3_connect_timeout);
2601+
s3_set_log_level(conf->s3_log_level);
2602+
}

src/backup_config.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -744,23 +744,6 @@ backup_config_init(int argc, char* argv[], backup_config_t* conf)
744744
return BACKUP_CONFIG_INIT_FAILURE;
745745
}
746746

747-
if (conf->s3_region != NULL) {
748-
s3_set_region(conf->s3_region);
749-
}
750-
751-
if (conf->s3_profile != NULL) {
752-
s3_set_profile(conf->s3_profile);
753-
}
754-
755-
if (conf->s3_endpoint_override != NULL) {
756-
s3_set_endpoint(conf->s3_endpoint_override);
757-
}
758-
759-
s3_set_max_async_downloads(conf->s3_max_async_downloads);
760-
s3_set_max_async_uploads(conf->s3_max_async_uploads);
761-
s3_set_connect_timeout_ms(conf->s3_connect_timeout);
762-
s3_set_log_level(conf->s3_log_level);
763-
764747
if (conf->estimate) {
765748
if (conf->filter_exp != NULL || conf->node_list != NULL ||
766749
conf->mod_after > 0 || conf->mod_before > 0 || conf->ttl_zero ||

0 commit comments

Comments
 (0)