From dc599c79ea2e8594214005a3702f8feadeb27649 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ji=C5=99=C3=AD=20=C5=A0kr=C3=A1bal?= Date: Wed, 21 Sep 2005 13:52:42 +0000 Subject: [PATCH] - glite_jpimporter_upload_files() creates tar archives now --- org.glite.jp.client/src/jpcl_ctx.c | 3 +- org.glite.jp.client/src/jpimp_lib.c | 110 +++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 28 deletions(-) diff --git a/org.glite.jp.client/src/jpcl_ctx.c b/org.glite.jp.client/src/jpcl_ctx.c index 48e4c06..d6725f0 100644 --- a/org.glite.jp.client/src/jpcl_ctx.c +++ b/org.glite.jp.client/src/jpcl_ctx.c @@ -4,8 +4,9 @@ #include #include -#include "jpimporter.h" +#include "jp_client.h" #include "jpcl_ctx_int.h" +#include "jpimporter.h" int glite_jpcl_InitContext(glite_jpcl_context_t *ctx) diff --git a/org.glite.jp.client/src/jpimp_lib.c b/org.glite.jp.client/src/jpimp_lib.c index 7b0f2bb..99a6299 100644 --- a/org.glite.jp.client/src/jpimp_lib.c +++ b/org.glite.jp.client/src/jpimp_lib.c @@ -1,5 +1,16 @@ +#include +#include #include #include +#include +#include +#include + + +#define COMPILE_WITH_LIBTAR +#ifdef COMPILE_WITH_LIBTAR +# include +#endif #include "glite/lb/lb_maildir.h" @@ -7,52 +18,97 @@ #include "jpimporter.h" #include "jpcl_ctx_int.h" +#define TEMP_FILE_PREFIX "/tmp/jpimporter" + int glite_jpimporter_upload_files( glite_jpcl_context_t ctx, const char *jobid, const char **files, const char *proxy) { - char *msg, - *file; +#ifdef COMPILE_WITH_LIBTAR + TAR *t = NULL; +#endif + char *msg = NULL, + *errs = NULL; + char archive[PATH_MAX]; + int fd = -1, + rv = 0, + i; - if ( !files || !files[0] ) { - glite_jpcl_SetError(ctx, EINVAL, "No files given"); - return -1; - } - if ( !jobid ) { - glite_jpcl_SetError(ctx, EINVAL, "No jobid given"); - return -1; - } - /* TODO: get the user proxy if it is not specified and - * find the file of its location. - */ + + assert((files != NULL) && (files[0] != NULL)); + assert(jobid != NULL); + /* TODO: get the user proxy if it is not specified and find its location */ + assert(proxy != NULL); if ( edg_wll_MaildirInit(ctx->lbmd_dir) ) { - char *aux; - asprintf(aux, "edg_wll_MaildirInit(): %s", lbm_errdesc); - glite_jpcl_SetError(ctx, errno, aux); - free(aux); + asprintf(errs, "edg_wll_MaildirInit(): %s", lbm_errdesc); + glite_jpcl_SetError(ctx, errno, errs); + free(errs); return -1; } - /* TODO: Pack all the files into one tar file */ - file = files[0]; + i = 0; + do { + if ( ++i > 10 ) { + glite_jpcl_SetError(ctx, ECANCELED, "Can't create temporary tar file"); + return -1; + } + snprintf(archive, PATH_MAX, "%s_%ld_%ld.tar", + TEMP_FILE_PREFIX, getpid(), time(NULL)); + if ( (fd = open(archive, O_CREAT|O_EXCL|O_WRONLY, 00600)) < 0 ) { + if ( errno == EEXIST ) { sleep(2); continue; } + asprintf(errs, "Can't create tar file %s", archive); + glite_jpcl_SetError(ctx, ECANCELED, errs); + free(errs); + return -1; + } + } while ( fd < 0 ); + +#ifdef COMPILE_WITH_LIBTAR + if ( tar_fdopen(&t, fd, archive, NULL, O_WRONLY, 0, TAR_GNU) < 0 ) { + asprintf(errs, "Can't create tar archive %s", archive); + glite_jpcl_SetError(ctx, errno, errs); + rv = -1; + goto cleanup; + } + + for ( i = 0; files[i]; i++ ) { + char *s = files[i]; + if ( tar_append_file(t, s, (s[0]=='/')? s+1: s) < 0 ) { + glite_jpcl_SetError(ctx, errno, "Can't append file into archive"); + rv = -1; + goto cleanup; + } + } +#endif if ( ctx->jpps ) asprintf(msg, "jobid\t%s\nfile\t%s\nproxy\t%sjpps\t%s", - jobid, file, proxy, ctx->jpps); + jobid, archive, proxy, ctx->jpps); else asprintf(msg, "jobid\t%s\nfile\t%s\nproxy\t%s", - jobid, file, proxy); + jobid, archive, proxy); if ( edg_wll_MaildirStoreMsg(ctx->lbmd_dir, "localhost", msg) ) { - char *aux; - asprintf(aux, "edg_wll_MaildirStoreMsg(): %s", lbm_errdesc); - glite_jpcl_SetError(ctx, errno, aux); - free(aux); - return -1; + asprintf(errs, "edg_wll_MaildirStoreMsg(): %s", lbm_errdesc); + glite_jpcl_SetError(ctx, errno, errs); + rv = -1; + goto cleanup; } - return 0; + +cleanup: +#ifdef COMPILE_WITH_LIBTAR + if ( t ) tar_close(t); + else close(fd); +#else + close(fd); +#endif + unlink(archive); + free(errs); + free(msg); + + return rv; } -- 1.8.2.3