Back to index

lightning-sunbird  0.9+nobinonly
mar_extract.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Archive code.
00017  *
00018  * The Initial Developer of the Original Code is Google Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Darin Fisher <darin@meer.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <sys/types.h>
00040 #include <sys/stat.h>
00041 #include <fcntl.h>
00042 #include <string.h>
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include "mar.h"
00046 #include "mar_private.h"
00047 
00048 #ifdef XP_WIN
00049 #include <io.h>
00050 #include <direct.h>
00051 #endif
00052 
00053 /* Ensure that the directory containing this file exists */
00054 static int mar_ensure_parent_dir(const char *path)
00055 {
00056   char *slash = strrchr(path, '/');
00057   if (slash)
00058   {
00059     *slash = '\0';
00060     mar_ensure_parent_dir(path);
00061 #ifdef XP_WIN
00062     _mkdir(path);
00063 #else
00064     mkdir(path, 0755);
00065 #endif
00066     *slash = '/';
00067   }
00068   return 0;
00069 }
00070 
00071 static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused) {
00072   FILE *fp;
00073   char buf[BLOCKSIZE];
00074   int fd, len, offset = 0;
00075 
00076   if (mar_ensure_parent_dir(item->name))
00077     return -1;
00078 
00079 #ifdef XP_WIN
00080   fd = _open(item->name, _O_BINARY|_O_CREAT|_O_TRUNC|_O_WRONLY, item->flags);
00081 #else
00082   fd = creat(item->name, item->flags);
00083 #endif
00084   if (fd == -1)
00085     return -1;
00086 
00087   fp = fdopen(fd, "wb");
00088   if (!fp)
00089     return -1;
00090 
00091   while ((len = mar_read(mar, item, offset, buf, sizeof(buf))) > 0) {
00092     if (fwrite(buf, len, 1, fp) != 1)
00093       break;
00094     offset += len;
00095   }
00096 
00097   fclose(fp);
00098   return len == 0 ? 0 : -1;
00099 }
00100 
00101 int mar_extract(const char *path) {
00102   MarFile *mar;
00103   int rv;
00104 
00105   mar = mar_open(path);
00106   if (!mar)
00107     return -1;
00108 
00109   rv = mar_enum_items(mar, mar_test_callback, NULL);
00110 
00111   mar_close(mar);
00112   return rv;
00113 }