Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.2 kB
5
Indexable
Never
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>



struct __blk {
   struct {
      char magic[10];
      char version[8];
   };

   char name[100];
   char size[12];
   char type[12];
   char mode[8];
   char uid[8];
   char gid[8];
   char ctime[16];
   char atime[16];
   char mtime[16];
   char linkcnt[12];
   char chksum[12]; /* Unused for now */
   char pad[10];
} __attribute__((packed));


int main(int argc, char *argv[]) {
   int index = 0;
   int offset = 0;
   int fcount = argc;

   FILE *_fd;
   struct stat st;
   char *_raw = NULL;

   DIR *d;
   struct dirent *d_entry;
   size_t dir_len = 0;

   struct __blk *blk = malloc(sizeof(struct __blk));

   memset(blk, 0, sizeof(struct __blk));
   FILE *fd = fopen("new.pkg", "wb");

   /* Don't write first 512 byte block header */
   //offset += 512 + index;
   //if (offset % 512) {
   //   offset += 512 - (offset % 512);
   //}
   //write(fd, blk, sizeof(struct __blk));


   for (int i = 1; i < fcount; i++) {
      memset(blk, 0, sizeof(struct __blk));
      stat(argv[i], &st);
      index += st.st_size;

      sprintf(blk->magic, "%s", "\x50\x41\x4b\x21");

      sprintf(blk->name, "%s\n", argv[i]);
      sprintf(blk->size, "%ld\n", st.st_size);
      sprintf(blk->mode, "%o\n", st.st_mode);
      sprintf(blk->uid, "%u\n", st.st_uid);
      sprintf(blk->gid, "%u\n", st.st_gid);

      sprintf(blk->ctime, "%ld\n", st.st_ctime);
      sprintf(blk->atime, "%ld\n", st.st_atime);
      sprintf(blk->mtime, "%ld\n", st.st_mtime);

      sprintf(blk->linkcnt, "%d\n", st.st_nlink);


      fseek(fd, offset, SEEK_SET);
      _fd = fopen(argv[i], "rb");
      _raw = malloc(st.st_size);

      fread(_raw, 1, st.st_size, _fd);
      fclose(_fd);

      fwrite(blk, 14, sizeof(struct __blk), fd);

      /* Ensure data doesn't override header */
      fseek(fd, offset + sizeof(struct __blk), SEEK_SET);
      fwrite(_raw, 1, st.st_size, fd);
      free(_raw);


      offset += 512 + index;
      if (offset % 512) {
         offset += 512 - (offset % 512);
      }
   }

   fclose(fd);
   free(blk);
   return 0;
}