summaryrefslogtreecommitdiff
path: root/tbdiff-create/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'tbdiff-create/main.c')
-rw-r--r--tbdiff-create/main.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/tbdiff-create/main.c b/tbdiff-create/main.c
new file mode 100644
index 0000000..d20feba
--- /dev/null
+++ b/tbdiff-create/main.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2011-2012 Codethink Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License Version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+
+#include <unistd.h>
+
+#include <tbdiff/tbdiff.h>
+
+
+int
+main(int argc,
+ char **argv)
+{
+ if(argc < 4) {
+ fprintf(stderr, "Usage: %s OUTPUT SOURCE_DIR TARGET_DIR.\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ size_t cwd_size = pathconf(".", _PC_PATH_MAX);
+ char cwd_buff[cwd_size];
+ if(getcwd(cwd_buff, cwd_size) == NULL)
+ return EXIT_FAILURE;
+
+ tbd_stat_t *tstat[2];
+
+ tstat[0] = tbd_stat(argv[2]);
+ if(tstat[0] == NULL) {
+ fprintf(stderr, "Error: Unable to stat '%s'.\n", argv[2]);
+ return EXIT_FAILURE;
+ }
+
+ if(chdir(cwd_buff) != 0) {
+ fprintf(stderr, "Error: Unable to return to '%s'.\n", cwd_buff);
+ return EXIT_FAILURE;
+ }
+
+ tstat[1] = tbd_stat(argv[3]);
+ if(tstat[1] == NULL) {
+ fprintf(stderr, "Error: Unable to stat '%s'.\n", argv[3]);
+ return EXIT_FAILURE;
+ }
+
+ if (chdir(cwd_buff) != 0) {
+ fprintf(stderr, "Error: Unable to return to '%s'.\n", cwd_buff);
+ return EXIT_FAILURE;
+ }
+
+ FILE *fp = fopen(argv[1], "wb");
+ if(fp == NULL) {
+ fprintf(stderr, "ERROR: Unable to open patch for writing.\n");
+ return EXIT_FAILURE;
+ }
+
+ int err;
+ if((err = tbd_create(fp, tstat[0], tstat[1])) != 0) {
+ fclose(fp);
+ tbd_stat_free(tstat[0]);
+ tbd_stat_free(tstat[1]);
+
+ remove(argv[1]);
+ fprintf(stderr, "Error: Failed to create tbdiff image (err=%d).\n", err);
+ switch (err) {
+ case TBD_ERROR_UNABLE_TO_CREATE_SOCKET_FILE:
+ fprintf(stderr, "%s directory contains Unix Sockets. "
+ "%s cannot create sockets, please ensure no "
+ "programs are using sockets in directory.\n",
+ argv[3], argv[0]);
+ break;
+ default:
+ break;
+ }
+ return EXIT_FAILURE;
+ }
+
+ fclose(fp);
+ tbd_stat_free(tstat[0]);
+ tbd_stat_free(tstat[1]);
+
+ return EXIT_SUCCESS;
+}