summaryrefslogtreecommitdiff
path: root/c/src/encarchive.c
blob: 0b6348438c0a5f5962fa376160d2b015e2f1f260 (plain)
    1 /**
    2  * GPGSecure manages GPG encrypted archives
    3  * Copyright (C) 2018  Aaron Ball <nullspoon@oper.io>
    4  * 
    5  * This program is free software: you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation, either version 3 of the License, or
    8  * (at your option) any later version.
    9  * 
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  * 
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   17  */
   18 #include "encarchive.h"
   19 
   20 int cd_open(struct archive *a, void *client_data) {
   21   struct clientdata *cd = client_data;
   22   cd->fd = open(cd->name, O_RDONLY);
   23   return (cd->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
   24 }
   25 
   26 la_ssize_t cd_read(struct archive *a, void *client_data, const void **buff) {
   27   struct clientdata *cd = client_data;
   28   *buff = cd->buff;
   29   return (read(cd->fd, cd->buff, 10240));
   30 }
   31 
   32 int cd_close(struct archive *a, void *client_data) {
   33   struct clientdata *cd = client_data;
   34 
   35   if(cd->fd > 0)
   36     close(cd->fd);
   37   return(ARCHIVE_OK);
   38 }
   39 
   40 int enc_archive_enum(char* path) {
   41   struct clientdata *cd;
   42   struct archive *a;
   43   struct archive_entry *entry;
   44 
   45   cd = malloc(sizeof(struct clientdata));
   46   a = archive_read_new();
   47   cd->name = path;
   48 
   49   // Add read support options
   50   int r;
   51   r = archive_read_support_filter_all(a);
   52   if(r > 0)
   53     return r;
   54   r = archive_read_support_format_all(a);
   55   if(r > 0)
   56     return r;
   57 
   58   // Open the archive
   59   archive_read_open(a, cd, cd_open, cd_read, cd_close);
   60   while(archive_read_next_header(a, &entry) == ARCHIVE_OK) {
   61     // Print archive filename
   62     printf("%s\n", archive_entry_pathname(entry));
   63     archive_read_data_skip(a);
   64   }
   65 
   66   archive_read_free(a);
   67   free(cd);
   68   return 0;
   69 }
   70 
   71 
   72 int enc_archive_extract(char* srcarchive, char* dest) {
   73   struct clientdata *cd;
   74   struct archive *a;
   75   struct archive_entry *entry;
   76 
   77   cd = malloc(sizeof(struct clientdata));
   78   a = archive_read_new();
   79   cd->name = srcarchive;
   80 
   81   // Add read support options
   82   int r;
   83   r = archive_read_support_filter_all(a);
   84   if(r > 0)
   85     return r;
   86   r = archive_read_support_format_all(a);
   87   if(r > 0)
   88     return r;
   89 
   90   // Open the archive
   91   if(archive_read_open(a, cd, cd_open, cd_read, cd_close) < 0) {
   92     printf("Error: Could not open archive '%s'\n", srcarchive);
   93     return 1;
   94   }
   95   while(archive_read_next_header(a, &entry) == ARCHIVE_OK) {
   96     archive_read_extract(a, entry, ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS);
   97     archive_read_data_skip(a);
   98     // Print archive filename
   99     printf("extracting %s\n", archive_entry_pathname(entry));
  100   }
  101 
  102   archive_read_free(a);
  103   free(cd);
  104   return 0;
  105 }

Generated by cgit