Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions
mar_create.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mar.h"
#include "mar_private.h"
#include <netinet/in.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  MarItemStack

Functions

static int mar_push (struct MarItemStack *stack, PRUint32 length, PRUint32 flags, const char *name)
 Push a new item onto the stack of items.
static int mar_concat_file (FILE *fp, const char *path)
int mar_create (const char *dest, int num_files, char **files)
 Create a MAR file from a set of files.

Class Documentation

struct MarItemStack

Definition at line 55 of file mar_create.c.

Class Members
void * head
PRUint32 last_offset
PRUint32 size_allocated
PRUint32 size_used

Function Documentation

static int mar_concat_file ( FILE fp,
const char *  path 
) [static]

Definition at line 107 of file mar_create.c.

                                                       {
  FILE *in;
  char buf[BLOCKSIZE];
  size_t len;
  int rv = 0;

  in = fopen(path, "rb");
  if (!in)
    return -1;

  while ((len = fread(buf, 1, BLOCKSIZE, in)) > 0) {
    if (fwrite(buf, len, 1, fp) != 1) {
      rv = -1;
      break;
    }
  }

  fclose(in);
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int mar_create ( const char *  dest,
int  numfiles,
char **  files 
)

Create a MAR file from a set of files.

Parameters:
destThe path to the file to create. This path must be compatible with fopen.
numfilesThe number of files to store in the archive.
filesThe list of null-terminated file paths. Each file path must be compatible with fopen.
Returns:
A non-zero value if an error occurs.

Definition at line 128 of file mar_create.c.

                                                              {
  struct MarItemStack stack;
  PRUint32 offset_to_index = 0, size_of_index;
  struct stat st;
  FILE *fp;
  int i, rv = -1;

  memset(&stack, 0, sizeof(stack));

  fp = fopen(dest, "wb");
  if (!fp) {
    fprintf(stderr, "ERROR: could not create target file: %s\n", dest);
    return -1;
  }

  if (fwrite(MAR_ID, MAR_ID_SIZE, 1, fp) != 1)
    goto failure;
  if (fwrite(&offset_to_index, sizeof(PRUint32), 1, fp) != 1)
    goto failure;

  stack.last_offset = MAR_ID_SIZE + sizeof(PRUint32);

  for (i = 0; i < num_files; ++i) {
    if (stat(files[i], &st)) {
      fprintf(stderr, "ERROR: file not found: %s\n", files[i]);
      goto failure;
    }

    if (mar_push(&stack, st.st_size, st.st_mode & 0777, files[i]))
      goto failure;

    /* concatenate input file to archive */
    if (mar_concat_file(fp, files[i]))
      goto failure;
  }

  /* write out the index (prefixed with length of index) */
  size_of_index = htonl(stack.size_used);
  if (fwrite(&size_of_index, sizeof(size_of_index), 1, fp) != 1)
    goto failure;
  if (fwrite(stack.head, stack.size_used, 1, fp) != 1)
    goto failure;

  /* write out offset to index file in network byte order */
  offset_to_index = htonl(stack.last_offset);
  if (fseek(fp, MAR_ID_SIZE, SEEK_SET))
    goto failure;
  if (fwrite(&offset_to_index, sizeof(offset_to_index), 1, fp) != 1)
    goto failure;

  rv = 0;
failure: 
  if (stack.head)
    free(stack.head);
  fclose(fp);
  if (rv)
    remove(dest);
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int mar_push ( struct MarItemStack stack,
PRUint32  length,
PRUint32  flags,
const char *  name 
) [static]

Push a new item onto the stack of items.

The stack is a single block of memory.

Definition at line 66 of file mar_create.c.

                                      {
  int namelen;
  PRUint32 n_offset, n_length, n_flags;
  PRUint32 size;
  char *data;
  
  namelen = strlen(name);
  size = MAR_ITEM_SIZE(namelen);

  if (stack->size_allocated - stack->size_used < size) {
    /* increase size of stack */
    size_t size_needed = ROUND_UP(stack->size_used + size, BLOCKSIZE);
    stack->head = realloc(stack->head, size_needed);
    if (!stack->head)
      return -1;
    stack->size_allocated = size_needed;
  }

  data = (((char *) stack->head) + stack->size_used);

  n_offset = htonl(stack->last_offset);
  n_length = htonl(length);
  n_flags = htonl(flags);

  memcpy(data, &n_offset, sizeof(n_offset));
  data += sizeof(n_offset);

  memcpy(data, &n_length, sizeof(n_length));
  data += sizeof(n_length);

  memcpy(data, &n_flags, sizeof(n_flags));
  data += sizeof(n_flags);

  memcpy(data, name, namelen + 1);
  
  stack->size_used += size;
  stack->last_offset += length;
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: