Back to index

lightning-sunbird  0.9+nobinonly
Functions
mar_extract.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "mar.h"
#include "mar_private.h"

Go to the source code of this file.

Functions

static int mar_ensure_parent_dir (const char *path)
static int mar_test_callback (MarFile *mar, const MarItem *item, void *unused)
int mar_extract (const char *path)
 Extract a MAR file to the current working directory.

Function Documentation

static int mar_ensure_parent_dir ( const char *  path) [static]

Definition at line 54 of file mar_extract.c.

{
  char *slash = strrchr(path, '/');
  if (slash)
  {
    *slash = '\0';
    mar_ensure_parent_dir(path);
#ifdef XP_WIN
    _mkdir(path);
#else
    mkdir(path, 0755);
#endif
    *slash = '/';
  }
  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:

static int mar_test_callback ( MarFile *  mar,
const MarItem item,
void unused 
) [static]

Definition at line 71 of file mar_extract.c.

                                                                              {
  FILE *fp;
  char buf[BLOCKSIZE];
  int fd, len, offset = 0;

  if (mar_ensure_parent_dir(item->name))
    return -1;

#ifdef XP_WIN
  fd = _open(item->name, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, item->flags);
#else
  fd = creat(item->name, item->flags);
#endif
  if (fd == -1)
    return -1;

  fp = fdopen(fd, "wb");
  if (!fp)
    return -1;

  while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) {
    if (fwrite(buf, len, 1, fp) != 1)
      break;
    offset += len;
  }

  fclose(fp);
  return len == 0 ? 0 : -1;
}

Here is the call graph for this function:

Here is the caller graph for this function: