Bug 479382 - libmar doesn't build on windows ce (uses posix file io); r=benjamin
authorBrad Lassey <bugmail@lassey.us>
Sun, 15 Mar 2009 14:59:59 +0100
changeset 26178 0720a4f0eeec1b58b48cb7f7da87dd39bf1229f7
parent 26177 f76138399b03d15d6c21884a77a285fa31cece85
child 26179 964586e5dbadef7cc75bf4be503470eeb0efc285
push idunknown
push userunknown
push dateunknown
reviewersbenjamin
bugs479382
milestone1.9.2a1pre
Bug 479382 - libmar doesn't build on windows ce (uses posix file io); r=benjamin
modules/libmar/src/mar_create.c
modules/libmar/src/mar_extract.c
--- a/modules/libmar/src/mar_create.c
+++ b/modules/libmar/src/mar_create.c
@@ -123,17 +123,24 @@ static int mar_concat_file(FILE *fp, con
 
   fclose(in);
   return rv;
 }
 
 int mar_create(const char *dest, int num_files, char **files) {
   struct MarItemStack stack;
   PRUint32 offset_to_index = 0, size_of_index;
+#ifdef WINCE
+  WIN32_FIND_DATAW file_data;
+  PRUnichar wide_path[MAX_PATH];
+  size_t size;
+  PRInt32 flags;
+#else
   struct stat st;
+#endif
   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);
@@ -143,22 +150,34 @@ int mar_create(const char *dest, int num
   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) {
+#ifdef WINCE
+    MultiByteToWideChar(CP_ACP, 0, files[i], -1, wide_path, MAX_PATH);
+    if (!FindFirstFile(wide_path, &file_data)) {
+#else
     if (stat(files[i], &st)) {
+#endif
       fprintf(stderr, "ERROR: file not found: %s\n", files[i]);
       goto failure;
     }
+#ifdef WINCE
+    flags = (file_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ?
+      0444 : 0666;
+    size = (file_data.nFileSizeHigh * (MAXDWORD + 1)) + file_data.nFileSizeLow;
+    if (mar_push(&stack, size, flags, files[i]))
+#else
 
     if (mar_push(&stack, st.st_size, st.st_mode & 0777, files[i]))
+#endif
       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) */
@@ -176,11 +195,18 @@ int mar_create(const char *dest, int num
     goto failure;
 
   rv = 0;
 failure: 
   if (stack.head)
     free(stack.head);
   fclose(fp);
   if (rv)
+#ifdef WINCE
+    {
+      MultiByteToWideChar(CP_ACP, 0, dest, -1, wide_path, MAX_PATH);
+      DeleteFileW(wide_path);
+    }
+#else
     remove(dest);
+#endif
   return rv;
 }
--- a/modules/libmar/src/mar_extract.c
+++ b/modules/libmar/src/mar_extract.c
@@ -43,53 +43,67 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include "mar.h"
 #include "mar_private.h"
 
 #ifdef XP_WIN
 #include <io.h>
 #include <direct.h>
+#ifdef WINCE
+#include <windows.h>
+#endif
 #endif
 
 /* Ensure that the directory containing this file exists */
 static int mar_ensure_parent_dir(const char *path)
 {
   char *slash = strrchr(path, '/');
+#ifdef WINCE
+  wchar_t wide_path[MAX_PATH];
+#endif
   if (slash)
   {
     *slash = '\0';
     mar_ensure_parent_dir(path);
 #ifdef XP_WIN
+#ifdef WINCE
+    MultiByteToWideChar(CP_ACP, 0, path, -1, wide_path, MAX_PATH);
+    CreateDirectory(wide_path, NULL);
+#else
     _mkdir(path);
+#endif
 #else
     mkdir(path, 0755);
 #endif
     *slash = '/';
   }
   return 0;
 }
 
 static int mar_test_callback(MarFile *mar, const MarItem *item, void *unused) {
   FILE *fp;
   char buf[BLOCKSIZE];
   int fd, len, offset = 0;
 
   if (mar_ensure_parent_dir(item->name))
     return -1;
-
+#ifdef WINCE 
+  fp = fopen(item->name, "bw");
+#else
 #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");
+#endif
   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;
   }