fragglet / lhasa

Free Software LHA implementation
http://fragglet.github.io/lhasa/
ISC License
80 stars 15 forks source link

MinGW compilation for 0.04 #7

Closed roytam1 closed 12 years ago

roytam1 commented 12 years ago

this patch fix 2 issues:

index c01b3a6..b0e09ae 100644
--- a/lib/lha_arch_win32.c
+++ b/lib/lha_arch_win32.c
@@ -32,10 +32,16 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include <utime.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <errno.h>

 int lha_arch_mkdir(char *path, unsigned int unix_mode)
 {
-       return mkdir(path) == 0;
+       int i;
+
+       i = mkdir(path);
+       if(i < 0 && errno == EEXIST) /* ignore if dir exists */
+               i = 0;
+       return i == 0;
 }

 int lha_arch_chown(char *filename, int unix_uid, int unix_gid)
diff --git a/src/safe.c b/src/safe.c
index 9e3034c..373033f 100644
--- a/src/safe.c
+++ b/src/safe.c
@@ -23,6 +23,18 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include <stdarg.h>
 #include <stdlib.h>

+#if LHA_ARCH == LHA_ARCH_WINDOWS
+#include <stdarg.h>
+int vasprintf( char **sptr, char *fmt, va_list argv )
+{
+    int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
+    if( (wanted > 0) && ((*sptr = malloc( 1 + wanted )) != NULL) )
+        return vsprintf( *sptr, fmt, argv );
+
+    return wanted;
+}
+#endif
+
 // Routines for safe terminal output.
 //
 // Data in LHA files (eg. filenames) may contain malicious string
fragglet commented 12 years ago

Thanks for the patch. Would you be willing to rework it slightly?

I've set up the lha_arch layer to separate the architecture-specific code from the non-specific code, to specifically avoid putting "#if LHA_ARCH ==" throughout the program. So if vasprintf is a problem on Windows, it should be moved into the layer.

Also, I'm a bit confused as to why mkdir() needs to ignore existing directories. As far as I know, the extraction code should only be creating directories if they don't already exist. Is this not the case?

Thanks!

roytam1 commented 12 years ago

Also, I'm a bit confused as to why mkdir() needs to ignore existing directories. As far as I know, the extraction code should only be creating directories if they don't already exist. Is this not the case?

I didn't dig though the sources, but it creates the directory and it checks the newly created directory every file that inside that directory when extracting.

EDIT:

LHAFileType lha_arch_exists(char *filename)
{
    // TODO
    return LHA_FILE_NONE;
}

so that's the problem.

file: http://homepage3.nifty.com/csdinc/archiver/lib/bga32037.lzh

without the patch:

16:42 D:\>lha bga32037.lzh
 PERMSSN    UID  GID      SIZE  RATIO     STAMP           NAME
---------- ----------- ------- ------ ------------ --------------------
[MS-DOS]                372736  57.1% Jun  4  2002 Bga32.dll
[MS-DOS]                 19586  37.7% Jun  4  2002 Bga32.txt
[MS-DOS]                     0 ****** Jun  4  2002 sdk/
[MS-DOS]                 25338  24.2% Jun  4  2002 SDK/Bga32Api.txt
[MS-DOS]                 14911  33.7% Jun  4  2002 SDK/Bga32Cmd.txt
[MS-DOS]                 17213  29.3% Jun  4  2002 SDK/Bga32Api.h
[MS-DOS]                  6144  20.5% Jun  4  2002 SDK/Bga32.lib
---------- ----------- ------- ------ ------------ --------------------
 Total         7 files  455928  52.2% Oct 18  2010

16:42 D:\>lha x bga32037.lzh
Bga32.dll       - Melted   :  oooooooooooooooooooooooooooooooooooooooooooooo
Bga32.txt       - Melted   :  ooo
Failed to create parent directory SDK/
Failed to create parent directory SDK/
Failed to create parent directory SDK/
Failed to create parent directory SDK/

16:43 D:\>dir/b sdk

with the patch:

16:43 D:\>lha x bga32037.lzh
Bga32.dll       - Melted   :  oooooooooooooooooooooooooooooooooooooooooooooo
Bga32.txt       - Melted   :  ooo
SDK/Bga32Api.txt        - Melted   :  oooo
SDK/Bga32Cmd.txt        - Melted   :  oo
SDK/Bga32Api.h  - Melted   :  ooo
SDK/Bga32.lib   - Melted   :  o

16:44 D:\>dir/b sdk
Bga32.lib
Bga32Api.h
Bga32Api.txt
Bga32Cmd.txt
roytam1 commented 12 years ago

Thanks for the patch. Would you be willing to rework it slightly?

Sure.

diff --git a/lib/lha_arch_win32.c b/lib/lha_arch_win32.c
index c01b3a6..15d86e9 100644
--- a/lib/lha_arch_win32.c
+++ b/lib/lha_arch_win32.c
@@ -32,10 +32,26 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include <utime.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <errno.h>
+#include <stdarg.h>
+
+int vasprintf( char **sptr, char *fmt, va_list argv )
+{
+    int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
+    if( (wanted > 0) && ((*sptr = malloc( 1 + wanted )) != NULL) )
+        return vsprintf( *sptr, fmt, argv );
+
+    return wanted;
+}

 int lha_arch_mkdir(char *path, unsigned int unix_mode)
 {
-       return mkdir(path) == 0;
+       int i;
+
+       i = mkdir(path);
+       if(i < 0 && errno == EEXIST) /* ignore if dir exists */
+               i = 0;
+       return i == 0;
 }

 int lha_arch_chown(char *filename, int unix_uid, int unix_gid)
fragglet commented 12 years ago

Ah, I see. The problem is with lha_arch_exists, not lha_arch_mkdir.

Several functions in lha_arch_win32.c aren't implemented yet. I'm hoping to put together a fully working and tested MingW build soon - as you can see it's rather sketchy at the moment.

fragglet commented 12 years ago

Hi,

I've implemented the incomplete functions in lha_arch_win32.c and got the test suite running on Windows. The current version on head passes all tests, so hopefully it should work a bit better for you.

Thanks!