Tcl Source Code

Changes On Branch core_zip_vfs-8-6
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch core_zip_vfs-aspect Excluding Merge-Ins

This is equivalent to a diff from ec5b26585d to 79dde51515

2017-11-17
12:54
Integrating fixes from aspect that make the //zipfs:/ mount point viable, and also allow one archive... check-in: 27df0ecb87 user: hypnotoad tags: core_zip_vfs
12:39
Pulling changes from core-zip-vfs Closed-Leaf check-in: 79dde51515 user: hypnotoad tags: core_zip_vfs-8-6, core_zip_vfs-aspect
12:29
Pulling changes from core-8-branch check-in: 044a5cf7f7 user: hypnotoad tags: core_zip_vfs
12:28
Closing a fork check-in: d533f130ec user: hypnotoad tags: core_zip_vfs
11:53
fix [glob] handling of leading // check-in: 66fe3ce72b user: aspect tags: core_zip_vfs-8-6, core_zip_vfs-aspect
04:47
Rebuilt the configure files without the call to the missing SC_ENABLE_ZIPFS macro Closed-Leaf check-in: ec5b26585d user: hypnotoad tags: core_zip_vfs-8-6
04:45
Final tweaks to get Msys Zipfs build working check-in: 0d0ba33c7c user: tne tags: core_zip_vfs-8-6

Changes to generic/tcl.decls.

938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
}
declare 265 {
    int Tcl_DumpActiveMemory(const char *fileName)
}
declare 266 {
    void Tcl_ValidateAllMemory(const char *file, int line)
}
declare 267 {
    void Tcl_AppendResultVA(Tcl_Interp *interp, va_list argList)
}
declare 268 {
    void Tcl_AppendStringsToObjVA(Tcl_Obj *objPtr, va_list argList)
}
declare 269 {
    char *Tcl_HashStats(Tcl_HashTable *tablePtr)
}
declare 270 {
    CONST84_RETURN char *Tcl_ParseVar(Tcl_Interp *interp, const char *start,







|


|







938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
}
declare 265 {
    int Tcl_DumpActiveMemory(const char *fileName)
}
declare 266 {
    void Tcl_ValidateAllMemory(const char *file, int line)
}
declare 267 {deprecated {see TIP #422}} {
    void Tcl_AppendResultVA(Tcl_Interp *interp, va_list argList)
}
declare 268 {deprecated {see TIP #422}} {
    void Tcl_AppendStringsToObjVA(Tcl_Obj *objPtr, va_list argList)
}
declare 269 {
    char *Tcl_HashStats(Tcl_HashTable *tablePtr)
}
declare 270 {
    CONST84_RETURN char *Tcl_ParseVar(Tcl_Interp *interp, const char *start,
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
	    const char *version)
}
# TIP #268: The internally used new Require function is in slot 573.
declare 274 {
    CONST84_RETURN char *Tcl_PkgRequire(Tcl_Interp *interp, const char *name,
	    const char *version, int exact)
}
declare 275 {
    void Tcl_SetErrorCodeVA(Tcl_Interp *interp, va_list argList)
}
declare 276 {
    int  Tcl_VarEvalVA(Tcl_Interp *interp, va_list argList)
}
declare 277 {
    Tcl_Pid Tcl_WaitPid(Tcl_Pid pid, int *statPtr, int options)
}
declare 278 {
    TCL_NORETURN void Tcl_PanicVA(const char *format, va_list argList)
}
declare 279 {
    void Tcl_GetVersion(int *major, int *minor, int *patchLevel, int *type)
}
declare 280 {
    void Tcl_InitMemory(Tcl_Interp *interp)







|


|





|







969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
	    const char *version)
}
# TIP #268: The internally used new Require function is in slot 573.
declare 274 {
    CONST84_RETURN char *Tcl_PkgRequire(Tcl_Interp *interp, const char *name,
	    const char *version, int exact)
}
declare 275 {deprecated {see TIP #422}} {
    void Tcl_SetErrorCodeVA(Tcl_Interp *interp, va_list argList)
}
declare 276 {deprecated {see TIP #422}} {
    int  Tcl_VarEvalVA(Tcl_Interp *interp, va_list argList)
}
declare 277 {
    Tcl_Pid Tcl_WaitPid(Tcl_Pid pid, int *statPtr, int options)
}
declare 278 {deprecated {see TIP #422}} {
    TCL_NORETURN void Tcl_PanicVA(const char *format, va_list argList)
}
declare 279 {
    void Tcl_GetVersion(int *major, int *minor, int *patchLevel, int *type)
}
declare 280 {
    void Tcl_InitMemory(Tcl_Interp *interp)

Changes to generic/tclDecls.h.

810
811
812
813
814
815
816

817
818
819

820
821
822
823
824
825
826
827
EXTERN void		Tcl_WrongNumArgs(Tcl_Interp *interp, int objc,
				Tcl_Obj *const objv[], const char *message);
/* 265 */
EXTERN int		Tcl_DumpActiveMemory(const char *fileName);
/* 266 */
EXTERN void		Tcl_ValidateAllMemory(const char *file, int line);
/* 267 */

EXTERN void		Tcl_AppendResultVA(Tcl_Interp *interp,
				va_list argList);
/* 268 */

EXTERN void		Tcl_AppendStringsToObjVA(Tcl_Obj *objPtr,
				va_list argList);
/* 269 */
EXTERN char *		Tcl_HashStats(Tcl_HashTable *tablePtr);
/* 270 */
EXTERN CONST84_RETURN char * Tcl_ParseVar(Tcl_Interp *interp,
				const char *start, CONST84 char **termPtr);
/* 271 */







>
|


>
|







810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
EXTERN void		Tcl_WrongNumArgs(Tcl_Interp *interp, int objc,
				Tcl_Obj *const objv[], const char *message);
/* 265 */
EXTERN int		Tcl_DumpActiveMemory(const char *fileName);
/* 266 */
EXTERN void		Tcl_ValidateAllMemory(const char *file, int line);
/* 267 */
TCL_DEPRECATED("see TIP #422")
void			Tcl_AppendResultVA(Tcl_Interp *interp,
				va_list argList);
/* 268 */
TCL_DEPRECATED("see TIP #422")
void			Tcl_AppendStringsToObjVA(Tcl_Obj *objPtr,
				va_list argList);
/* 269 */
EXTERN char *		Tcl_HashStats(Tcl_HashTable *tablePtr);
/* 270 */
EXTERN CONST84_RETURN char * Tcl_ParseVar(Tcl_Interp *interp,
				const char *start, CONST84 char **termPtr);
/* 271 */
836
837
838
839
840
841
842

843
844
845

846
847
848
849

850
851
852
853
854
855
856
857
EXTERN int		Tcl_PkgProvide(Tcl_Interp *interp, const char *name,
				const char *version);
/* 274 */
EXTERN CONST84_RETURN char * Tcl_PkgRequire(Tcl_Interp *interp,
				const char *name, const char *version,
				int exact);
/* 275 */

EXTERN void		Tcl_SetErrorCodeVA(Tcl_Interp *interp,
				va_list argList);
/* 276 */

EXTERN int		Tcl_VarEvalVA(Tcl_Interp *interp, va_list argList);
/* 277 */
EXTERN Tcl_Pid		Tcl_WaitPid(Tcl_Pid pid, int *statPtr, int options);
/* 278 */

EXTERN TCL_NORETURN void Tcl_PanicVA(const char *format, va_list argList);
/* 279 */
EXTERN void		Tcl_GetVersion(int *major, int *minor,
				int *patchLevel, int *type);
/* 280 */
EXTERN void		Tcl_InitMemory(Tcl_Interp *interp);
/* 281 */
EXTERN Tcl_Channel	Tcl_StackChannel(Tcl_Interp *interp,







>
|


>
|



>
|







838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
EXTERN int		Tcl_PkgProvide(Tcl_Interp *interp, const char *name,
				const char *version);
/* 274 */
EXTERN CONST84_RETURN char * Tcl_PkgRequire(Tcl_Interp *interp,
				const char *name, const char *version,
				int exact);
/* 275 */
TCL_DEPRECATED("see TIP #422")
void			Tcl_SetErrorCodeVA(Tcl_Interp *interp,
				va_list argList);
/* 276 */
TCL_DEPRECATED("see TIP #422")
int			Tcl_VarEvalVA(Tcl_Interp *interp, va_list argList);
/* 277 */
EXTERN Tcl_Pid		Tcl_WaitPid(Tcl_Pid pid, int *statPtr, int options);
/* 278 */
TCL_DEPRECATED("see TIP #422")
TCL_NORETURN void	Tcl_PanicVA(const char *format, va_list argList);
/* 279 */
EXTERN void		Tcl_GetVersion(int *major, int *minor,
				int *patchLevel, int *type);
/* 280 */
EXTERN void		Tcl_InitMemory(Tcl_Interp *interp);
/* 281 */
EXTERN Tcl_Channel	Tcl_StackChannel(Tcl_Interp *interp,
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
    int (*tcl_VarEval) (Tcl_Interp *interp, ...); /* 260 */
    ClientData (*tcl_VarTraceInfo) (Tcl_Interp *interp, const char *varName, int flags, Tcl_VarTraceProc *procPtr, ClientData prevClientData); /* 261 */
    ClientData (*tcl_VarTraceInfo2) (Tcl_Interp *interp, const char *part1, const char *part2, int flags, Tcl_VarTraceProc *procPtr, ClientData prevClientData); /* 262 */
    int (*tcl_Write) (Tcl_Channel chan, const char *s, int slen); /* 263 */
    void (*tcl_WrongNumArgs) (Tcl_Interp *interp, int objc, Tcl_Obj *const objv[], const char *message); /* 264 */
    int (*tcl_DumpActiveMemory) (const char *fileName); /* 265 */
    void (*tcl_ValidateAllMemory) (const char *file, int line); /* 266 */
    void (*tcl_AppendResultVA) (Tcl_Interp *interp, va_list argList); /* 267 */
    void (*tcl_AppendStringsToObjVA) (Tcl_Obj *objPtr, va_list argList); /* 268 */
    char * (*tcl_HashStats) (Tcl_HashTable *tablePtr); /* 269 */
    CONST84_RETURN char * (*tcl_ParseVar) (Tcl_Interp *interp, const char *start, CONST84 char **termPtr); /* 270 */
    CONST84_RETURN char * (*tcl_PkgPresent) (Tcl_Interp *interp, const char *name, const char *version, int exact); /* 271 */
    CONST84_RETURN char * (*tcl_PkgPresentEx) (Tcl_Interp *interp, const char *name, const char *version, int exact, void *clientDataPtr); /* 272 */
    int (*tcl_PkgProvide) (Tcl_Interp *interp, const char *name, const char *version); /* 273 */
    CONST84_RETURN char * (*tcl_PkgRequire) (Tcl_Interp *interp, const char *name, const char *version, int exact); /* 274 */
    void (*tcl_SetErrorCodeVA) (Tcl_Interp *interp, va_list argList); /* 275 */
    int (*tcl_VarEvalVA) (Tcl_Interp *interp, va_list argList); /* 276 */
    Tcl_Pid (*tcl_WaitPid) (Tcl_Pid pid, int *statPtr, int options); /* 277 */
    TCL_NORETURN1 void (*tcl_PanicVA) (const char *format, va_list argList); /* 278 */
    void (*tcl_GetVersion) (int *major, int *minor, int *patchLevel, int *type); /* 279 */
    void (*tcl_InitMemory) (Tcl_Interp *interp); /* 280 */
    Tcl_Channel (*tcl_StackChannel) (Tcl_Interp *interp, const Tcl_ChannelType *typePtr, ClientData instanceData, int mask, Tcl_Channel prevChan); /* 281 */
    int (*tcl_UnstackChannel) (Tcl_Interp *interp, Tcl_Channel chan); /* 282 */
    Tcl_Channel (*tcl_GetStackedChannel) (Tcl_Channel chan); /* 283 */
    void (*tcl_SetMainLoop) (Tcl_MainLoopProc *proc); /* 284 */
    void (*reserved285)(void);







|
|






|
|

|







2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
    int (*tcl_VarEval) (Tcl_Interp *interp, ...); /* 260 */
    ClientData (*tcl_VarTraceInfo) (Tcl_Interp *interp, const char *varName, int flags, Tcl_VarTraceProc *procPtr, ClientData prevClientData); /* 261 */
    ClientData (*tcl_VarTraceInfo2) (Tcl_Interp *interp, const char *part1, const char *part2, int flags, Tcl_VarTraceProc *procPtr, ClientData prevClientData); /* 262 */
    int (*tcl_Write) (Tcl_Channel chan, const char *s, int slen); /* 263 */
    void (*tcl_WrongNumArgs) (Tcl_Interp *interp, int objc, Tcl_Obj *const objv[], const char *message); /* 264 */
    int (*tcl_DumpActiveMemory) (const char *fileName); /* 265 */
    void (*tcl_ValidateAllMemory) (const char *file, int line); /* 266 */
    TCL_DEPRECATED_API("see TIP #422") void (*tcl_AppendResultVA) (Tcl_Interp *interp, va_list argList); /* 267 */
    TCL_DEPRECATED_API("see TIP #422") void (*tcl_AppendStringsToObjVA) (Tcl_Obj *objPtr, va_list argList); /* 268 */
    char * (*tcl_HashStats) (Tcl_HashTable *tablePtr); /* 269 */
    CONST84_RETURN char * (*tcl_ParseVar) (Tcl_Interp *interp, const char *start, CONST84 char **termPtr); /* 270 */
    CONST84_RETURN char * (*tcl_PkgPresent) (Tcl_Interp *interp, const char *name, const char *version, int exact); /* 271 */
    CONST84_RETURN char * (*tcl_PkgPresentEx) (Tcl_Interp *interp, const char *name, const char *version, int exact, void *clientDataPtr); /* 272 */
    int (*tcl_PkgProvide) (Tcl_Interp *interp, const char *name, const char *version); /* 273 */
    CONST84_RETURN char * (*tcl_PkgRequire) (Tcl_Interp *interp, const char *name, const char *version, int exact); /* 274 */
    TCL_DEPRECATED_API("see TIP #422") void (*tcl_SetErrorCodeVA) (Tcl_Interp *interp, va_list argList); /* 275 */
    TCL_DEPRECATED_API("see TIP #422") int (*tcl_VarEvalVA) (Tcl_Interp *interp, va_list argList); /* 276 */
    Tcl_Pid (*tcl_WaitPid) (Tcl_Pid pid, int *statPtr, int options); /* 277 */
    TCL_DEPRECATED_API("see TIP #422") TCL_NORETURN1 void (*tcl_PanicVA) (const char *format, va_list argList); /* 278 */
    void (*tcl_GetVersion) (int *major, int *minor, int *patchLevel, int *type); /* 279 */
    void (*tcl_InitMemory) (Tcl_Interp *interp); /* 280 */
    Tcl_Channel (*tcl_StackChannel) (Tcl_Interp *interp, const Tcl_ChannelType *typePtr, ClientData instanceData, int mask, Tcl_Channel prevChan); /* 281 */
    int (*tcl_UnstackChannel) (Tcl_Interp *interp, Tcl_Channel chan); /* 282 */
    Tcl_Channel (*tcl_GetStackedChannel) (Tcl_Channel chan); /* 283 */
    void (*tcl_SetMainLoop) (Tcl_MainLoopProc *proc); /* 284 */
    void (*reserved285)(void);

Changes to generic/tclFileName.c.

1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
	 * ':' no longer needed as a separator. It is only relevant to the
	 * beginning of the path.
	 */

	separators = "/\\";

    } else if (tclPlatform == TCL_PLATFORM_UNIX) {
	if (pathPrefix == NULL && tail[0] == '/') {
	    pathPrefix = Tcl_NewStringObj(tail, 1);
	    tail++;
	    Tcl_IncrRefCount(pathPrefix);
	}
    }

    /*







|







1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
	 * ':' no longer needed as a separator. It is only relevant to the
	 * beginning of the path.
	 */

	separators = "/\\";

    } else if (tclPlatform == TCL_PLATFORM_UNIX) {
	if (pathPrefix == NULL && tail[0] == '/' && tail[1] != '/') {
	    pathPrefix = Tcl_NewStringObj(tail, 1);
	    tail++;
	    Tcl_IncrRefCount(pathPrefix);
	}
    }

    /*

Changes to generic/tclStubInit.c.

68
69
70
71
72
73
74





75
76
77
78
79
80
81
#   define TclWinGetServByName 0
#   define TclWinGetSockOpt 0
#   define TclWinSetSockOpt 0
#   define TclWinNToHS 0
#   define TclBNInitBignumFromWideUInt 0
#   define TclBNInitBignumFromWideInt 0
#   define TclBNInitBignumFromLong 0





#else
#define TclSetStartupScriptPath setStartupScriptPath
static void TclSetStartupScriptPath(Tcl_Obj *path)
{
    Tcl_SetStartupScript(path, NULL);
}
#define TclGetStartupScriptPath getStartupScriptPath







>
>
>
>
>







68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#   define TclWinGetServByName 0
#   define TclWinGetSockOpt 0
#   define TclWinSetSockOpt 0
#   define TclWinNToHS 0
#   define TclBNInitBignumFromWideUInt 0
#   define TclBNInitBignumFromWideInt 0
#   define TclBNInitBignumFromLong 0
#   define Tcl_AppendResultVA 0
#   define Tcl_AppendStringsToObjVA 0
#   define Tcl_SetErrorCodeVA 0
#   define Tcl_PanicVA 0
#   define Tcl_VarEvalVA 0
#else
#define TclSetStartupScriptPath setStartupScriptPath
static void TclSetStartupScriptPath(Tcl_Obj *path)
{
    Tcl_SetStartupScript(path, NULL);
}
#define TclGetStartupScriptPath getStartupScriptPath

Changes to generic/tclZipfs.c.

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdlib.h>
#include <fcntl.h>

#ifdef HAVE_ZLIB
#include "zlib.h"
#include "crypt.h"

#define ZIPFS_VOLUME      "zipfs:/"
#define ZIPFS_APP_MOUNT   "zipfs:/app"
#define ZIPFS_ZIP_MOUNT   "zipfs:/lib/tcl"

#define ZIPFS_VOLUME_LEN 7

/*
 * Various constants and offsets found in ZIP archive files
 */

#define ZIP_SIG_LEN                     4








|
|
|

|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdlib.h>
#include <fcntl.h>

#ifdef HAVE_ZLIB
#include "zlib.h"
#include "crypt.h"

#define ZIPFS_VOLUME      "//zipfs:/"
#define ZIPFS_APP_MOUNT   "//zipfs:/app"
#define ZIPFS_ZIP_MOUNT   "//zipfs:/lib/tcl"

#define ZIPFS_VOLUME_LEN 9

/*
 * Various constants and offsets found in ZIP archive files
 */

#define ZIP_SIG_LEN                     4

499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
 *
 *-------------------------------------------------------------------------
 */

static char *
CanonicalPath(const char *root, const char *tail, Tcl_DString *dsPtr,int ZIPFSPATH)
{
  char *path;
  char *result;
  int i, j, c, isunc = 0, isvfs=0, n=0;
#if HAS_DRIVES
  int zipfspath=1;
  if ((tail[0] != '\0') && (strchr(drvletters, tail[0]) != NULL) &&
    (tail[1] == ':')) {
    tail += 2;
    zipfspath=0;
  }
  /* UNC style path */
  if (tail[0] == '\\') {
    root = "";
    ++tail;
    zipfspath=0;
  }
  if (tail[0] == '\\') {
    root = "/";
    ++tail;
    zipfspath=0;
  }
  if(zipfspath) {
#endif
    /* UNC style path */
    if(root && strncmp(root,ZIPFS_VOLUME,ZIPFS_VOLUME_LEN)==0) {
      isvfs=1;
    } else if (tail && strncmp(tail,ZIPFS_VOLUME,ZIPFS_VOLUME_LEN) == 0) {
      isvfs=2;
    }
    if(isvfs!=1) {
      if ((root[0] == '/') && (root[1] == '/')) {
        isunc = 1;
      }
    }
#if HAS_DRIVES
  }
#endif
  if(isvfs!=2) {
    if (tail[0] == '/') {
      if(isvfs!=1) {
        root = "";
      }
      ++tail;
      isunc = 0;
    }
    if (tail[0] == '/') {
      if(isvfs!=1) {
        root = "/";
      }
      ++tail;
      isunc = 1;
    }
  }
  i = strlen(root);
  j = strlen(tail);
  if(isvfs==1) {
    if(i>ZIPFS_VOLUME_LEN) {
      Tcl_DStringSetLength(dsPtr, i + j + 1);
      path = Tcl_DStringValue(dsPtr);
      memcpy(path, root, i);
      path[i++] = '/';
      memcpy(path + i, tail, j);
    } else {
      Tcl_DStringSetLength(dsPtr, i + j);
      path = Tcl_DStringValue(dsPtr);
      memcpy(path, root, i);
      memcpy(path + i, tail, j);
    }
  } else if(isvfs==2) {
    Tcl_DStringSetLength(dsPtr, j);
    path = Tcl_DStringValue(dsPtr);
    memcpy(path, tail, j);
  } else {
    if (ZIPFSPATH) {
      Tcl_DStringSetLength(dsPtr, i + j + ZIPFS_VOLUME_LEN);
      path = Tcl_DStringValue(dsPtr);
      memcpy(path, ZIPFS_VOLUME, ZIPFS_VOLUME_LEN);
      memcpy(path + ZIPFS_VOLUME_LEN + i , tail, j);
    } else {
      Tcl_DStringSetLength(dsPtr, i + j + 1);
      path = Tcl_DStringValue(dsPtr);
      memcpy(path, root, i);
      path[i++] = '/';
      memcpy(path + i, tail, j);
    }
  }
#if HAS_DRIVES
  for (i = 0; path[i] != '\0'; i++) {
  	if (path[i] == '\\') {
 	    path[i] = '/';
    }
  }
#endif
  if(ZIPFSPATH) {
    n=ZIPFS_VOLUME_LEN;
  } else {
    n=0;
  }
  for (i = j = n; (c = path[i]) != '\0'; i++) {
  	if (c == '/') {
	    int c2 = path[i + 1];
	    if (c2 == '/') {
    		continue;
	    }
	    if (c2 == '.') {
    		int c3 = path[i + 2];
        if ((c3 == '/') || (c3 == '\0')) {
          i++;
          continue;
        }
        if ((c3 == '.') &&
          ((path[i + 3] == '/') || (path [i + 3] == '\0'))) {
          i += 2;
          while ((j > 0) && (path[j - 1] != '/')) {
          	j--;
          }
          if (j > isunc) {
            --j;
            while ((j > 1 + isunc) && (path[j - 2] == '/')) {
              j--;
          	}
          }
          continue;
        }
	    }
    }
    path[j++] = c;
  }
  if (j == 0) {
    path[j++] = '/';
  }
  path[j] = 0;
  Tcl_DStringSetLength(dsPtr, j);
  result=Tcl_DStringValue(dsPtr);
  return result;
}



/*
 *-------------------------------------------------------------------------
 *
 * AbsolutePath --
 *
 *	This function computes the absolute path from a given
 *	(relative) path name into the specified Tcl_DString.
 *
 * Results:
 *	Returns the pointer to the absolute path contained in the
 *	specified Tcl_DString.
 *
 * Side effects:
 *	Modifies the specified Tcl_DString.
 *
 *-------------------------------------------------------------------------
 */

static char *
AbsolutePath(const char *path,
	Tcl_DString *dsPtr,
  int ZIPFSPATH)
{
  char *result;
  if (*path == '~') {
    Tcl_DStringAppend(dsPtr, path, -1);
    return Tcl_DStringValue(dsPtr);
  }
  if (*path != '/') {
    Tcl_DString pwd;

    /* relative path */
    Tcl_DStringInit(&pwd);
    Tcl_GetCwd(NULL, &pwd);
    result = Tcl_DStringValue(&pwd);
    result = CanonicalPath(result, path, dsPtr,ZIPFSPATH);
    Tcl_DStringFree(&pwd);
  } else {
    /* absolute path */
    result = CanonicalPath("", path, dsPtr,ZIPFSPATH);
  }
  return result;
}

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSLookup --
 *
 *	This function returns the ZIP entry struct corresponding to







|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|

|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|

|
|
|
|
|
|
|


|


|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643












































644
645
646
647
648
649
650
 *
 *-------------------------------------------------------------------------
 */

static char *
CanonicalPath(const char *root, const char *tail, Tcl_DString *dsPtr,int ZIPFSPATH)
{
    char *path;
    char *result;
    int i, j, c, isunc = 0, isvfs=0, n=0;
#if HAS_DRIVES
    int zipfspath=1;
    if ((tail[0] != '\0') && (strchr(drvletters, tail[0]) != NULL) &&
	    (tail[1] == ':')) {
	tail += 2;
	zipfspath=0;
    }
    /* UNC style path */
    if (tail[0] == '\\') {
	root = "";
	++tail;
	zipfspath=0;
    }
    if (tail[0] == '\\') {
	root = "/";
	++tail;
	zipfspath=0;
    }
    if(zipfspath) {
#endif
	/* UNC style path */
	if(root && strncmp(root,ZIPFS_VOLUME,ZIPFS_VOLUME_LEN)==0) {
	    isvfs=1;
	} else if (tail && strncmp(tail,ZIPFS_VOLUME,ZIPFS_VOLUME_LEN) == 0) {
	    isvfs=2;
	}
	if(isvfs!=1) {
	    if ((root[0] == '/') && (root[1] == '/')) {
		isunc = 1;
	    }
	}
#if HAS_DRIVES
    }
#endif
    if(isvfs!=2) {
	if (tail[0] == '/') {
	    if(isvfs!=1) {
		root = "";
	    }
	    ++tail;
	    isunc = 0;
	}
	if (tail[0] == '/') {
	    if(isvfs!=1) {
		root = "/";
	    }
	    ++tail;
	    isunc = 1;
	}
    }
    i = strlen(root);
    j = strlen(tail);
    if(isvfs==1) {
	if(i>ZIPFS_VOLUME_LEN) {
	    Tcl_DStringSetLength(dsPtr, i + j + 1);
	    path = Tcl_DStringValue(dsPtr);
	    memcpy(path, root, i);
	    path[i++] = '/';
	    memcpy(path + i, tail, j);
	} else {
	    Tcl_DStringSetLength(dsPtr, i + j);
	    path = Tcl_DStringValue(dsPtr);
	    memcpy(path, root, i);
	    memcpy(path + i, tail, j);
	}
    } else if(isvfs==2) {
	Tcl_DStringSetLength(dsPtr, j);
	path = Tcl_DStringValue(dsPtr);
	memcpy(path, tail, j);
    } else {
	if (ZIPFSPATH) {
	    Tcl_DStringSetLength(dsPtr, i + j + ZIPFS_VOLUME_LEN);
	    path = Tcl_DStringValue(dsPtr);
	    memcpy(path, ZIPFS_VOLUME, ZIPFS_VOLUME_LEN);
	    memcpy(path + ZIPFS_VOLUME_LEN + i , tail, j);
	} else {
	    Tcl_DStringSetLength(dsPtr, i + j + 1);
	    path = Tcl_DStringValue(dsPtr);
	    memcpy(path, root, i);
	    path[i++] = '/';
	    memcpy(path + i, tail, j);
	}
    }
#if HAS_DRIVES
    for (i = 0; path[i] != '\0'; i++) {
	if (path[i] == '\\') {
	    path[i] = '/';
	}
    }
#endif
    if(ZIPFSPATH) {
	n=ZIPFS_VOLUME_LEN;
    } else {
	n=0;
    }
    for (i = j = n; (c = path[i]) != '\0'; i++) {
	if (c == '/') {
	    int c2 = path[i + 1];
	    if (c2 == '/') {
		continue;
	    }
	    if (c2 == '.') {
		int c3 = path[i + 2];
		if ((c3 == '/') || (c3 == '\0')) {
		    i++;
		    continue;
		}
		if ((c3 == '.') &&
			((path[i + 3] == '/') || (path [i + 3] == '\0'))) {
		    i += 2;
		    while ((j > 0) && (path[j - 1] != '/')) {
			j--;
		    }
		    if (j > isunc) {
			--j;
			while ((j > 1 + isunc) && (path[j - 2] == '/')) {
			    j--;
			}
		    }
		    continue;
		}
	    }
	}
	path[j++] = c;
    }
    if (j == 0) {
	path[j++] = '/';
    }
    path[j] = 0;
    Tcl_DStringSetLength(dsPtr, j);
    result=Tcl_DStringValue(dsPtr);
    return result;
}















































/*
 *-------------------------------------------------------------------------
 *
 * ZipFSLookup --
 *
 *	This function returns the ZIP entry struct corresponding to
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
 *
 *-------------------------------------------------------------------------
 */

static ZipEntry *
ZipFSLookup(char *filename)
{
  char *realname;

  Tcl_HashEntry *hPtr;
  ZipEntry *z;
  Tcl_DString ds;
  Tcl_DStringInit(&ds);
  realname = AbsolutePath(filename, &ds, 1);
  hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, realname);
  z = hPtr ? (ZipEntry *) Tcl_GetHashValue(hPtr) : NULL;
  Tcl_DStringFree(&ds);
  return z;
}

#ifdef NEVER_USED

/*
 *-------------------------------------------------------------------------
 *







<
<
|
|
|
|
<
|
|
|
|







660
661
662
663
664
665
666


667
668
669
670

671
672
673
674
675
676
677
678
679
680
681
 *
 *-------------------------------------------------------------------------
 */

static ZipEntry *
ZipFSLookup(char *filename)
{


    Tcl_HashEntry *hPtr;
    ZipEntry *z;
    Tcl_DString ds;
    Tcl_DStringInit(&ds);

    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, filename);
    z = hPtr ? (ZipEntry *) Tcl_GetHashValue(hPtr) : NULL;
    Tcl_DStringFree(&ds);
    return z;
}

#ifdef NEVER_USED

/*
 *-------------------------------------------------------------------------
 *
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
 *
 *-------------------------------------------------------------------------
 */

static int
ZipFSLookupMount(char *filename)
{
  char *realname;
  Tcl_HashEntry *hPtr;
  Tcl_HashSearch search;
  ZipFile *zf;
  Tcl_DString ds;
  int match = 0;
  Tcl_DStringInit(&ds);
  realname = AbsolutePath(filename, &ds, 1);
  hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
  while (hPtr != NULL) {
    if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
      if (strcmp(zf->mntpt, realname) == 0) {
        match = 1;
        break;
      }
    }
    hPtr = Tcl_NextHashEntry(&search);
  }
  Tcl_DStringFree(&ds);
  return match;
}
#endif

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSCloseArchive --







<
|
|
|
<
|
<
<
|
|
|
|
|
|
|
|
|
|
<
|







692
693
694
695
696
697
698

699
700
701

702


703
704
705
706
707
708
709
710
711
712

713
714
715
716
717
718
719
720
 *
 *-------------------------------------------------------------------------
 */

static int
ZipFSLookupMount(char *filename)
{

    Tcl_HashEntry *hPtr;
    Tcl_HashSearch search;
    ZipFile *zf;

    int match = 0;


    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
    while (hPtr != NULL) {
	if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
	    if (strcmp(zf->mntpt, filename) == 0) {
		match = 1;
		break;
	    }
	}
	hPtr = Tcl_NextHashEntry(&search);
    }

    return match;
}
#endif

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSCloseArchive --
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
 *-------------------------------------------------------------------------
 */

int
TclZipfs_Mount(Tcl_Interp *interp, const char *zipname, const char *mntpt,
	    const char *passwd)
{
  char *realname, *p;
  int i, pwlen, isNew;
  ZipFile *zf, zf0;
  ZipEntry *z;
  Tcl_HashEntry *hPtr;
  Tcl_DString ds, dsm, fpBuf;
  unsigned char *q;

  ReadLock();
  if (!ZipFS.initialized) {
    if (interp != NULL) {
      Tcl_SetObjResult(interp,
         Tcl_NewStringObj("not initialized", -1));
    }
    Unlock();
    return TCL_ERROR;
  }
  if (zipname == NULL) {
    Tcl_HashSearch search;
    int ret = TCL_OK;

    i = 0;
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
    while (hPtr != NULL) {
      if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
        if (interp != NULL) {
          Tcl_AppendElement(interp, zf->mntpt);
          Tcl_AppendElement(interp, zf->name);
        }
        ++i;
      }
      hPtr = Tcl_NextHashEntry(&search);
    }
    if (interp == NULL) {
      ret = (i > 0) ? TCL_OK : TCL_BREAK;
    }
    Unlock();
    return ret;
  }
  if (mntpt == NULL) {
    if (interp == NULL) {
	    Unlock();
	    return TCL_OK;
    }
    Tcl_DStringInit(&ds);
    p = AbsolutePath(zipname, &ds, 0);
    hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, p);
    if (hPtr != NULL) {
      if ((zf = Tcl_GetHashValue(hPtr)) != NULL) {
        Tcl_SetObjResult(interp,
        Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
      }
    }
    Unlock();
    Tcl_DStringFree(&ds);
    return TCL_OK;
  }
  Unlock();
  pwlen = 0;
  if (passwd != NULL) {
    pwlen = strlen(passwd);
    if ((pwlen > 255) || (strchr(passwd, 0xff) != NULL)) {
	    if (interp) {
        Tcl_SetObjResult(interp,
      		 Tcl_NewStringObj("illegal password", -1));
	    }
	    return TCL_ERROR;
    }
  }
  if (ZipFSOpenArchive(interp, zipname, 1, &zf0) != TCL_OK) {
    return TCL_ERROR;
  }
  Tcl_DStringInit(&ds);
  realname = AbsolutePath(zipname, &ds, 0);
  /*
   * Mount point can come from Tcl_GetNameOfExecutable()
   * which sometimes is a relative or otherwise denormalized path.
   * But an absolute name is needed as mount point here.
   */
  Tcl_DStringInit(&dsm);
  mntpt = CanonicalPath("", mntpt, &dsm, 1);
  WriteLock();
  hPtr = Tcl_CreateHashEntry(&ZipFS.zipHash, realname, &isNew);
  Tcl_DStringSetLength(&ds, 0);
  if (!isNew) {
    zf = (ZipFile *) Tcl_GetHashValue(hPtr);
    if (interp != NULL) {
      Tcl_AppendResult(interp, "already mounted on \"", zf->mntptlen ?
         zf->mntpt : "/", "\"", (char *) NULL);
    }
    Unlock();
    Tcl_DStringFree(&ds);
    Tcl_DStringFree(&dsm);
    ZipFSCloseArchive(interp, &zf0);
    return TCL_ERROR;
  }
  if (strcmp(mntpt, "/") == 0) {
    mntpt = "";
  }
  zf = (ZipFile *) Tcl_AttemptAlloc(sizeof (*zf) + strlen(mntpt) + 1);
  if (zf == NULL) {
    if (interp != NULL) {
      Tcl_AppendResult(interp, "out of memory", (char *) NULL);
    }
    Unlock();
    Tcl_DStringFree(&ds);
    Tcl_DStringFree(&dsm);
    ZipFSCloseArchive(interp, &zf0);
    return TCL_ERROR;
  }
  *zf = zf0;
  zf->name = Tcl_GetHashKey(&ZipFS.zipHash, hPtr);
  strcpy(zf->mntpt, mntpt);
  zf->mntptlen = strlen(zf->mntpt);
  zf->entries = NULL;
  zf->topents = NULL;
  zf->nopen = 0;
  Tcl_SetHashValue(hPtr, (ClientData) zf);
  if ((zf->pwbuf[0] == 0) && pwlen) {
    int k = 0;
    i = pwlen;
    zf->pwbuf[k++] = i;
    while (i > 0) {
      zf->pwbuf[k] = (passwd[i - 1] & 0x0f) |
      pwrot[(passwd[i - 1] >> 4) & 0x0f];
      k++;
      i--;
    }
    zf->pwbuf[k] = '\0';
  }
  if (mntpt[0] != '\0') {
    z = (ZipEntry *) Tcl_Alloc(sizeof (*z));
    z->name = NULL;
    z->tnext = NULL;
    z->depth = CountSlashes(mntpt);
    z->zipfile = zf;
    z->isdir = 1;
    z->isenc = 0;
    z->offset = zf->baseoffs;
    z->crc32 = 0;
    z->timestamp = 0;
    z->nbyte = z->nbytecompr = 0;
    z->cmeth = ZIP_COMPMETH_STORED;
    z->data = NULL;
    hPtr = Tcl_CreateHashEntry(&ZipFS.fileHash, mntpt, &isNew);
    if (!isNew) {
	    /* skip it */
	    Tcl_Free((char *) z);
    } else {
	    Tcl_SetHashValue(hPtr, (ClientData) z);
	    z->name = Tcl_GetHashKey(&ZipFS.fileHash, hPtr);
	    z->next = zf->entries;
	    zf->entries = z;
    }
  }
  q = zf->data + zf->centoffs;
  Tcl_DStringInit(&fpBuf);

  for (i = 0; i < zf->nfiles; i++) {
    int pathlen, comlen, extra, isdir = 0, dosTime, dosDate, nbcompr, offs;
    unsigned char *lq, *gq = NULL;
    char *fullpath, *path;

    pathlen = zip_read_short(q + ZIP_CENTRAL_PATHLEN_OFFS);
    comlen = zip_read_short(q + ZIP_CENTRAL_FCOMMENTLEN_OFFS);
    extra = zip_read_short(q + ZIP_CENTRAL_EXTRALEN_OFFS);
    Tcl_DStringSetLength(&ds, 0);
    Tcl_DStringAppend(&ds, (char *) q + ZIP_CENTRAL_HEADER_LEN, pathlen);
    path = Tcl_DStringValue(&ds);
    if ((pathlen > 0) && (path[pathlen - 1] == '/')) {
	    Tcl_DStringSetLength(&ds, pathlen - 1);
	    path = Tcl_DStringValue(&ds);
	    isdir = 1;
    }
    if ((strcmp(path, ".") == 0) || (strcmp(path, "..") == 0)) {
	    goto nextent;
    }
    lq = zf->data + zf->baseoffs +
	     zip_read_int(q + ZIP_CENTRAL_LOCALHDR_OFFS);
    if ((lq < zf->data) || (lq > (zf->data + zf->length))) {
	    goto nextent;
    }
    nbcompr = zip_read_int(lq + ZIP_LOCAL_COMPLEN_OFFS);
    if (!isdir && (nbcompr == 0) &&
	    (zip_read_int(lq + ZIP_LOCAL_UNCOMPLEN_OFFS) == 0) &&
	    (zip_read_int(lq + ZIP_LOCAL_CRC32_OFFS) == 0)) {
	    gq = q;
	    nbcompr = zip_read_int(gq + ZIP_CENTRAL_COMPLEN_OFFS);
    }
    offs = (lq - zf->data)
	     + ZIP_LOCAL_HEADER_LEN
	     + zip_read_short(lq + ZIP_LOCAL_PATHLEN_OFFS)
	     + zip_read_short(lq + ZIP_LOCAL_EXTRALEN_OFFS);
    if ((offs + nbcompr) > zf->length) {
	    goto nextent;
    }
    if (!isdir && (mntpt[0] == '\0') && !CountSlashes(path)) {
#ifdef ANDROID
	    /*
	     * When mounting the ZIP archive on the root directory try
	     * to remap top level regular files of the archive to
	     * /assets/.root/... since this directory should not be
	     * in a valid APK due to the leading dot in the file name
	     * component. This trick should make the files
	     * AndroidManifest.xml, resources.arsc, and classes.dex
	     * visible to Tcl.
	     */
	    Tcl_DString ds2;

	    Tcl_DStringInit(&ds2);
	    Tcl_DStringAppend(&ds2, "assets/.root/", -1);
	    Tcl_DStringAppend(&ds2, path, -1);
	    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, Tcl_DStringValue(&ds2));
	    if (hPtr != NULL) {
        /* should not happen but skip it anyway */
        Tcl_DStringFree(&ds2);
        goto nextent;
	    }
	    Tcl_DStringSetLength(&ds, 0);
	    Tcl_DStringAppend(&ds, Tcl_DStringValue(&ds2),
			      Tcl_DStringLength(&ds2));
	    path = Tcl_DStringValue(&ds);
	    Tcl_DStringFree(&ds2);
#else
      /*
       * Regular files skipped when mounting on root.
       */
      goto nextent;
#endif
    }
    Tcl_DStringSetLength(&fpBuf, 0);
    fullpath = CanonicalPath(mntpt, path, &fpBuf, 1);
    z = (ZipEntry *) Tcl_Alloc(sizeof (*z));
    z->name = NULL;
    z->tnext = NULL;
    z->depth = CountSlashes(fullpath);
    z->zipfile = zf;
    z->isdir = isdir;
    z->isenc = (zip_read_short(lq + ZIP_LOCAL_FLAGS_OFFS) & 1)
	    && (nbcompr > 12);
    z->offset = offs;
    if (gq != NULL) {
	    z->crc32 = zip_read_int(gq + ZIP_CENTRAL_CRC32_OFFS);
	    dosDate = zip_read_short(gq + ZIP_CENTRAL_MDATE_OFFS);
	    dosTime = zip_read_short(gq + ZIP_CENTRAL_MTIME_OFFS);
	    z->timestamp = DosTimeDate(dosDate, dosTime);
	    z->nbyte = zip_read_int(gq + ZIP_CENTRAL_UNCOMPLEN_OFFS);
	    z->cmeth = zip_read_short(gq + ZIP_CENTRAL_COMPMETH_OFFS);
    } else {
	    z->crc32 = zip_read_int(lq + ZIP_LOCAL_CRC32_OFFS);
	    dosDate = zip_read_short(lq + ZIP_LOCAL_MDATE_OFFS);
	    dosTime = zip_read_short(lq + ZIP_LOCAL_MTIME_OFFS);
	    z->timestamp = DosTimeDate(dosDate, dosTime);
	    z->nbyte = zip_read_int(lq + ZIP_LOCAL_UNCOMPLEN_OFFS);
	    z->cmeth = zip_read_short(lq + ZIP_LOCAL_COMPMETH_OFFS);
    }
    z->nbytecompr = nbcompr;
    z->data = NULL;
    hPtr = Tcl_CreateHashEntry(&ZipFS.fileHash, fullpath, &isNew);
    if (!isNew) {
	    /* should not happen but skip it anyway */
	    Tcl_Free((char *) z);
    } else {
	    Tcl_SetHashValue(hPtr, (ClientData) z);
	    z->name = Tcl_GetHashKey(&ZipFS.fileHash, hPtr);
	    z->next = zf->entries;
	    zf->entries = z;
	    if (isdir && (mntpt[0] == '\0') && (z->depth == 1)) {
      	z->tnext = zf->topents;
      	zf->topents = z;
	    }
	    if (!z->isdir && (z->depth > 1)) {
      	char *dir, *end;
      	ZipEntry *zd;

      	Tcl_DStringSetLength(&ds, strlen(z->name) + 8);
      	Tcl_DStringSetLength(&ds, 0);
      	Tcl_DStringAppend(&ds, z->name, -1);
      	dir = Tcl_DStringValue(&ds);
      	end = strrchr(dir, '/');
      	while ((end != NULL) && (end != dir)) {
    	    Tcl_DStringSetLength(&ds, end - dir);
    	    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, dir);
    	    if (hPtr != NULL) {
        		break;
    	    }
    	    zd = (ZipEntry *) Tcl_Alloc(sizeof (*zd));
    	    zd->name = NULL;
    	    zd->tnext = NULL;
    	    zd->depth = CountSlashes(dir);
    	    zd->zipfile = zf;
    	    zd->isdir = 1;
    	    zd->isenc = 0;
    	    zd->offset = z->offset;
    	    zd->crc32 = 0;
    	    zd->timestamp = z->timestamp;
    	    zd->nbyte = zd->nbytecompr = 0;
    	    zd->cmeth = ZIP_COMPMETH_STORED;
    	    zd->data = NULL;
    	    hPtr = Tcl_CreateHashEntry(&ZipFS.fileHash, dir, &isNew);
    	    if (!isNew) {
        		/* should not happen but skip it anyway */
        		Tcl_Free((char *) zd);
    	    } else {
        		Tcl_SetHashValue(hPtr, (ClientData) zd);
        		zd->name = Tcl_GetHashKey(&ZipFS.fileHash, hPtr);
        		zd->next = zf->entries;
        		zf->entries = zd;
        		if ((mntpt[0] == '\0') && (zd->depth == 1)) {
      		    zd->tnext = zf->topents;
      		    zf->topents = zd;
        		}
          }
          end = strrchr(dir, '/');
        }
	    }
    }
nextent:
    q += pathlen + comlen + extra + ZIP_CENTRAL_HEADER_LEN;
  }
  Unlock();
  Tcl_DStringFree(&fpBuf);
  Tcl_DStringFree(&ds);
  Tcl_DStringFree(&dsm);
  Tcl_FSMountsChanged(NULL);
  return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * TclZipfs_Unmount --
 *







<
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|


|
<
<
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|

|
|


|
|
|
|
|
<
<
|
|
|
|
|
<
<
|
|
<
|
|
|
|
|
|
|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|


|




|
|
|
|
>
|
|
|
|

|
|
|
|
|
|
|



|
|

|
|
|
|

|
|
|
|
|


|
|
|
|
|
|

|
|

















|
|
|



|



|
|
|
|

|
|
|
|
|
|
|
|
|
|

|
|






|






|
|
|
|
|


|





|
|


|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|

|
|
|
|
|
<
|
|







996
997
998
999
1000
1001
1002

1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045


1046
1047
1048
1049
1050
1051
1052
1053

1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070


1071
1072
1073
1074
1075


1076
1077

1078
1079
1080
1081
1082
1083
1084


1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096


1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312

1313
1314
1315
1316
1317
1318
1319
1320
1321
 *-------------------------------------------------------------------------
 */

int
TclZipfs_Mount(Tcl_Interp *interp, const char *zipname, const char *mntpt,
	    const char *passwd)
{

    int i, pwlen, isNew;
    ZipFile *zf, zf0;
    ZipEntry *z;
    Tcl_HashEntry *hPtr;
    Tcl_DString ds, fpBuf;
    unsigned char *q;

    ReadLock();
    if (!ZipFS.initialized) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
		    Tcl_NewStringObj("not initialized", -1));
	}
	Unlock();
	return TCL_ERROR;
    }
    if (zipname == NULL) {
	Tcl_HashSearch search;
	int ret = TCL_OK;

	i = 0;
	hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
	while (hPtr != NULL) {
	    if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
		if (interp != NULL) {
		    Tcl_AppendElement(interp, zf->mntpt);
		    Tcl_AppendElement(interp, zf->name);
		}
		++i;
	    }
	    hPtr = Tcl_NextHashEntry(&search);
	}
	if (interp == NULL) {
	    ret = (i > 0) ? TCL_OK : TCL_BREAK;
	}
	Unlock();
	return ret;
    }
    if (mntpt == NULL) {
	if (interp == NULL) {
	    Unlock();
	    return TCL_OK;
	}


	hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, zipname);
	if (hPtr != NULL) {
	    if ((zf = Tcl_GetHashValue(hPtr)) != NULL) {
		Tcl_SetObjResult(interp,
			Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
	    }
	}
	Unlock();

	return TCL_OK;
    }
    Unlock();
    pwlen = 0;
    if (passwd != NULL) {
	pwlen = strlen(passwd);
	if ((pwlen > 255) || (strchr(passwd, 0xff) != NULL)) {
	    if (interp) {
		Tcl_SetObjResult(interp,
			Tcl_NewStringObj("illegal password", -1));
	    }
	    return TCL_ERROR;
	}
    }
    if (ZipFSOpenArchive(interp, zipname, 1, &zf0) != TCL_OK) {
	return TCL_ERROR;
    }


    /*
     * Mount point can come from Tcl_GetNameOfExecutable()
     * which sometimes is a relative or otherwise denormalized path.
     * But an absolute name is needed as mount point here.
     */


    WriteLock();
    hPtr = Tcl_CreateHashEntry(&ZipFS.zipHash, zipname, &isNew);

    if (!isNew) {
	zf = (ZipFile *) Tcl_GetHashValue(hPtr);
	if (interp != NULL) {
	    Tcl_AppendResult(interp, "already mounted on \"", zf->mntptlen ?
		    zf->mntpt : "/", "\"", (char *) NULL);
	}
	Unlock();


	ZipFSCloseArchive(interp, &zf0);
	return TCL_ERROR;
    }
    if (strcmp(mntpt, "/") == 0) {
	mntpt = "";
    }
    zf = (ZipFile *) Tcl_AttemptAlloc(sizeof (*zf) + strlen(mntpt) + 1);
    if (zf == NULL) {
	if (interp != NULL) {
	    Tcl_AppendResult(interp, "out of memory", (char *) NULL);
	}
	Unlock();


	ZipFSCloseArchive(interp, &zf0);
	return TCL_ERROR;
    }
    *zf = zf0;
    zf->name = Tcl_GetHashKey(&ZipFS.zipHash, hPtr);
    strcpy(zf->mntpt, mntpt);
    zf->mntptlen = strlen(zf->mntpt);
    zf->entries = NULL;
    zf->topents = NULL;
    zf->nopen = 0;
    Tcl_SetHashValue(hPtr, (ClientData) zf);
    if ((zf->pwbuf[0] == 0) && pwlen) {
	int k = 0;
	i = pwlen;
	zf->pwbuf[k++] = i;
	while (i > 0) {
	    zf->pwbuf[k] = (passwd[i - 1] & 0x0f) |
		pwrot[(passwd[i - 1] >> 4) & 0x0f];
	    k++;
	    i--;
	}
	zf->pwbuf[k] = '\0';
    }
    if (mntpt[0] != '\0') {
	z = (ZipEntry *) Tcl_Alloc(sizeof (*z));
	z->name = NULL;
	z->tnext = NULL;
	z->depth = CountSlashes(mntpt);
	z->zipfile = zf;
	z->isdir = 1;
	z->isenc = 0;
	z->offset = zf->baseoffs;
	z->crc32 = 0;
	z->timestamp = 0;
	z->nbyte = z->nbytecompr = 0;
	z->cmeth = ZIP_COMPMETH_STORED;
	z->data = NULL;
	hPtr = Tcl_CreateHashEntry(&ZipFS.fileHash, mntpt, &isNew);
	if (!isNew) {
	    /* skip it */
	    Tcl_Free((char *) z);
	} else {
	    Tcl_SetHashValue(hPtr, (ClientData) z);
	    z->name = Tcl_GetHashKey(&ZipFS.fileHash, hPtr);
	    z->next = zf->entries;
	    zf->entries = z;
	}
    }
    q = zf->data + zf->centoffs;
    Tcl_DStringInit(&fpBuf);
    Tcl_DStringInit(&ds);
    for (i = 0; i < zf->nfiles; i++) {
	int pathlen, comlen, extra, isdir = 0, dosTime, dosDate, nbcompr, offs;
	unsigned char *lq, *gq = NULL;
	char *fullpath, *path;

	pathlen = zip_read_short(q + ZIP_CENTRAL_PATHLEN_OFFS);
	comlen = zip_read_short(q + ZIP_CENTRAL_FCOMMENTLEN_OFFS);
	extra = zip_read_short(q + ZIP_CENTRAL_EXTRALEN_OFFS);
	Tcl_DStringSetLength(&ds, 0);
	Tcl_DStringAppend(&ds, (char *) q + ZIP_CENTRAL_HEADER_LEN, pathlen);
	path = Tcl_DStringValue(&ds);
	if ((pathlen > 0) && (path[pathlen - 1] == '/')) {
	    Tcl_DStringSetLength(&ds, pathlen - 1);
	    path = Tcl_DStringValue(&ds);
	    isdir = 1;
	}
	if ((strcmp(path, ".") == 0) || (strcmp(path, "..") == 0)) {
	    goto nextent;
	}
	lq = zf->data + zf->baseoffs +
	    zip_read_int(q + ZIP_CENTRAL_LOCALHDR_OFFS);
	if ((lq < zf->data) || (lq > (zf->data + zf->length))) {
	    goto nextent;
	}
	nbcompr = zip_read_int(lq + ZIP_LOCAL_COMPLEN_OFFS);
	if (!isdir && (nbcompr == 0) &&
		(zip_read_int(lq + ZIP_LOCAL_UNCOMPLEN_OFFS) == 0) &&
		(zip_read_int(lq + ZIP_LOCAL_CRC32_OFFS) == 0)) {
	    gq = q;
	    nbcompr = zip_read_int(gq + ZIP_CENTRAL_COMPLEN_OFFS);
	}
	offs = (lq - zf->data)
	    + ZIP_LOCAL_HEADER_LEN
	    + zip_read_short(lq + ZIP_LOCAL_PATHLEN_OFFS)
	    + zip_read_short(lq + ZIP_LOCAL_EXTRALEN_OFFS);
	if ((offs + nbcompr) > zf->length) {
	    goto nextent;
	}
	if (!isdir && (mntpt[0] == '\0') && !CountSlashes(path)) {
#ifdef ANDROID
	    /*
	     * When mounting the ZIP archive on the root directory try
	     * to remap top level regular files of the archive to
	     * /assets/.root/... since this directory should not be
	     * in a valid APK due to the leading dot in the file name
	     * component. This trick should make the files
	     * AndroidManifest.xml, resources.arsc, and classes.dex
	     * visible to Tcl.
	     */
	    Tcl_DString ds2;

	    Tcl_DStringInit(&ds2);
	    Tcl_DStringAppend(&ds2, "assets/.root/", -1);
	    Tcl_DStringAppend(&ds2, path, -1);
	    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, Tcl_DStringValue(&ds2));
	    if (hPtr != NULL) {
		/* should not happen but skip it anyway */
		Tcl_DStringFree(&ds2);
		goto nextent;
	    }
	    Tcl_DStringSetLength(&ds, 0);
	    Tcl_DStringAppend(&ds, Tcl_DStringValue(&ds2),
		    Tcl_DStringLength(&ds2));
	    path = Tcl_DStringValue(&ds);
	    Tcl_DStringFree(&ds2);
#else
	    /*
	     * Regular files skipped when mounting on root.
	     */
	    goto nextent;
#endif
	}
	Tcl_DStringSetLength(&fpBuf, 0);
	fullpath = CanonicalPath(mntpt, path, &fpBuf, 1);
	z = (ZipEntry *) Tcl_Alloc(sizeof (*z));
	z->name = NULL;
	z->tnext = NULL;
	z->depth = CountSlashes(fullpath);
	z->zipfile = zf;
	z->isdir = isdir;
	z->isenc = (zip_read_short(lq + ZIP_LOCAL_FLAGS_OFFS) & 1)
	    && (nbcompr > 12);
	z->offset = offs;
	if (gq != NULL) {
	    z->crc32 = zip_read_int(gq + ZIP_CENTRAL_CRC32_OFFS);
	    dosDate = zip_read_short(gq + ZIP_CENTRAL_MDATE_OFFS);
	    dosTime = zip_read_short(gq + ZIP_CENTRAL_MTIME_OFFS);
	    z->timestamp = DosTimeDate(dosDate, dosTime);
	    z->nbyte = zip_read_int(gq + ZIP_CENTRAL_UNCOMPLEN_OFFS);
	    z->cmeth = zip_read_short(gq + ZIP_CENTRAL_COMPMETH_OFFS);
	} else {
	    z->crc32 = zip_read_int(lq + ZIP_LOCAL_CRC32_OFFS);
	    dosDate = zip_read_short(lq + ZIP_LOCAL_MDATE_OFFS);
	    dosTime = zip_read_short(lq + ZIP_LOCAL_MTIME_OFFS);
	    z->timestamp = DosTimeDate(dosDate, dosTime);
	    z->nbyte = zip_read_int(lq + ZIP_LOCAL_UNCOMPLEN_OFFS);
	    z->cmeth = zip_read_short(lq + ZIP_LOCAL_COMPMETH_OFFS);
	}
	z->nbytecompr = nbcompr;
	z->data = NULL;
	hPtr = Tcl_CreateHashEntry(&ZipFS.fileHash, fullpath, &isNew);
	if (!isNew) {
	    /* should not happen but skip it anyway */
	    Tcl_Free((char *) z);
	} else {
	    Tcl_SetHashValue(hPtr, (ClientData) z);
	    z->name = Tcl_GetHashKey(&ZipFS.fileHash, hPtr);
	    z->next = zf->entries;
	    zf->entries = z;
	    if (isdir && (mntpt[0] == '\0') && (z->depth == 1)) {
		z->tnext = zf->topents;
		zf->topents = z;
	    }
	    if (!z->isdir && (z->depth > 1)) {
		char *dir, *end;
		ZipEntry *zd;

		Tcl_DStringSetLength(&ds, strlen(z->name) + 8);
		Tcl_DStringSetLength(&ds, 0);
		Tcl_DStringAppend(&ds, z->name, -1);
		dir = Tcl_DStringValue(&ds);
		end = strrchr(dir, '/');
		while ((end != NULL) && (end != dir)) {
		    Tcl_DStringSetLength(&ds, end - dir);
		    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, dir);
		    if (hPtr != NULL) {
			break;
		    }
		    zd = (ZipEntry *) Tcl_Alloc(sizeof (*zd));
		    zd->name = NULL;
		    zd->tnext = NULL;
		    zd->depth = CountSlashes(dir);
		    zd->zipfile = zf;
		    zd->isdir = 1;
		    zd->isenc = 0;
		    zd->offset = z->offset;
		    zd->crc32 = 0;
		    zd->timestamp = z->timestamp;
		    zd->nbyte = zd->nbytecompr = 0;
		    zd->cmeth = ZIP_COMPMETH_STORED;
		    zd->data = NULL;
		    hPtr = Tcl_CreateHashEntry(&ZipFS.fileHash, dir, &isNew);
		    if (!isNew) {
			/* should not happen but skip it anyway */
			Tcl_Free((char *) zd);
		    } else {
			Tcl_SetHashValue(hPtr, (ClientData) zd);
			zd->name = Tcl_GetHashKey(&ZipFS.fileHash, hPtr);
			zd->next = zf->entries;
			zf->entries = zd;
			if ((mntpt[0] == '\0') && (zd->depth == 1)) {
			    zd->tnext = zf->topents;
			    zf->topents = zd;
			}
		    }
		    end = strrchr(dir, '/');
		}
	    }
	}
nextent:
	q += pathlen + comlen + extra + ZIP_CENTRAL_HEADER_LEN;
    }
    Unlock();
    Tcl_DStringFree(&fpBuf);
    Tcl_DStringFree(&ds);

    Tcl_FSMountsChanged(NULL);
    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * TclZipfs_Unmount --
 *
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436


1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
 *
 *-------------------------------------------------------------------------
 */

int
TclZipfs_Unmount(Tcl_Interp *interp, const char *zipname)
{
  char *realname;
  ZipFile *zf;
  ZipEntry *z, *znext;
  Tcl_HashEntry *hPtr;
  Tcl_DString ds;
  int ret = TCL_OK, unmounted = 0;

  Tcl_DStringInit(&ds);
  realname = AbsolutePath(zipname, &ds, 0);
  WriteLock();
  if (!ZipFS.initialized) {
  	goto done;
  }
  hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, realname);
  if (hPtr == NULL) {
    /* don't report error */
  	goto done;
  }
  zf = (ZipFile *) Tcl_GetHashValue(hPtr);
  if (zf->nopen > 0) {
    if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("filesystem is busy", -1));
    }
    ret = TCL_ERROR;
    goto done;
  }
  Tcl_DeleteHashEntry(hPtr);
  for (z = zf->entries; z; z = znext) {
    znext = z->next;
    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, z->name);
    if (hPtr) {
	    Tcl_DeleteHashEntry(hPtr);
    }
    if (z->data != NULL) {
	    Tcl_Free((char *) z->data);


    }
    Tcl_Free((char *) z);
  }
  ZipFSCloseArchive(interp, zf);
  Tcl_Free((char *) zf);
  unmounted = 1;
done:
  Unlock();
  Tcl_DStringFree(&ds);
  if (unmounted) {
    Tcl_FSMountsChanged(NULL);
  }
  return ret;
}

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSMountObjCmd --
 *







<
|
|
|
<
|

<
<
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|

|
|

>
>

<
<
|
|
|

|
<
|
|
|
|







1329
1330
1331
1332
1333
1334
1335

1336
1337
1338

1339
1340


1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370


1371
1372
1373
1374
1375

1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
 *
 *-------------------------------------------------------------------------
 */

int
TclZipfs_Unmount(Tcl_Interp *interp, const char *zipname)
{

    ZipFile *zf;
    ZipEntry *z, *znext;
    Tcl_HashEntry *hPtr;

    int ret = TCL_OK, unmounted = 0;



    WriteLock();
    if (!ZipFS.initialized) {
	goto done;
    }
    hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, zipname);
    if (hPtr == NULL) {
	/* don't report error */
	goto done;
    }
    zf = (ZipFile *) Tcl_GetHashValue(hPtr);
    if (zf->nopen > 0) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
		    Tcl_NewStringObj("filesystem is busy", -1));
	}
	ret = TCL_ERROR;
	goto done;
    }
    Tcl_DeleteHashEntry(hPtr);
    for (z = zf->entries; z; z = znext) {
	znext = z->next;
	hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, z->name);
	if (hPtr) {
	    Tcl_DeleteHashEntry(hPtr);
	}
	if (z->data != NULL) {
	    Tcl_Free((char *) z->data);
	}
	Tcl_Free((char *) z);
    }


    ZipFSCloseArchive(interp, zf);
    Tcl_Free((char *) zf);
    unmounted = 1;
done:
    Unlock();

    if (unmounted) {
	Tcl_FSMountsChanged(NULL);
    }
    return ret;
}

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSMountObjCmd --
 *
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
 *-------------------------------------------------------------------------
 */

static int
ZipFSCanonicalObjCmd(ClientData clientData, Tcl_Interp *interp,
		  int objc, Tcl_Obj *const objv[])
{
  char *mntpoint=NULL;
  char *filename=NULL;
  char *result;
  Tcl_DString dPath;

  if (objc != 2 && objc != 3 && objc!=4) {
    Tcl_WrongNumArgs(interp, 1, objv, "?mntpnt? filename ?ZIPFS?");
    return TCL_ERROR;
  }
  Tcl_DStringInit(&dPath);
  if(objc==2) {
    filename = Tcl_GetString(objv[1]);
    result=CanonicalPath("",filename,&dPath,1);
  } else if (objc==3) {
    mntpoint = Tcl_GetString(objv[1]);
    filename = Tcl_GetString(objv[2]);
    result=CanonicalPath(mntpoint,filename,&dPath,1);
  } else {
    int zipfs=0;
    if(Tcl_GetBooleanFromObj(interp,objv[3],&zipfs)) {
      return TCL_ERROR;
    }
    mntpoint = Tcl_GetString(objv[1]);
    filename = Tcl_GetString(objv[2]);
    result=CanonicalPath(mntpoint,filename,&dPath,zipfs);
  }
  Tcl_SetObjResult(interp,Tcl_NewStringObj(result,-1));
  return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSExistsObjCmd --
 *







|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
 *-------------------------------------------------------------------------
 */

static int
ZipFSCanonicalObjCmd(ClientData clientData, Tcl_Interp *interp,
		  int objc, Tcl_Obj *const objv[])
{
    char *mntpoint=NULL;
    char *filename=NULL;
    char *result;
    Tcl_DString dPath;

    if (objc != 2 && objc != 3 && objc!=4) {
	Tcl_WrongNumArgs(interp, 1, objv, "?mntpnt? filename ?ZIPFS?");
	return TCL_ERROR;
    }
    Tcl_DStringInit(&dPath);
    if(objc==2) {
	filename = Tcl_GetString(objv[1]);
	result=CanonicalPath("",filename,&dPath,1);
    } else if (objc==3) {
	mntpoint = Tcl_GetString(objv[1]);
	filename = Tcl_GetString(objv[2]);
	result=CanonicalPath(mntpoint,filename,&dPath,1);
    } else {
	int zipfs=0;
	if(Tcl_GetBooleanFromObj(interp,objv[3],&zipfs)) {
	    return TCL_ERROR;
	}
	mntpoint = Tcl_GetString(objv[1]);
	filename = Tcl_GetString(objv[2]);
	result=CanonicalPath(mntpoint,filename,&dPath,zipfs);
    }
    Tcl_SetObjResult(interp,Tcl_NewStringObj(result,-1));
    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * ZipFSExistsObjCmd --
 *
2300
2301
2302
2303
2304
2305
2306

2307
2308
2309
2310
2311


2312





2313
2314
2315

2316
2317
2318
2319
2320
2321
2322

static int
ZipFSExistsObjCmd(ClientData clientData, Tcl_Interp *interp,
		  int objc, Tcl_Obj *const objv[])
{
    char *filename;
    int exists;


    if (objc != 2) {
      Tcl_WrongNumArgs(interp, 1, objv, "filename");
      return TCL_ERROR;
    }


    filename = Tcl_GetStringFromObj(objv[1], 0);





    ReadLock();
    exists = ZipFSLookup(filename) != NULL;
    Unlock();

    Tcl_SetObjResult(interp,Tcl_NewBooleanObj(exists));
    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *







>





>
>

>
>
>
>
>



>







2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261

static int
ZipFSExistsObjCmd(ClientData clientData, Tcl_Interp *interp,
		  int objc, Tcl_Obj *const objv[])
{
    char *filename;
    int exists;
    Tcl_DString ds;

    if (objc != 2) {
      Tcl_WrongNumArgs(interp, 1, objv, "filename");
      return TCL_ERROR;
    }

    /* prepend ZIPFS_VOLUME to filename, eliding the final / */
    filename = Tcl_GetStringFromObj(objv[1], 0);
    Tcl_DStringInit(&ds);
    Tcl_DStringAppend(&ds, ZIPFS_VOLUME, ZIPFS_VOLUME_LEN-1);
    Tcl_DStringAppend(&ds, filename, -1);
    filename = Tcl_DStringValue(&ds);

    ReadLock();
    exists = ZipFSLookup(filename) != NULL;
    Unlock();

    Tcl_SetObjResult(interp,Tcl_NewBooleanObj(exists));
    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
2771
2772
2773
2774
2775
2776
2777

2778
2779
2780
2781
2782
2783
2784
	return NULL;
    }
    WriteLock();
    z = ZipFSLookup(filename);
    if (z == NULL) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj("file not found", -1));

	}
	goto error;
    }
    trunc = (mode & O_TRUNC) != 0;
    wr = (mode & (O_WRONLY | O_RDWR)) != 0;
    if ((z->cmeth != ZIP_COMPMETH_STORED) &&
	(z->cmeth != ZIP_COMPMETH_DEFLATED)) {







>







2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
	return NULL;
    }
    WriteLock();
    z = ZipFSLookup(filename);
    if (z == NULL) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj("file not found", -1));
	    Tcl_AppendResult(interp, " \"", filename, "\"", NULL);
	}
	goto error;
    }
    trunc = (mode & O_TRUNC) != 0;
    wr = (mode & (O_WRONLY | O_RDWR)) != 0;
    if ((z->cmeth != ZIP_COMPMETH_STORED) &&
	(z->cmeth != ZIP_COMPMETH_DEFLATED)) {
3152
3153
3154
3155
3156
3157
3158


3159
3160
3161
3162
3163
3164
3165

static Tcl_Channel
Zip_FSOpenFileChannelProc(Tcl_Interp *interp, Tcl_Obj *pathPtr,
			  int mode, int permissions)
{
    int len;



    return ZipChannelOpen(interp, Tcl_GetStringFromObj(pathPtr, &len),
			  mode, permissions);
}

/*
 *-------------------------------------------------------------------------
 *







>
>







3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107

static Tcl_Channel
Zip_FSOpenFileChannelProc(Tcl_Interp *interp, Tcl_Obj *pathPtr,
			  int mode, int permissions)
{
    int len;

    if (!(pathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr))) return NULL;

    return ZipChannelOpen(interp, Tcl_GetStringFromObj(pathPtr, &len),
			  mode, permissions);
}

/*
 *-------------------------------------------------------------------------
 *
3178
3179
3180
3181
3182
3183
3184


3185
3186
3187
3188
3189
3190
3191
 */

static int
Zip_FSStatProc(Tcl_Obj *pathPtr, Tcl_StatBuf *buf)
{
    int len;



    return ZipEntryStat(Tcl_GetStringFromObj(pathPtr, &len), buf);
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSAccessProc --







>
>







3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
 */

static int
Zip_FSStatProc(Tcl_Obj *pathPtr, Tcl_StatBuf *buf)
{
    int len;

    if (!(pathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr))) return -1;

    return ZipEntryStat(Tcl_GetStringFromObj(pathPtr, &len), buf);
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSAccessProc --
3202
3203
3204
3205
3206
3207
3208


3209
3210
3211
3212
3213
3214
3215
 *-------------------------------------------------------------------------
 */

static int
Zip_FSAccessProc(Tcl_Obj *pathPtr, int mode)
{
    int len;



    return ZipEntryAccess(Tcl_GetStringFromObj(pathPtr, &len), mode);
}

/*
 *-------------------------------------------------------------------------
 *







>
>







3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
 *-------------------------------------------------------------------------
 */

static int
Zip_FSAccessProc(Tcl_Obj *pathPtr, int mode)
{
    int len;

    if (!(pathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr))) return -1;

    return ZipEntryAccess(Tcl_GetStringFromObj(pathPtr, &len), mode);
}

/*
 *-------------------------------------------------------------------------
 *
3254
3255
3256
3257
3258
3259
3260
3261
3262

3263
3264
3265



3266
3267
3268
3269
3270

3271





3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
 *-------------------------------------------------------------------------
 */
static int
Zip_FSMatchInDirectoryProc(Tcl_Interp* interp, Tcl_Obj *result,
			   Tcl_Obj *pathPtr, const char *pattern,
			   Tcl_GlobTypeData *types)
{
  Tcl_HashEntry *hPtr;
  Tcl_HashSearch search;

  int scnt, len, l, dirOnly = -1, prefixLen, strip = 0;
  char *pat, *prefix, *path;
  Tcl_DString ds, dsPref;



  if (types != NULL) {
    dirOnly = (types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR;
  }
  Tcl_DStringInit(&ds);
  Tcl_DStringInit(&dsPref);

  prefix = Tcl_GetStringFromObj(pathPtr, &prefixLen);





  Tcl_DStringAppend(&dsPref, prefix, prefixLen);
  prefix = Tcl_DStringValue(&dsPref);
  path = AbsolutePath(prefix, &ds, 1);
  len = Tcl_DStringLength(&ds);
  if (strcmp(prefix, path) == 0) {
    prefix = NULL;
  } else {
    strip = len + 1;
  }
  if (prefix != NULL) {
    Tcl_DStringAppend(&dsPref, "/", 1);
    prefixLen++;
    prefix = Tcl_DStringValue(&dsPref);
  }
  ReadLock();
  if ((types != NULL) && (types->type == TCL_GLOB_TYPE_MOUNT)) {
    l = CountSlashes(path);
    if (path[len - 1] == '/') {
	    len--;
    } else {
	    l++;
    }
    if ((pattern == NULL) || (pattern[0] == '\0')) {
	    pattern = "*";
    }
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
    while (hPtr != NULL) {
	    ZipFile *zf = (ZipFile *) Tcl_GetHashValue(hPtr);

	    if (zf->mntptlen == 0) {
        ZipEntry *z = zf->topents;
        while (z != NULL) {
          int lenz = strlen(z->name);
          if ((lenz > len + 1) &&
              (strncmp(z->name, path, len) == 0) &&
              (z->name[len] == '/') &&
              (CountSlashes(z->name) == l) &&
              Tcl_StringCaseMatch(z->name + len + 1, pattern, 0)) {
            if (prefix != NULL) {
              Tcl_DStringAppend(&dsPref, z->name, lenz);
              Tcl_ListObjAppendElement(NULL, result,
            	Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
        	    Tcl_DStringLength(&dsPref)));
              Tcl_DStringSetLength(&dsPref, prefixLen);
            } else {
              Tcl_ListObjAppendElement(NULL, result,
            	Tcl_NewStringObj(z->name, lenz));
            }
          }
          z = z->tnext;
        }
	    } else if ((zf->mntptlen > len + 1) &&
		       (strncmp(zf->mntpt, path, len) == 0) &&
		       (zf->mntpt[len] == '/') &&
		       (CountSlashes(zf->mntpt) == l) &&
		       Tcl_StringCaseMatch(zf->mntpt + len + 1, pattern, 0)) {
        if (prefix != NULL) {
          Tcl_DStringAppend(&dsPref, zf->mntpt, zf->mntptlen);
          Tcl_ListObjAppendElement(NULL, result,
        	Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
			    Tcl_DStringLength(&dsPref)));
          Tcl_DStringSetLength(&dsPref, prefixLen);
        } else {
          Tcl_ListObjAppendElement(NULL, result,
        	Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
        }
	    }
	    hPtr = Tcl_NextHashEntry(&search);
    }
    goto end;
  }
  if ((pattern == NULL) || (pattern[0] == '\0')) {
    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, path);
    if (hPtr != NULL) {
	    ZipEntry *z = (ZipEntry *) Tcl_GetHashValue(hPtr);

	    if ((dirOnly < 0) ||
        	(!dirOnly && !z->isdir) ||
        	(dirOnly && z->isdir)) {
      	if (prefix != NULL) {
    	    Tcl_DStringAppend(&dsPref, z->name, -1);
    	    Tcl_ListObjAppendElement(NULL, result,
      		Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
			    Tcl_DStringLength(&dsPref)));
    	    Tcl_DStringSetLength(&dsPref, prefixLen);
      	} else {
    	    Tcl_ListObjAppendElement(NULL, result,
      		Tcl_NewStringObj(z->name, -1));
        }
	    }
    }
    goto end;
  }
  l = strlen(pattern);
  pat = Tcl_Alloc(len + l + 2);
  memcpy(pat, path, len);
  while ((len > 1) && (pat[len - 1] == '/')) {
    --len;
  }
  if ((len > 1) || (pat[0] != '/')) {
    pat[len] = '/';
    ++len;
  }
  memcpy(pat + len, pattern, l + 1);
  scnt = CountSlashes(pat);
  for (hPtr = Tcl_FirstHashEntry(&ZipFS.fileHash, &search);
      hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
    ZipEntry *z = (ZipEntry *) Tcl_GetHashValue(hPtr);
    if ((dirOnly >= 0) &&
  	    ((dirOnly && !z->isdir) || (!dirOnly && z->isdir))) {
	    continue;
    }
    if ((z->depth == scnt) && Tcl_StringCaseMatch(z->name, pat, 0)) {
	    if (prefix != NULL) {
        Tcl_DStringAppend(&dsPref, z->name + strip, -1);
        Tcl_ListObjAppendElement(NULL, result,
		    Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
      	Tcl_DStringLength(&dsPref)));
        Tcl_DStringSetLength(&dsPref, prefixLen);
	    } else {
        Tcl_ListObjAppendElement(NULL, result,
		    Tcl_NewStringObj(z->name + strip, -1));
	    }
    }
  }
  Tcl_Free(pat);
end:
  Unlock();
  Tcl_DStringFree(&dsPref);
  Tcl_DStringFree(&ds);
  return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSPathInFilesystemProc --
 *







|
|
>
|
|
|
>
>
>
|
|
|
|
<
>
|
>
>
>
>
>
|
|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|

|
|

|
|
|



|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|


|
|
|
|
|
|



|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|

|
|
|
|
|

|
|

|
|
|

|
|
<
|







3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219

3220
3221
3222
3223
3224
3225
3226
3227
3228


3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353

3354
3355
3356
3357
3358
3359
3360
3361
 *-------------------------------------------------------------------------
 */
static int
Zip_FSMatchInDirectoryProc(Tcl_Interp* interp, Tcl_Obj *result,
			   Tcl_Obj *pathPtr, const char *pattern,
			   Tcl_GlobTypeData *types)
{
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch search;
    Tcl_Obj *normPathPtr;
    int scnt, len, l, dirOnly = -1, prefixLen, strip = 0;
    char *pat, *prefix, *path;
    Tcl_DString dsPref;

    if (!(normPathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr))) return -1;

    if (types != NULL) {
	dirOnly = (types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR;
    }


    /* the prefix that gets prepended to results */
    prefix = Tcl_GetStringFromObj(pathPtr, &prefixLen);

    /* the (normalized) path we're searching */
    path = Tcl_GetStringFromObj(normPathPtr, &len);

    Tcl_DStringInit(&dsPref);
    Tcl_DStringAppend(&dsPref, prefix, prefixLen);



    if (strcmp(prefix, path) == 0) {
	prefix = NULL;
    } else {
	strip = len + 1;
    }
    if (prefix != NULL) {
	Tcl_DStringAppend(&dsPref, "/", 1);
	prefixLen++;
	prefix = Tcl_DStringValue(&dsPref);
    }
    ReadLock();
    if ((types != NULL) && (types->type == TCL_GLOB_TYPE_MOUNT)) {
	l = CountSlashes(path);
	if (path[len - 1] == '/') {
	    len--;
	} else {
	    l++;
	}
	if ((pattern == NULL) || (pattern[0] == '\0')) {
	    pattern = "*";
	}
	hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
	while (hPtr != NULL) {
	    ZipFile *zf = (ZipFile *) Tcl_GetHashValue(hPtr);

	    if (zf->mntptlen == 0) {
		ZipEntry *z = zf->topents;
		while (z != NULL) {
		    int lenz = strlen(z->name);
		    if ((lenz > len + 1) &&
			    (strncmp(z->name, path, len) == 0) &&
			    (z->name[len] == '/') &&
			    (CountSlashes(z->name) == l) &&
			    Tcl_StringCaseMatch(z->name + len + 1, pattern, 0)) {
			if (prefix != NULL) {
			    Tcl_DStringAppend(&dsPref, z->name, lenz);
			    Tcl_ListObjAppendElement(NULL, result,
				    Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
					Tcl_DStringLength(&dsPref)));
			    Tcl_DStringSetLength(&dsPref, prefixLen);
			} else {
			    Tcl_ListObjAppendElement(NULL, result,
				    Tcl_NewStringObj(z->name, lenz));
			}
		    }
		    z = z->tnext;
		}
	    } else if ((zf->mntptlen > len + 1) &&
		    (strncmp(zf->mntpt, path, len) == 0) &&
		    (zf->mntpt[len] == '/') &&
		    (CountSlashes(zf->mntpt) == l) &&
		    Tcl_StringCaseMatch(zf->mntpt + len + 1, pattern, 0)) {
		if (prefix != NULL) {
		    Tcl_DStringAppend(&dsPref, zf->mntpt, zf->mntptlen);
		    Tcl_ListObjAppendElement(NULL, result,
			    Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
				Tcl_DStringLength(&dsPref)));
		    Tcl_DStringSetLength(&dsPref, prefixLen);
		} else {
		    Tcl_ListObjAppendElement(NULL, result,
			    Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
		}
	    }
	    hPtr = Tcl_NextHashEntry(&search);
	}
	goto end;
    }
    if ((pattern == NULL) || (pattern[0] == '\0')) {
	hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, path);
	if (hPtr != NULL) {
	    ZipEntry *z = (ZipEntry *) Tcl_GetHashValue(hPtr);

	    if ((dirOnly < 0) ||
		    (!dirOnly && !z->isdir) ||
		    (dirOnly && z->isdir)) {
		if (prefix != NULL) {
		    Tcl_DStringAppend(&dsPref, z->name, -1);
		    Tcl_ListObjAppendElement(NULL, result,
			    Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
				Tcl_DStringLength(&dsPref)));
		    Tcl_DStringSetLength(&dsPref, prefixLen);
		} else {
		    Tcl_ListObjAppendElement(NULL, result,
			    Tcl_NewStringObj(z->name, -1));
		}
	    }
	}
	goto end;
    }
    l = strlen(pattern);
    pat = Tcl_Alloc(len + l + 2);
    memcpy(pat, path, len);
    while ((len > 1) && (pat[len - 1] == '/')) {
	--len;
    }
    if ((len > 1) || (pat[0] != '/')) {
	pat[len] = '/';
	++len;
    }
    memcpy(pat + len, pattern, l + 1);
    scnt = CountSlashes(pat);
    for (hPtr = Tcl_FirstHashEntry(&ZipFS.fileHash, &search);
	    hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
	ZipEntry *z = (ZipEntry *) Tcl_GetHashValue(hPtr);
	if ((dirOnly >= 0) &&
		((dirOnly && !z->isdir) || (!dirOnly && z->isdir))) {
	    continue;
	}
	if ((z->depth == scnt) && Tcl_StringCaseMatch(z->name, pat, 0)) {
	    if (prefix != NULL) {
		Tcl_DStringAppend(&dsPref, z->name + strip, -1);
		Tcl_ListObjAppendElement(NULL, result,
			Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
			    Tcl_DStringLength(&dsPref)));
		Tcl_DStringSetLength(&dsPref, prefixLen);
	    } else {
		Tcl_ListObjAppendElement(NULL, result,
			Tcl_NewStringObj(z->name + strip, -1));
	    }
	}
    }
    Tcl_Free(pat);
end:
    Unlock();
    Tcl_DStringFree(&dsPref);

    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSPathInFilesystemProc --
 *
3423
3424
3425
3426
3427
3428
3429
3430

3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
Zip_FSPathInFilesystemProc(Tcl_Obj *pathPtr, ClientData *clientDataPtr)
{
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch search;
    ZipFile *zf;
    int ret = -1, len;
    char *path;
    Tcl_DString ds;


    path = Tcl_GetStringFromObj(pathPtr, &len);
    if(strncmp(path,ZIPFS_VOLUME,ZIPFS_VOLUME_LEN)!=0) {
      return -1;
    }

    Tcl_DStringInit(&ds);
    path = CanonicalPath("",path, &ds, 1);
    len = Tcl_DStringLength(&ds);
    ReadLock();
    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, path);
    if (hPtr != NULL) {
      ret = TCL_OK;
      goto endloop;
    }
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);







|
>






<
|
|







3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389

3390
3391
3392
3393
3394
3395
3396
3397
3398
Zip_FSPathInFilesystemProc(Tcl_Obj *pathPtr, ClientData *clientDataPtr)
{
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch search;
    ZipFile *zf;
    int ret = -1, len;
    char *path;

    if (!(pathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr))) return -1;

    path = Tcl_GetStringFromObj(pathPtr, &len);
    if(strncmp(path,ZIPFS_VOLUME,ZIPFS_VOLUME_LEN)!=0) {
      return -1;
    }


    len = strlen(path);

    ReadLock();
    hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, path);
    if (hPtr != NULL) {
      ret = TCL_OK;
      goto endloop;
    }
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
        ret = TCL_OK;
        goto endloop;
      }
      hPtr = Tcl_NextHashEntry(&search);
    }
endloop:
    Unlock();
    Tcl_DStringFree(&ds);
    return ret;
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSListVolumesProc --
 *
 *	Lists the currently mounted ZIP filesystem volumes.
 *
 * Results:
 *	The list of volumes.
 *
 * Side effects:
 *	None
 *
 *-------------------------------------------------------------------------
 */
static Tcl_Obj *
Zip_FSListVolumesProc(void) {
  return Tcl_NewStringObj(ZIPFS_VOLUME, -1);
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSFileAttrStringsProc --
 *







<




















|







3415
3416
3417
3418
3419
3420
3421

3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
        ret = TCL_OK;
        goto endloop;
      }
      hPtr = Tcl_NextHashEntry(&search);
    }
endloop:
    Unlock();

    return ret;
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSListVolumesProc --
 *
 *	Lists the currently mounted ZIP filesystem volumes.
 *
 * Results:
 *	The list of volumes.
 *
 * Side effects:
 *	None
 *
 *-------------------------------------------------------------------------
 */
static Tcl_Obj *
Zip_FSListVolumesProc(void) {
    return Tcl_NewStringObj(ZIPFS_VOLUME, -1);
}

/*
 *-------------------------------------------------------------------------
 *
 * Zip_FSFileAttrStringsProc --
 *
3547
3548
3549
3550
3551
3552
3553


3554
3555
3556
3557
3558
3559
3560
static int
Zip_FSFileAttrsGetProc(Tcl_Interp *interp, int index, Tcl_Obj *pathPtr,
		       Tcl_Obj **objPtrRef)
{
    int len, ret = TCL_OK;
    char *path;
    ZipEntry *z;



    path = Tcl_GetStringFromObj(pathPtr, &len);
    ReadLock();
    z = ZipFSLookup(path);
    if (z == NULL) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj("file not found", -1));







>
>







3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
static int
Zip_FSFileAttrsGetProc(Tcl_Interp *interp, int index, Tcl_Obj *pathPtr,
		       Tcl_Obj **objPtrRef)
{
    int len, ret = TCL_OK;
    char *path;
    ZipEntry *z;

    if (!(pathPtr = Tcl_FSGetNormalizedPath(NULL, pathPtr))) return -1;

    path = Tcl_GetStringFromObj(pathPtr, &len);
    ReadLock();
    z = ZipFSLookup(path);
    if (z == NULL) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj("file not found", -1));

Changes to tests/zipfs.test.

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28













































29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

104



105




106

107
108
109
110
111
112
113
114
115
116
117
118
    package require tcltest 2.1
    namespace import -force ::tcltest::*
}

testConstraint zipfs [expr {[llength [info commands zlib]] && [regexp tcltest [info nameofexecutable]]}]

# Removed in tip430 - zipfs is no longer a static package
#test zipfs-1.1 {zipfs basics} -constraints zipfs -body {
#    load {} zipfs
#} -result {}

test zipfs-1.2 {zipfs basics} -constraints zipfs -body {
    package require zipfs
} -result {1.0}














































test zipfs-1.3 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs mount a b c d e f
} -result {wrong # args: should be "zipfs mount ?zipfile? ?mountpoint? ?password?"}

test zipfs-1.4 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs unmount a b c d e f
} -result {wrong # args: should be "zipfs unmount zipfile"}

test zipfs-1.5 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs mkkey a b c d e f
} -result {wrong # args: should be "zipfs mkkey password"}

test zipfs-1.6 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs mkimg a b c d e f
} -result {wrong # args: should be "zipfs mkimg outfile indir ?strip? ?password? ?infile?"}

test zipfs-1.7 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs mkzip a b c d e f
} -result {wrong # args: should be "zipfs mkzip outfile indir ?strip? ?password?"}

test zipfs-1.8 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs exists a b c d e f
} -result {wrong # args: should be "zipfs exists filename"}

test zipfs-1.9 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs info a b c d e f
} -result {wrong # args: should be "zipfs info filename"}

test zipfs-1.10 {zipfs basics} -constraints zipfs -returnCodes error -body {
    zipfs list a b c d e f
} -result {wrong # args: should be "zipfs list ?(-glob|-regexp)? ?pattern?"}

set ntcl_library [file normalize $tcl_library]

test zipfs-2.1 {zipfs mkzip empty archive} -constraints zipfs -returnCodes error -body {
    zipfs mkzip abc.zip $ntcl_library/xxxx
} -result {empty archive}

test zipfs-2.2 {zipfs mkzip} -constraints zipfs -body {
    set pwd [pwd]
    cd $ntcl_library/encoding
    zipfs mkzip abc.zip .
    zipfs mount abc.zip zipfs:/abc
    zipfs list -glob zipfs:/abc/cp850.*
} -cleanup {
    cd $pwd
} -result {zipfs:/abc/cp850.enc}

test zipfs-2.3 {zipfs unmount} -constraints zipfs -body {
    zipfs info zipfs:/abc/cp850.enc
} -result [list $ntcl_library/encoding/abc.zip 1090 527 39434]

test zipfs-2.4 {zipfs unmount} -constraints zipfs -body {
    set f [open zipfs:/abc/cp850.enc]
    read $f
} -result {# Encoding file: cp850, single-byte
S
003F 0 1
00
0000000100020003000400050006000700080009000A000B000C000D000E000F
0010001100120013001400150016001700180019001A001B001C001D001E001F
0020002100220023002400250026002700280029002A002B002C002D002E002F
0030003100320033003400350036003700380039003A003B003C003D003E003F
0040004100420043004400450046004700480049004A004B004C004D004E004F
0050005100520053005400550056005700580059005A005B005C005D005E005F
0060006100620063006400650066006700680069006A006B006C006D006E006F
0070007100720073007400750076007700780079007A007B007C007D007E007F
00C700FC00E900E200E400E000E500E700EA00EB00E800EF00EE00EC00C400C5
00C900E600C600F400F600F200FB00F900FF00D600DC00F800A300D800D70192
00E100ED00F300FA00F100D100AA00BA00BF00AE00AC00BD00BC00A100AB00BB
2591259225932502252400C100C200C000A9256325512557255D00A200A52510
25142534252C251C2500253C00E300C3255A25542569256625602550256C00A4
00F000D000CA00CB00C8013100CD00CE00CF2518250C2588258400A600CC2580
00D300DF00D400D200F500D500B500FE00DE00DA00DB00D900FD00DD00AF00B4
00AD00B1201700BE00B600A700F700B800B000A800B700B900B300B225A000A0

}








test zipfs-2.5 {zipfs exists} -constraints zipfs -body {

    zipfs unmount abc.zip
    zipfs exists /abc/cp850.enc
} -cleanup {
    file delete abc.zip
} -result 1

::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End:







|



|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|



|



|



|



|



|



|



|



<
<

|




|
|
|
|


|

|
|
|

|
|
|




















>
|
>
>
>

>
>
>
>
|
>
|


|
|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    package require tcltest 2.1
    namespace import -force ::tcltest::*
}

testConstraint zipfs [expr {[llength [info commands zlib]] && [regexp tcltest [info nameofexecutable]]}]

# Removed in tip430 - zipfs is no longer a static package
#test zipfs-0.0 {zipfs basics} -constraints zipfs -body {
#    load {} zipfs
#} -result {}

test zipfs-0.1 {zipfs basics} -constraints zipfs -body {
    package require zipfs
} -result {1.0}

test zipfs-0.1 {zipfs basics} -constraints zipfs -body {
    expr {"//zipfs:/" in [file volumes]}
} -result 1

test zipfs-0.2 {zipfs basics} -constraints zipfs -body {
    string match //zipfs:/* $tcl_library
} -result 1

test zipfs-0.3 {zipfs basics: glob} -constraints zipfs -body {
    set pwd [pwd]
    cd $tcl_library
    lsort [glob -dir . http*]
} -cleanup {
    cd $pwd
} -result {./http ./http1.0}

test zipfs-0.4 {zipfs basics: glob} -constraints zipfs -body {
    set pwd [pwd]
    cd $tcl_library
    lsort [glob -dir [pwd] http*]
} -cleanup {
    cd $pwd
} -result [list $tcl_library/http $tcl_library/http1.0]

test zipfs-0.5 {zipfs basics: glob} -constraints zipfs -body {
    lsort [glob -dir $tcl_library http*]
} -result [list $tcl_library/http $tcl_library/http1.0]

test zipfs-0.6 {zipfs basics: glob} -constraints zipfs -body {
    lsort [glob $tcl_library/http*]
} -result [list $tcl_library/http $tcl_library/http1.0]

test zipfs-0.7 {zipfs basics: glob} -constraints zipfs -body {
    lsort [glob -tails -dir $tcl_library http*]
} -result {http http1.0}

test zipfs-0.8 {zipfs basics: glob} -constraints zipfs -body {
    lsort [glob -nocomplain -tails -types d -dir $tcl_library http*]
} -result {http http1.0}

test zipfs-0.9 {zipfs basics: glob} -constraints zipfs -body {
    lsort [glob -nocomplain -tails -types f -dir $tcl_library http*]
} -result {}


test zipfs-1.3 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs mount a b c d e f
} -result {wrong # args: should be "zipfs mount ?zipfile? ?mountpoint? ?password?"}

test zipfs-1.4 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs unmount a b c d e f
} -result {wrong # args: should be "zipfs unmount zipfile"}

test zipfs-1.5 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs mkkey a b c d e f
} -result {wrong # args: should be "zipfs mkkey password"}

test zipfs-1.6 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs mkimg a b c d e f
} -result {wrong # args: should be "zipfs mkimg outfile indir ?strip? ?password? ?infile?"}

test zipfs-1.7 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs mkzip a b c d e f
} -result {wrong # args: should be "zipfs mkzip outfile indir ?strip? ?password?"}

test zipfs-1.8 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs exists a b c d e f
} -result {wrong # args: should be "zipfs exists filename"}

test zipfs-1.9 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs info a b c d e f
} -result {wrong # args: should be "zipfs info filename"}

test zipfs-1.10 {zipfs errors} -constraints zipfs -returnCodes error -body {
    zipfs list a b c d e f
} -result {wrong # args: should be "zipfs list ?(-glob|-regexp)? ?pattern?"}



test zipfs-2.1 {zipfs mkzip empty archive} -constraints zipfs -returnCodes error -body {
    zipfs mkzip /tmp/abc.zip $tcl_library/xxxx      ;# FIXME: test independence
} -result {empty archive}

test zipfs-2.2 {zipfs mkzip} -constraints zipfs -body {
    set pwd [pwd]
    cd $tcl_library/encoding
    zipfs mkzip /tmp/abc.zip .
    zipfs mount /tmp/abc.zip //zipfs:/abc       ;# FIXME: test independence
    zipfs list -glob //zipfs:/abc/cp850.*
} -cleanup {
    cd $pwd
} -result {//zipfs:/abc/cp850.enc}

test zipfs-2.3 {zipfs info} -constraints zipfs -body {
    zipfs info //zipfs:/abc/cp850.enc
} -result [list /tmp/abc.zip 1090 527 39318] ;# FIXME: result depends on content of encodings dir

test zipfs-2.4 {zipfs data} -constraints zipfs -body {
    set zipfd [open //zipfs:/abc/cp850.enc]	;# FIXME: leave open - see later test
    read $zipfd
} -result {# Encoding file: cp850, single-byte
S
003F 0 1
00
0000000100020003000400050006000700080009000A000B000C000D000E000F
0010001100120013001400150016001700180019001A001B001C001D001E001F
0020002100220023002400250026002700280029002A002B002C002D002E002F
0030003100320033003400350036003700380039003A003B003C003D003E003F
0040004100420043004400450046004700480049004A004B004C004D004E004F
0050005100520053005400550056005700580059005A005B005C005D005E005F
0060006100620063006400650066006700680069006A006B006C006D006E006F
0070007100720073007400750076007700780079007A007B007C007D007E007F
00C700FC00E900E200E400E000E500E700EA00EB00E800EF00EE00EC00C400C5
00C900E600C600F400F600F200FB00F900FF00D600DC00F800A300D800D70192
00E100ED00F300FA00F100D100AA00BA00BF00AE00AC00BD00BC00A100AB00BB
2591259225932502252400C100C200C000A9256325512557255D00A200A52510
25142534252C251C2500253C00E300C3255A25542569256625602550256C00A4
00F000D000CA00CB00C8013100CD00CE00CF2518250C2588258400A600CC2580
00D300DF00D400D200F500D500B500FE00DE00DA00DB00D900FD00DD00AF00B4
00AD00B1201700BE00B600A700F700B800B000A800B700B900B300B225A000A0
} ;# FIXME: result depends on content of encodings dir

test zipfs-2.5 {zipfs exists} -constraints zipfs -body {
    zipfs exists /abc/cp850.enc
} -result 1

test zipfs-2.6 {zipfs unmount while busy} -constraints zipfs -body {
    zipfs unmount /tmp/abc.zip
} -returnCodes error -result {filesystem is busy}

test zipfs-2.7 {zipfs unmount} -constraints zipfs -body {
    close $zipfd
    zipfs unmount /tmp/abc.zip
    zipfs exists /abc/cp850.enc
} -cleanup {
    file delete /tmp/abc.zip        ;# FIXME: test independence
} -result 0

::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: