Back to index

lightning-sunbird  0.9+nobinonly
Classes | Typedefs | Functions
mar.h File Reference
#include "prtypes.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  MarItem_
 The MAR item data structure. More...

Typedefs

typedef struct MarItem_ MarItem
 The MAR item data structure.
typedef struct MarFile_
typedef int(* MarItemCallback )(MarFile *mar, const MarItem *item, void *data)
 Signature of callback function passed to mar_enum_items.

Functions

MarFile * mar_open (const char *path)
 Open a MAR file for reading.
void mar_close (MarFile *mar)
 Close a MAR file that was opened using mar_open.
const MarItemmar_find_item (MarFile *mar, const char *item)
 Find an item in the MAR file by name.
int mar_enum_items (MarFile *mar, MarItemCallback callback, void *data)
 Enumerate all MAR items via callback function.
int mar_read (MarFile *mar, const MarItem *item, int offset, char *buf, int bufsize)
 Read from MAR item at given offset up to bufsize bytes.
int mar_create (const char *dest, int numfiles, char **files)
 Create a MAR file from a set of files.
int mar_extract (const char *path)
 Extract a MAR file to the current working directory.

Class Documentation

struct MarItem_

The MAR item data structure.

Definition at line 52 of file mar.h.

Collaboration diagram for MarItem_:
Class Members
PRUint32 flags
PRUint32 length
char name
struct MarItem_ * next
PRUint32 offset

Typedef Documentation

typedef struct MarFile_

Definition at line 60 of file mar.h.

typedef struct MarItem_ MarItem

The MAR item data structure.

typedef int(* MarItemCallback)(MarFile *mar, const MarItem *item, void *data)

Signature of callback function passed to mar_enum_items.

Parameters:
marThe MAR file being visited.
itemThe MAR item being visited.
dataThe data parameter passed by the caller of mar_enum_items.
Returns:
A non-zero value to stop enumerating.

Definition at line 69 of file mar.h.


Function Documentation

void mar_close ( MarFile *  mar)

Close a MAR file that was opened using mar_open.

Parameters:
marThe MarFile object to close.

Definition at line 206 of file mar_read.c.

                             {
  MarItem *item;
  int i;

  fclose(mar->fp);

  for (i = 0; i < TABLESIZE; ++i) {
    item = mar->item_table[i];
    while (item) {
      MarItem *temp = item;
      item = item->next;
      free(temp);
    }
  }

  free(mar);
}

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:

int mar_enum_items ( MarFile *  mar,
MarItemCallback  callback,
void data 
)

Enumerate all MAR items via callback function.

Parameters:
marThe MAR file to enumerate.
callbackThe function to call for each MAR item.
dataA caller specified value that is passed along to the callback function.
Returns:
Zero if the enumeration ran to completion. Otherwise, any non-zero return value from the callback is returned.

Definition at line 237 of file mar_read.c.

                                                                          {
  MarItem *item;
  int i;

  for (i = 0; i < TABLESIZE; ++i) {
    item = mar->item_table[i];
    while (item) {
      int rv = callback(mar, item, closure);
      if (rv)
        return rv;
      item = item->next;
    }
  }

  return 0;
}

Here is the caller graph for this function:

int mar_extract ( const char *  path)

Extract a MAR file to the current working directory.

Parameters:
pathThe path to the MAR file to extract. This path must be compatible with fopen.
Returns:
A non-zero value if an error occurs.

Definition at line 101 of file mar_extract.c.

                                  {
  MarFile *mar;
  int rv;

  mar = mar_open(path);
  if (!mar)
    return -1;

  rv = mar_enum_items(mar, mar_test_callback, NULL);

  mar_close(mar);
  return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const MarItem* mar_find_item ( MarFile *  mar,
const char *  item 
)

Find an item in the MAR file by name.

Parameters:
marThe MarFile object to query.
itemThe name of the item to query.
Returns:
A const reference to a MAR item or NULL if not found.

Definition at line 224 of file mar_read.c.

                                                             {
  PRUint32 hash;
  const MarItem *item;

  hash = mar_hash_name(name);

  item = mar->item_table[hash];
  while (item && strcmp(item->name, name) != 0)
    item = item->next;

  return item;
}

Here is the call graph for this function:

Here is the caller graph for this function:

MarFile* mar_open ( const char *  path)

Open a MAR file for reading.

Parameters:
pathSpecifies the path to the MAR file to open. This path must be compatible with fopen.
Returns:
NULL if an error occurs.

Definition at line 182 of file mar_read.c.

                                    {
  MarFile *mar;
  FILE *fp;

  fp = fopen(path, "rb");
  if (!fp)
    return NULL;

  mar = (MarFile *) malloc(sizeof(*mar));
  if (!mar) {
    fclose(fp);
    return NULL;
  }

  mar->fp = fp;
  memset(mar->item_table, 0, sizeof(mar->item_table));
  if (mar_read_index(mar)) {
    mar_close(mar);
    return NULL;
  }

  return mar;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int mar_read ( MarFile *  mar,
const MarItem item,
int  offset,
char *  buf,
int  bufsize 
)

Read from MAR item at given offset up to bufsize bytes.

Parameters:
marThe MAR file to read.
itemThe MAR item to read.
offsetThe byte offset relative to the start of the item.
bufA pointer to a buffer to copy the data into.
bufsizeThe length of the buffer to copy the data into.
Returns:
The number of bytes written or a negative value if an error occurs.

Definition at line 254 of file mar_read.c.

                          {
  int nr;

  if (offset == (int) item->length)
    return 0;
  if (offset > (int) item->length)
    return -1;

  nr = item->length - offset;
  if (nr > bufsize)
    nr = bufsize;

  if (fseek(mar->fp, item->offset + offset, SEEK_SET))
    return -1;

  return fread(buf, 1, nr, mar->fp);
}

Here is the call graph for this function:

Here is the caller graph for this function: