Working on adding in compression, decompress works!
This commit is contained in:
parent
6dd32ca7a1
commit
8322e91fae
7
Makefile
7
Makefile
@ -10,10 +10,13 @@ PREFIX ?= /usr
|
|||||||
CC ?= gcc
|
CC ?= gcc
|
||||||
CFLAGS += -O2 -pedantic -g -Wall -Wextra --param=ssp-buffer-size=2 -fstack-protector-all
|
CFLAGS += -O2 -pedantic -g -Wall -Wextra --param=ssp-buffer-size=2 -fstack-protector-all
|
||||||
CPPFLAGS += -DVERSION=$(VERSION) -D_FORTIFY_SOURCE=2
|
CPPFLAGS += -DVERSION=$(VERSION) -D_FORTIFY_SOURCE=2
|
||||||
LDFLAGS += -Wl,-O1
|
LDFLAGS += -Wl,-O1 -Lsrc/libutil
|
||||||
BIN ?= slidescript
|
BIN ?= slidescript
|
||||||
|
|
||||||
OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o src/pipe.o src/network.o src/math.o src/inset.o src/search.o
|
SRCS=$(wildcard src/*.c)
|
||||||
|
|
||||||
|
OBJECTS=$(SRCS:%.c=%.o)
|
||||||
|
|
||||||
|
|
||||||
all: main
|
all: main
|
||||||
|
|
||||||
|
@ -56,6 +56,7 @@
|
|||||||
// For files
|
// For files
|
||||||
#define MAX_FILENAME_LEN 1024
|
#define MAX_FILENAME_LEN 1024
|
||||||
#define MAX_FILENAME_BUFSIZE (MAX_FILENAME_LEN + 1)
|
#define MAX_FILENAME_BUFSIZE (MAX_FILENAME_LEN + 1)
|
||||||
|
#define MAX_FILES 1024
|
||||||
// END
|
// END
|
||||||
#define TOKEN '%'
|
#define TOKEN '%'
|
||||||
#define TOKEN_STR "%"
|
#define TOKEN_STR "%"
|
||||||
|
136
src/functions.c
136
src/functions.c
@ -15,6 +15,7 @@
|
|||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "inset.h"
|
#include "inset.h"
|
||||||
|
#include "tar.h"
|
||||||
|
|
||||||
char *process_line(char *line)
|
char *process_line(char *line)
|
||||||
{
|
{
|
||||||
@ -44,9 +45,142 @@ char *process_line(char *line)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strncmp("exit", tok_srch, 4) == 0)
|
else if(strncmp("exit", tok_srch, 4) == 0)
|
||||||
syn_error("ss:exit called");
|
syn_error("ss:exit called");
|
||||||
|
|
||||||
|
/* COMPRESSION AND DECOMPRESSION */
|
||||||
|
else if(strncmp("decompress",tok_srch,10) == 0)
|
||||||
|
{
|
||||||
|
char *filename;
|
||||||
|
struct tar_t *archive = NULL;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
tok_srch = strtok(NULL, "\"");
|
||||||
|
if(tok_srch == NULL)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:decompress syntax error, missing quote?");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strtok(NULL, "\"")==NULL)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:decompress syntax error, missing end quote?");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
filename = parse_vars(tok_srch);
|
||||||
|
|
||||||
|
// open existing file
|
||||||
|
if ((fd = open(filename, O_RDWR)) < 0) {
|
||||||
|
syn_warn("ss:warn:decompress, failed to open archive");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read in data
|
||||||
|
if (tar_read(fd, &archive, '1') < 0) {
|
||||||
|
tar_free(archive);
|
||||||
|
close(fd);
|
||||||
|
syn_warn("ss:warn:decompress, failed to read archive");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// perform operation
|
||||||
|
if(tar_extract(fd, archive, 0, NULL, '1') < 0) { // extract entries
|
||||||
|
syn_warn("ss:warn:decompress, error occured");
|
||||||
|
}
|
||||||
|
|
||||||
|
tar_free(archive);
|
||||||
|
close(fd); // don't bother checking for fd < 0
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Compression function of tar */
|
||||||
|
else if(strncmp("compress",tok_srch,8) == 0)
|
||||||
|
{
|
||||||
|
char filename[MAX_FILENAME_LEN+1];
|
||||||
|
char *files[MAX_FILENAME_LEN+1];
|
||||||
|
struct tar_t *archive = NULL;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
bzero(filename, MAX_FILENAME_LEN);
|
||||||
|
|
||||||
|
*filename = '\0';
|
||||||
|
|
||||||
|
tok_srch = strtok(NULL, "\"");
|
||||||
|
if(tok_srch == NULL)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:compress syntax error, missing quote?");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:compress syntax error, missing archive name...");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
tok_srch = strtok(NULL, "\"");
|
||||||
|
if(tok_srch == NULL)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:compress syntax error, missing quote?");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strlen(parse_vars(tok_srch)) < MAX_FILENAME_LEN)
|
||||||
|
{
|
||||||
|
strcat(files, parse_vars(tok_srch));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:compress, filename too long!");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)) == -1){
|
||||||
|
syn_warn("ss:warn:compress, failed to open new archive");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
tok_srch = strtok(NULL, "\"");
|
||||||
|
if(tok_srch == NULL)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:compress syntax error, missing quote?");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:compress, missing file path (what are we gonna compress?)");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strtok(NULL, "\"")==NULL)
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:decompress syntax error, missing end quote?");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strlen(parse_vars(tok_srch)) < MAX_FILENAME_LEN)
|
||||||
|
{
|
||||||
|
strcat(files[0], parse_vars(tok_srch));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
syn_warn("ss:warn:comrpress, filename too long!");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tar_write(fd, &archive, 1, &files, '1') < 0) {
|
||||||
|
syn_warn("ss:warn:compress, failed to create archive");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
tar_free(archive);
|
||||||
|
close(fd); // don't bother checking for fd < 0
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* mkdir function, and mkfile functions */
|
/* mkdir function, and mkfile functions */
|
||||||
else if(strncmp("mkdir",tok_srch,5) == 0)
|
else if(strncmp("mkdir",tok_srch,5) == 0)
|
||||||
|
177
src/tar.h
Normal file
177
src/tar.h
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
/*
|
||||||
|
tar.h
|
||||||
|
tar data structure and accompanying functions
|
||||||
|
|
||||||
|
Copyright (c) 2015 Jason Lee
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __TAR__
|
||||||
|
#define __TAR__
|
||||||
|
|
||||||
|
#ifndef _DEFAULT_SOURCE
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <grp.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#if !defined(__APPLE__)
|
||||||
|
#include <sys/sysmacros.h>
|
||||||
|
#endif
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define DEFAULT_DIR_MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH // 0755
|
||||||
|
|
||||||
|
#define BLOCKSIZE 512
|
||||||
|
#define BLOCKING_FACTOR 20
|
||||||
|
#define RECORDSIZE 10240
|
||||||
|
|
||||||
|
// file type values (1 octet)
|
||||||
|
#define REGULAR 0
|
||||||
|
#define NORMAL '0'
|
||||||
|
#define HARDLINK '1'
|
||||||
|
#define SYMLINK '2'
|
||||||
|
#define CHAR '3'
|
||||||
|
#define BLOCK '4'
|
||||||
|
#define DIRECTORY '5'
|
||||||
|
#define FIFO '6'
|
||||||
|
#define CONTIGUOUS '7'
|
||||||
|
|
||||||
|
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||||
|
|
||||||
|
// tar entry metadata structure (singly-linked list)
|
||||||
|
struct tar_t {
|
||||||
|
char original_name[100]; // original filenme; only availible when writing into a tar
|
||||||
|
unsigned int begin; // location of data in file (including metadata)
|
||||||
|
union {
|
||||||
|
union {
|
||||||
|
// Pre-POSIX.1-1988 format
|
||||||
|
struct {
|
||||||
|
char name[100]; // file name
|
||||||
|
char mode[8]; // permissions
|
||||||
|
char uid[8]; // user id (octal)
|
||||||
|
char gid[8]; // group id (octal)
|
||||||
|
char size[12]; // size (octal)
|
||||||
|
char mtime[12]; // modification time (octal)
|
||||||
|
char check[8]; // sum of unsigned characters in block, with spaces in the check field while calculation is done (octal)
|
||||||
|
char link; // link indicator
|
||||||
|
char link_name[100]; // name of linked file
|
||||||
|
};
|
||||||
|
|
||||||
|
// UStar format (POSIX IEEE P1003.1)
|
||||||
|
struct {
|
||||||
|
char old[156]; // first 156 octets of Pre-POSIX.1-1988 format
|
||||||
|
char type; // file type
|
||||||
|
char also_link_name[100]; // name of linked file
|
||||||
|
char ustar[8]; // ustar\000
|
||||||
|
char owner[32]; // user name (string)
|
||||||
|
char group[32]; // group name (string)
|
||||||
|
char major[8]; // device major number
|
||||||
|
char minor[8]; // device minor number
|
||||||
|
char prefix[155];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
char block[512]; // raw memory (500 octets of actual data, padded to 1 block)
|
||||||
|
};
|
||||||
|
|
||||||
|
struct tar_t * next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// core functions //////////////////////////////////////////////////////////////
|
||||||
|
// read a tar file
|
||||||
|
// archive should be address to null pointer
|
||||||
|
int tar_read(const int fd, struct tar_t ** archive, const char verbosity);
|
||||||
|
|
||||||
|
// write to a tar file
|
||||||
|
// if archive contains data, the new data will be appended to the back of the file (terminating blocks will be rewritten)
|
||||||
|
int tar_write(const int fd, struct tar_t ** archive, const size_t filecount, const char * files[], const char verbosity);
|
||||||
|
|
||||||
|
// recursive freeing of entries
|
||||||
|
void tar_free(struct tar_t * archive);
|
||||||
|
// /////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// utilities ///////////////////////////////////////////////////////////////////
|
||||||
|
// print contents of archive
|
||||||
|
// verbosity should be greater than 0
|
||||||
|
int tar_ls(FILE * f, struct tar_t * archive, const size_t filecount, const char * files[], const char verbosity);
|
||||||
|
|
||||||
|
// extracts files from an archive
|
||||||
|
int tar_extract(const int fd, struct tar_t * archive, const size_t filecount, const char * files[], const char verbosity);
|
||||||
|
|
||||||
|
// update files in tar with provided list
|
||||||
|
int tar_update(const int fd, struct tar_t ** archive, const size_t filecount, const char * files[], const char verbosity);
|
||||||
|
|
||||||
|
// remove entries from tar
|
||||||
|
int tar_remove(const int fd, struct tar_t ** archive, const size_t filecount, const char * files[], const char verbosity);
|
||||||
|
|
||||||
|
// show files that are missing from the current directory
|
||||||
|
int tar_diff(FILE * f, struct tar_t * archive, const char verbosity);
|
||||||
|
// /////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// internal functions; generally don't call from outside ///////////////////////
|
||||||
|
// print raw data with definitions (meant for debugging)
|
||||||
|
int print_entry_metadata(FILE * f, struct tar_t * entry);
|
||||||
|
|
||||||
|
// print metadata of entire tar file
|
||||||
|
int print_tar_metadata(FILE * f, struct tar_t * archive);
|
||||||
|
|
||||||
|
// check if file with original name/modified name exists
|
||||||
|
struct tar_t * exists(struct tar_t * archive, const char * filename, const char ori);
|
||||||
|
|
||||||
|
// read file and construct metadata
|
||||||
|
int format_tar_data(struct tar_t * entry, const char * filename, const char verbosity);
|
||||||
|
|
||||||
|
// calculate checksum (6 ASCII octet digits + NULL + space)
|
||||||
|
unsigned int calculate_checksum(struct tar_t * entry);
|
||||||
|
|
||||||
|
// print single entry
|
||||||
|
// verbosity should be greater than 0
|
||||||
|
int ls_entry(FILE * f, struct tar_t * archive, const size_t filecount, const char * files[], const char verbosity);
|
||||||
|
|
||||||
|
// extracts a single entry
|
||||||
|
// expects file descriptor offset to already be set to correct location
|
||||||
|
int extract_entry(const int fd, struct tar_t * entry, const char verbosity);
|
||||||
|
|
||||||
|
// write entries to a tar file
|
||||||
|
int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, const size_t filecount, const char * files[], int * offset, const char verbosity);
|
||||||
|
|
||||||
|
// add ending data
|
||||||
|
int write_end_data(const int fd, int size, const char verbosity);
|
||||||
|
|
||||||
|
// check if entry is a match for any of the given file names
|
||||||
|
// returns index + 1 if match is found
|
||||||
|
int check_match(struct tar_t * entry, const size_t filecount, const char * files[]);
|
||||||
|
// /////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#endif
|
25
src/util.h
25
src/util.h
@ -4,6 +4,30 @@
|
|||||||
|
|
||||||
View README file supplied with this software for more details
|
View README file supplied with this software for more details
|
||||||
*/
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <regex.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include "config.h"
|
||||||
|
#include "sbyteswap.h"
|
||||||
|
#include <sys/types.h>
|
||||||
|
// Networking
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
// For file handling
|
||||||
|
#include <assert.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
// For string searching
|
||||||
|
#include <regex.h>
|
||||||
|
|
||||||
void syn_error(char *message);
|
void syn_error(char *message);
|
||||||
void syn_warn(char *message);
|
void syn_warn(char *message);
|
||||||
@ -14,3 +38,4 @@ int mkpath(char* file_path, mode_t mode);
|
|||||||
char *ss_concat(char *str1, char *str2);
|
char *ss_concat(char *str1, char *str2);
|
||||||
void parse_args(int argc, char** argv);
|
void parse_args(int argc, char** argv);
|
||||||
char *ss_time();
|
char *ss_time();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user