#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "spasm.h"

int main(int argc, char **argv) {
    char *buf;
    struct flock lockinfo;
    int fd;

    buf = calloc(strlen(QUARANTINE_DIR) + strlen(argv[1]) + strlen(argv[2]) + 3, 1);
    if(buf == NULL) {
	fprintf(stderr, "Error allocating memory\n");
	exit(1);
    }
    sprintf(buf, "%s/%s/%s", QUARANTINE_DIR, argv[1], argv[2]);
    /* lock file to prevent corruption */
    if((fd = open(buf, O_RDWR)) == -1) {
        fprintf(stderr, "Error opening qfile: %m\n");
        exit(1);
    }
    memset(&lockinfo, 0, sizeof(lockinfo));
    lockinfo.l_type = F_WRLCK;
    if(fcntl(fd, F_SETLKW, &lockinfo) == -1) {
        fprintf(stderr, "Error locking qfile: %m\n");
        close(fd);
        exit(1);
    }
    unlink(buf);
    close(fd);
    free(buf);
    exit(0);
}
