summaryrefslogtreecommitdiff
path: root/zip/zip.cpp
blob: e7dc7edb245012fff1525b39ee43e971bc10df58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
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
/*  ZIP plugin for Tux Commander
 *   version 0.6.2, designed for ZipArchive v3.2.0
 *  Copyright (C) 2004-2009 Tomas Bzatek <tbzatek@users.sourceforge.net>
 *   Check for updates on tuxcmd.sourceforge.net
 *
 *  Uses ZipArchive library
 *   Copyright (C) 2000 - 2007 Artpol Software - Tadeusz Dracz
 *   http://www.artpol-software.com/ZipArchive/
 * 


 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>
#include <fnmatch.h>
#include <unistd.h>
#include <glib.h>
#include <gio/gio.h>

#include "tuxcmd-vfs.h"
#include "strutils.h"
#include "vfsutils.h"
#include "filelist.h"
#include "filelist-vfs-intf.h"

#include "ZipArchive.h"
#include "ZipPlatform.h"
#include "ZipCallback.h"



#define VERSION "0.6.2"
#define BUILD_DATE "2009-12-13"
#define DEFAULT_BLOCK_SIZE 65536


using namespace std;
extern "C" {

  

/******************************************************************************************************/
/**  Utilities                                                                                        */
/**************                                                                        ****************/                  

static void 
zip_error_to_gerror (CZipException e, GError **error)
{
  gint code;
  
  switch (e.m_iCause) {
    case CZipException::noError:             //  No error. 
      code = G_IO_ERROR_UNKNOWN;
      break;
    case CZipException::genericError:        //  An unknown error.
    case CZipException::badCrc:              //  Crc is mismatched.
    case CZipException::internalError:       //  An internal error.
    case CZipException::badPassword:         //  An incorrect password set for the file being decrypted.
    case CZipException::dirWithSize:         //  The directory with a non-zero size found while testing.
    case CZipException::streamEnd:           //  Zlib library error.
    case CZipException::needDict:            //  Zlib library error.
    case CZipException::errNo:               //  Zlib library error.
    case CZipException::streamError:         //  Zlib library error.
    case CZipException::dataError:           //  Zlib library error.
    case CZipException::memError:            //  Zlib library or CZipMemFile error.
    case CZipException::bufError:            //  Zlib library error.
      code = G_IO_ERROR_FAILED;
      break;
    case CZipException::badZipFile:          //  Damaged or not a zip file.
      code = G_IO_ERROR_NOT_MOUNTABLE_FILE;
      break;
    case CZipException::aborted:             //  The disk change callback method returned false.
    case CZipException::abortedAction:       //  The action callback method returned false.
    case CZipException::abortedSafely:       //  The action callback method returned false, but the data is not corrupted.
      code = G_IO_ERROR_CANCELLED;
      break;
    case CZipException::nonRemovable:        //  The device selected for the spanned archive is not removable.
    case CZipException::tooManyVolumes:      //  The limit of the maximum volumes reached.
    case CZipException::tooManyFiles:        //  The limit of the maximum files in an archive reached.
    case CZipException::tooLongData:         //  The filename, the comment or the local or central extra field of the file added to the archive is too long.
    case CZipException::tooBigSize:          //  The file size is too large to be supported.
      code = G_IO_ERROR_NOT_SUPPORTED;
      break;
      
    case CZipException::notRemoved:          //  Error while removing a file
    case CZipException::notRenamed:          //  Error while renaming a file (under Windows call GetLastError() to find out more).
      code = G_IO_ERROR_NOT_EMPTY;
      break;
    case CZipException::cdirNotFound:        //  The central directory was not found in the archive (or you were trying to open not the last disk of a segmented archive).
      code = G_IO_ERROR_NOT_FOUND;
      break;
//    case CZipException::cdir64NotFound: return cVFS_ReadErr;    //  The Zip64 central directory signature was not found in the archive where expected.
//    case CZipException::noBBZInZip64: return cVFS_ReadErr;      //  The number of bytes before a zip archive must be zero in the Zip64 format.
//    case CZipException::badAesAuthCode: return cVFS_ReadErr;    //  Mismatched authentication code in WinZip AEC decrypted data.
    case CZipException::platfNotSupp:        //  Cannot create a file for the specified platform.
    case CZipException::noZip64:             //  The Zip64 format has not been enabled for the library, but is required to open the archive.
    case CZipException::noAES:               //  WinZip AES encryption has not been enabled for the library, but is required to decompress the archive.
      code = G_IO_ERROR_NOT_SUPPORTED;
      break;
    case CZipException::noCallback:          //  There is no spanned archive callback object set.
    case CZipException::outOfBounds:         //  The collection is empty and the bounds do not exist.
    case CZipException::versionError:        //  Zlib library error.
      code = G_IO_ERROR_INVALID_ARGUMENT;
      break;
//    case CZipException::mutexError: return cVFS_ReadErr;        //  Locking or unlocking resources access was unsuccessful.
//    case CZipException::bzSequenceError: return cVFS_ReadErr;   //  Bzlib library error.
//    case CZipException::bzParamError: return cVFS_ReadErr;      //  Bzlib library error.
//    case CZipException::bzMemError: return cVFS_ReadErr;        //  Bzlib library error.
//    case CZipException::bzDataError: return cVFS_ReadErr;       //  Bzlib library error.
//    case CZipException::bzDataErrorMagic: return cVFS_ReadErr;  //  Bzlib library error.
//    case CZipException::bzIoError: return cVFS_ReadErr;         //  Bzlib library error.
//    case CZipException::bzUnexpectedEof: return cVFS_ReadErr;   //  Bzlib library error.
//    case CZipException::bzOutbuffFull: return cVFS_ReadErr;     //  Bzlib library error.
//    case CZipException::bzConfigError: return cVFS_ReadErr;     //  Bzlib library error.
//    case CZipException::bzInternalError: return cVFS_ReadErr;   //  Internal Bzlib library error.    	
    default:
      code = G_IO_ERROR_FAILED;
      break;
  }
  g_set_error_literal (error, G_IO_ERROR, code, (LPCTSTR)e.GetErrorDescription());
}



/******************************************************************************************************/
/**  Auxiliary classes                                                                                */
/**************                                                                        ****************/                  

struct ZIP_API CVFSZipActionCallback;

struct TVFSGlobs {
  TVFSLogFunc log_func;
  char *curr_dir;
  char *archive_path;

  gboolean need_password;

  CZipArchive *zip;
  CVFSZipActionCallback *extract_callback;

  gboolean archive_opened;
  guint32 block_size;
  gboolean archive_modified;

  struct PathTree *files;
  struct VfsFilelistData *vfs_filelist;

  TVFSAskQuestionCallback callback_ask_question;
  TVFSAskPasswordCallback callback_ask_password;
  TVFSProgressCallback callback_progress;
  void *callback_data;
};

//  Define the progress class and the class methods
struct ZIP_API CVFSZipActionCallback : public CZipActionCallback
{
        CVFSZipActionCallback()
        {
                m_uTotalToProcess = 0;
                m_uProcessed = 0;
                globs = NULL;
        }

        struct TVFSGlobs *globs;

        virtual bool Callback(ZIP_SIZE_TYPE uProgress)
        {
          fprintf (stderr, "(II) Callback called, position = %lu; m_uTotalToProcess = %lu; m_uProcessed = %lu\n", 
                          uProgress, m_uTotalToProcess, m_uProcessed);
          bool ret = true;
          try {
            if (globs && globs->callback_progress)
              ret = globs->callback_progress (m_uProcessed, NULL, globs->callback_data);
          }
          catch (...) {
            fprintf (stderr, "(EE) extract_callback: Fatal error occured when calling pCallBackProgress\n");
          }
          return ret;
        }
};




/***********************************************************************************************************************
*  Internal tree functions
********/

static void 
build_global_filelist (struct TVFSGlobs *globs)
{
  unsigned long int iCount;
  unsigned long int i;
  CZipFileHeader *fh;
  struct TVFSItem *item;
  char *s;
  
  /*  Ensure the filelist is freed */
  if (globs->vfs_filelist) 
    vfs_filelist_free (globs->vfs_filelist); 
  if (globs->files) 
    filelist_tree_free (globs->files); 
	  
  globs->files = filelist_tree_new();
  globs->vfs_filelist = vfs_filelist_new (globs->files);

  iCount = globs->zip->GetCount();
  /*  list files in the archive  */
  for (i = 0; i < iCount; i++) {
    fh = globs->zip->GetFileInfo (i);
    if (fh != NULL)  
      printf("  No: %lu, '%s', IsDir: %i, Size: %lu, SystemAttr = 0x%lX, OriginalAttr = 0x%lX, encrypted = %d\n", 
    	     i, (LPCTSTR)fh->GetFileName(), fh->IsDirectory(), fh->m_uUncomprSize, fh->GetSystemAttr(), fh->GetOriginalAttributes(), fh->IsEncrypted());
  }    
  printf("\n\n");
    
  for (i = 0; i < iCount; i++) {
    fh = globs->zip->GetFileInfo (i);
    if (fh != NULL) { 
      /*  Create a TVFSItem entry and fill all info  */
      item = (struct TVFSItem *) g_malloc0 (sizeof (struct TVFSItem));

      item->iSize = (guint64) fh->m_uUncomprSize;
      item->iPackedSize = (guint64) fh->m_uComprSize;
      item->inode_no = (guint64) (i + 1);
      if (fh->IsDirectory ())
        item->ItemType = vDirectory;
      else 
        item->ItemType = vRegular;
      item->iMode = fh->GetSystemAttr ();
      item->iUID = geteuid ();
      item->iGID = getegid ();
      item->m_time = (__time_t) fh->GetTime ();
      item->c_time = item->m_time;
      item->a_time = item->m_time;
      
      if (fh->IsEncrypted ())
        globs->need_password = TRUE;
     
      /*  Add item to the global list and continue with next file  */     
      s = g_filename_display_name ((LPCTSTR) fh->GetFileName ());
      filelist_tree_add_item (globs->files, s, item, (LPCTSTR) fh->GetFileName (), i + 1);
      g_free (s);
      printf ("\n");
    }
  }

  if (globs->need_password) 
    printf ("Password present.\n");

  printf ("\n\n\n\nPrinting the contents of the global filelist:\n\n");
  filelist_tree_print (globs->files);
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Basic initialization functions

struct TVFSGlobs *
VFSNew (TVFSLogFunc log_func)
{
  struct TVFSGlobs * globs;

  globs = (struct TVFSGlobs *) g_malloc0 (sizeof (struct TVFSGlobs));

  globs->archive_opened = FALSE;
  globs->block_size = DEFAULT_BLOCK_SIZE;
  globs->archive_modified = FALSE;
  globs->need_password = FALSE;

  globs->callback_data = NULL;
  globs->callback_ask_question = NULL;
  globs->callback_ask_password = NULL;
  globs->callback_progress = NULL;

  globs->log_func = log_func;
  if (globs->log_func != NULL) 
    globs->log_func ("zip plugin: VFSInit");

  return globs;
}


void
VFSSetCallbacks (struct TVFSGlobs *globs,
                 TVFSAskQuestionCallback ask_question_callback,
                 TVFSAskPasswordCallback ask_password_callback,
                 TVFSProgressCallback progress_func,
                 void *data)
{
  globs->callback_ask_question = ask_question_callback;
  globs->callback_ask_password = ask_password_callback;
  globs->callback_progress = progress_func;
  globs->callback_data = data;
}


void
VFSFree (struct TVFSGlobs *globs)
{
  if (globs->log_func != NULL) 
    globs->log_func ("zip plugin: VFSFree");
  g_free (globs);
}


int 
VFSVersion ()
{
  return cVFSVersion;
}


struct TVFSInfo *
VFSGetInfo ()
{
  struct TVFSInfo *module_info;
  
  module_info = (TVFSInfo*) g_malloc0 (sizeof (struct TVFSInfo));

  module_info->ID = g_strdup ("zip_plugin");
  module_info->Name = g_strdup ("ZIP plugin");
  module_info->About = g_strdup_printf ("version %s, build date: %s\nusing ZipArchive library v%s\n", VERSION, BUILD_DATE, CZipArchive::m_gszVersion);
  module_info->Copyright = g_strdup_printf ("Plugin Copyright (C) 2004-2009 Tomáš Bžatek\n%s", CZipArchive::m_gszCopyright);

  return module_info;
}


guint32
VFSGetCapabilities ()
{
  return VFS_CAP_CAN_CREATE_ARCHIVES;
}


char *
VFSGetArchiveExts ()
{
  return g_strdup ("zip");
}


/**************************************************************************************************************************************/
/**************************************************************************************************************************************/

gboolean 
VFSOpenArchive (struct TVFSGlobs *globs, const char *sName, GError **error)
{
  int iCount;
  
  globs->files = NULL;
  globs->vfs_filelist = NULL;
  
  globs->curr_dir = NULL;
  globs->zip = new CZipArchive;

  try {
    fprintf (stderr, "(--) VFSOpenArchive: trying to open the file...\n");

    try {
      if (! globs->zip->Open (sName, CZipArchive::zipOpen, 0)) {
        printf ("(EE) VFSOpenArchive: error opening zip archive\n");
        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error opening zip archive.");
        return FALSE;
      }
    }
    catch (...) {
      printf ("(!!) VFSOpenArchive: error opening readwrite zip, trying readonly...\n");
      try {
        /*  try to open in read only mode (required if there's no write access on the media)  */
        if (! globs->zip->Open (sName, CZipArchive::zipOpenReadOnly, 0)) {
            printf ("(EE) VFSOpenArchive: error opening readonly zip archive\n");
            g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error opening readonly zip archive.");
            return FALSE;
        }
      }
      catch (CZipException e) {
        printf ("(EE) VFSOpenArchive: error opening readonly zip\n");
        zip_error_to_gerror (e, error);
        return FALSE;
      }
    }
    iCount = globs->zip->GetCount (false);
    printf ("(II) VFSOpenArchive: %i records found, %i files.\n", iCount, globs->zip->GetCount (true));
    if (iCount < 1) {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No files found in the archive.");
      return FALSE;
    }

    /*  build global file list  */
    build_global_filelist (globs);

    /*  set progress callback  */
    globs->extract_callback = new CVFSZipActionCallback;
    globs->extract_callback->globs = globs;
    globs->zip->SetCallback (globs->extract_callback, CZipActionCallback::cbExtract);
    globs->zip->SetCallback (globs->extract_callback, CZipActionCallback::cbAdd);

    /*  set automatic flushing of changes to disk  */
    globs->zip->SetAutoFlush (true);
  }
  catch (CZipException e) {
    printf ("(EE) VFSOpenArchive: Error while processing archive %s\n%s\n", (LPCTSTR) sName, (LPCTSTR)e.GetErrorDescription());
    if (e.m_szFileName.IsEmpty()) 
      printf ("\n");
    else 
      printf ("(EE) VFSOpenArchive: Filename in error object: %s\n\n", (LPCTSTR)e.m_szFileName);
    globs->zip->Close (true);
    zip_error_to_gerror (e, error);
    return FALSE;
  }
  catch (...) {
    printf ("(EE) VFSOpenArchive: Unknown error while processing archive %s\n\n", (LPCTSTR) sName);
    globs->zip->Close (true);
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown error while processing zip archive.");
    return FALSE;
  }

  globs->archive_path = g_strdup (sName);
  globs->archive_modified = FALSE;
  return TRUE;
}


gboolean 
VFSClose (struct TVFSGlobs *globs, GError **error)
{
  if (globs) {
    /*  close the archive...  */
    fprintf (stderr, "(II) VFSClose: Closing the archive...\n");
    try {
      if (globs->archive_modified) 
        globs->zip->Flush ();
      /*   In case of inconsistency, try using afWriteDir value.
       *   (use when an exception was thrown, the Close method writes the
       *   central directory structure to the archive, so that the archive should be usable.)
       */
      globs->zip->Close (CZipArchive::afNoException, false);
    }
    catch (CZipException e) {
      fprintf (stderr, "(EE) VFSClose: Error while closing archive: %s\n", (LPCTSTR)e.GetErrorDescription());
      zip_error_to_gerror (e, error);
      return FALSE;
    }

    /*  free the ZIP objects...  */
    fprintf (stderr, "(II) VFSClose: Freeing ZipArchive objects...\n");
    try {
      delete globs->extract_callback;
      delete globs->zip;
    }
    catch (...)  {
      fprintf (stderr, "(EE) VFSClose: Error freeing ZipArchive objects\n");
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error freeing ZipArchive objects.");
      return FALSE;
    }

    /*  free the filelist  */
    fprintf (stderr, "(II) VFSClose: Freeing filelist...\n");
    if (globs->vfs_filelist) 
      vfs_filelist_free (globs->vfs_filelist);
    if (globs->files) 
      filelist_tree_free (globs->files);

    /*  free the rest...  */
    g_free (globs->archive_path);
  }
  return TRUE;
}

char *
VFSGetPath (struct TVFSGlobs *globs)
{
  return g_strdup (globs->curr_dir);
}

void
VFSGetFileSystemInfo (struct TVFSGlobs *globs, const char *APath, gint64 *FSSize, gint64 *FSFree, char **FSLabel)
{
  if (FSSize)
    *FSSize = globs->zip->GetOccupiedSpace ();
  if (FSFree)
    *FSFree = 0;
  if (FSLabel)
    *FSLabel = NULL;
}


/******************************************************************************************************/

gboolean 
VFSChangeDir (struct TVFSGlobs *globs, const char *NewPath, GError **error)
{
  char *s;
  
  s = vfs_filelist_change_dir (globs->vfs_filelist, NewPath, error);
  if (s) {
    globs->curr_dir = s;
    return TRUE;
  }
  else
    return FALSE;
}

gboolean
VFSGetPasswordRequired (struct TVFSGlobs *globs)
{
  if (globs)  
    return globs->need_password;
  return FALSE;
}

void
VFSResetPassword (struct TVFSGlobs *globs)
{
  if (globs)
    globs->zip->SetPassword (NULL);
}


/******************************************************************************************************/

struct TVFSItem *
VFSListFirst (struct TVFSGlobs *globs, const char *sDir, gboolean FollowSymlinks, gboolean AddFullPath, GError **error)
{
  printf ("(--) VFSListFirst: Going to list all items in '%s'\n", sDir);

  return vfs_filelist_list_first (globs->vfs_filelist, sDir, FollowSymlinks, AddFullPath, error);
}

struct TVFSItem *
VFSListNext (struct TVFSGlobs *globs, GError **error)
{
  return vfs_filelist_list_next (globs->vfs_filelist, error);
}

gboolean
VFSListClose (struct TVFSGlobs *globs, GError **error)
{
  return vfs_filelist_list_close (globs->vfs_filelist, error);
}


/******************************************************************************************************/

struct TVFSItem *
VFSFileInfo (struct TVFSGlobs *globs, const char *AFileName, gboolean FollowSymlinks, gboolean AddFullPath, GError **error)
{
  printf ("(--) VFSFileInfo: requested info for object '%s'\n", AFileName);

  return vfs_filelist_file_info (globs->vfs_filelist, AFileName, FollowSymlinks, AddFullPath, error);
}

/******************************************************************************************************/
/**  Recursive tree size counting                                                                     */
/**************                                                                        ****************/                  

guint64 
VFSGetDirSize (struct TVFSGlobs *globs, const char *APath)
{
  if (! globs)
    return 0;
  return vfs_filelist_get_dir_size (globs->vfs_filelist, APath);
}

void 
VFSBreakGetDirSize (struct TVFSGlobs *globs)
{
  printf ("(WW) VFSBreakGetDirSize: calling break\n");
  if (globs) 
    vfs_filelist_get_dir_size_break (globs->vfs_filelist);
}


/******************************************************************************************************/
/**  Methods modifying the archive                                                                    */
/**************                                                                        ****************/                  

gboolean
VFSMkDir (struct TVFSGlobs *globs, const char *sDirName, GError **error)
{
  CZipFileHeader header;
  char *s;
  bool bRet;

  if (sDirName == NULL || strlen (sDirName) < 1) {
    printf ("(EE) VFSMkDir: The value of 'sDirName' is NULL or empty\n");
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "The value of 'sDirName' is NULL or empty.");
    return FALSE;
  }
  if (strcmp (sDirName, "/") == 0) {
    printf ("(EE) VFSMkDir: Invalid value '%s' (duplicate the root entry?)\n", sDirName);
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Invalid value '%s' (duplicate the root entry?)", sDirName);
    return FALSE;
  }
  printf ("(II) VFSMkDir: Going to create new directory '%s'...\n", sDirName);

  try {
    try {  
//      globs->zip->SetFileHeaderAttr (header, 0x41ED0010); 
      globs->zip->SetFileHeaderAttr (header, 0x41ED);  /*  alternatively use ZipPlatform::GetDefaultAttributes();  */
      s = exclude_leading_path_sep (sDirName);
      header.SetFileName(s);
      g_free (s);
      header.SetTime (time (NULL));
      bRet = globs->zip->OpenNewFile (header, 0, NULL);
      globs->zip->CloseNewFile ();
      if (! bRet) {
        printf ("(EE) VFSMkDir: Error creating new directory '%s'\n", sDirName);
        g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error creating new directory.");
        return FALSE;
      } 
      globs->archive_modified = TRUE;   
      build_global_filelist (globs);
      return TRUE;
    }
    catch (CZipException e) {
      globs->zip->CloseNewFile (true);
      fprintf (stderr, "(EE) VFSMkDir: Error creating new directory '%s': [%d] %s, archive closed = %d.\n", 
                       sDirName, e.m_iCause, (LPCTSTR)e.GetErrorDescription(), globs->zip->IsClosed ());
      zip_error_to_gerror (e, error);
      return FALSE;
    }
  }
  catch (...)
  {
    printf ("(EE) VFSMkDir: Error creating new directory '%s'\n", sDirName);
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error creating new directory.");
    return FALSE;
  }
}


gboolean 
VFSRemove (struct TVFSGlobs *globs, const char *APath, GError **error)
{
  char *AFile, *AFile1, *AFile2, *AFile3;
  long int file_no;
  
  
  printf ("(II) VFSRemove: Going to remove the file '%s'...\n", APath);

  AFile = exclude_trailing_path_sep (APath);
  file_no = filelist_find_original_index_by_path (globs->files, AFile) - 1;
  g_free (AFile);

  if (file_no < 0) {
    printf ("(EE) VFSRemove: can't find the file specified: '%s'\n", APath);
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Can't find the file specified.");
    return FALSE;
  }

  try {
    try {
      if (! globs->zip->RemoveFile (file_no)) {
        printf ("(EE) VFSRemove: Delete file '%s' failed.\n", APath);
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Delete file '%s' failed.", APath);
        return FALSE;
      }
      build_global_filelist (globs);
      globs->archive_modified = TRUE;
      printf ("(II) VFSRemove OK.\n");
      
      /*  If we delete last file from a directory, we should make an empty one.
       *  Some archives store pathnames only with filenames, no separate records for directories.
       **/
      AFile1 = exclude_trailing_path_sep (APath);
      AFile2 = g_path_get_dirname (AFile1);
      AFile3 = exclude_trailing_path_sep (AFile2);
      if (strlen (AFile3) > 0 && g_strcmp0 (AFile3, "/") != 0) {
        printf ("(II) VFSRemove: AFile1: '%s', AFile2: '%s', AFile3: '%s'\n", AFile1, AFile2, AFile3);
        file_no = filelist_find_original_index_by_path (globs->files, AFile2) - 1;
        printf ("(II) VFSRemove: deleted: '%s', parent: '%s', file_no = %ld\n", APath, AFile3, file_no);
        if (file_no < 0) {
          printf ("(WW) VFSRemove: sparse ZIP archive detected, adding empty directory: '%s'\n", AFile3);
          VFSMkDir (globs, AFile3, NULL);
        }
      }  
      g_free (AFile1);   
      g_free (AFile2);   
      g_free (AFile3);
      
      return TRUE;
    }
    catch (CZipException e) {
      fprintf (stderr, "(EE) VFSRemove: Delete file '%s' failed: [%d] %s, archive closed = %d.\n", 
                       APath, e.m_iCause, (LPCTSTR)e.GetErrorDescription(), globs->zip->IsClosed ());
      zip_error_to_gerror (e, error);
      return FALSE;
    }
  }
  catch (...)
  {
    printf ("(EE) VFSRemove: Delete file '%s' failed.\n", APath);
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Delete file '%s' failed.", APath);
    return FALSE;
  }
}


gboolean 
VFSRename (struct TVFSGlobs *globs, const char *sSrcName, const char *sDstName, GError **error)
{
  char *AFile;
  char *ADestFile;
  long int file_no;

  printf ("(II) VFSRename: Going to rename/move the file '%s' to '%s'...\n", sSrcName, sDstName);

  AFile = exclude_trailing_path_sep (sSrcName);
  ADestFile = exclude_trailing_path_sep (sDstName);
  file_no = filelist_find_original_index_by_path (globs->files, AFile) - 1;
  g_free (AFile);

  if (file_no < 0) {
    printf ("(EE) VFSRename: can't find the file specified: '%s'\n", sSrcName);
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Can't find the file specified.");
    return FALSE;
  }

  try {
    try {
      if (! globs->zip->RenameFile (file_no, ADestFile)) {
        printf ("(EE) VFSRename: Rename/move file '%s' failed.\n", sSrcName);
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Rename/move file '%s' failed.", sSrcName);
        return FALSE;
      }
      g_free (ADestFile);
      build_global_filelist (globs);
      globs->archive_modified = TRUE;	
      return TRUE;
    }
    catch (CZipException e) {
      fprintf (stderr, "(EE) VFSRename: Rename/move file '%s' failed: [%d] %s, archive closed = %d.\n",
                       sSrcName, e.m_iCause, (LPCTSTR)e.GetErrorDescription(), globs->zip->IsClosed ());
      zip_error_to_gerror (e, error);
      return FALSE;
    }
  }
  catch (...)
  {
    printf ("(EE) VFSRename: Rename/move file failed.\n");
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Rename/move file failed.");
    return FALSE;
  }
}


gboolean 
VFSMakeSymLink (struct TVFSGlobs *globs, const char *NewFileName, const char *PointTo, GError **error)
{
  fprintf (stderr, "(EE) VFSMakeSymLink: Symbolic links not supported in ZIP archives.\n");
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Symbolic links not supported in ZIP archives.");
  return FALSE;
}


gboolean 
VFSChmod (struct TVFSGlobs *globs, const char *FileName, guint32 Mode, GError **error)
{
  char *AFile;
  long int file_no;
  CZipFileHeader *header;

  printf ("(II) VFSChmod: Going to change permissions of the file '%s'...\n", FileName);

  AFile = exclude_trailing_path_sep (FileName);
  file_no = filelist_find_original_index_by_path (globs->files, AFile) - 1;
  g_free (AFile);

  if (file_no < 0) {
    printf ("(EE) VFSChmod: can't find the file specified: '%s'\n", FileName);
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Can't find the file specified.");
    return FALSE;
  }

  try {
    try {
      /*  set system compatibility first  */
      if (! globs->zip->SetSystemCompatibility (ZipCompatibility::zcUnix)) {
          printf ("(EE) VFSChmod: Unable to set system compatibility\n");
      }
    	
      /*  change the header data  */
      globs->zip->ReadLocalHeader (file_no);
      header = globs->zip->GetFileInfo (file_no);
      if (! header) {
        printf ("(EE) VFSChmod: Permissions modification of the file '%s' failed: NULL returned by GetFileInfo()\n", FileName);
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Permissions modification of the file '%s' failed: NULL returned by GetFileInfo()", FileName);
        return FALSE;
      }
      //  We need to change only 0xF000FFFF mask
      //  The 0xF_______ bits represents file/directory type, the 0x____FFFF represents ZIP attributes and 0x_FFF____ represents unix permissions

//      printf("(II) VFSChmod: Current permissions: %lX, stripped: %lX, setting to: %X, modified: %lX\n", 
//              header->GetSystemAttr(), header->GetSystemAttr() & 0xF000FFFF, Mode & 0xFFF, (header->GetSystemAttr() & 0xF000FFFF) + ((Mode & 0xFFF) << 16));
//      globs->zip->SetFileHeaderAttr(*header, (header->GetSystemAttr() & 0xF000FFFF) + ((Mode & 0xFFF) << 16));

      printf ("(II) VFSChmod: Current permissions: 0x%lX, stripped: 0x%lX, setting to: 0x%X, modified: 0x%lX\n", 
              header->GetSystemAttr(), header->GetSystemAttr() & 0xFFFFF000, 
              Mode & 0xFFF, (header->GetSystemAttr() & 0xFFFFF000) + (Mode & 0xFFF));
      globs->zip->SetFileHeaderAttr (*header, (header->GetSystemAttr() & 0xFFFFF000) + (Mode & 0xFFF));

      /*  write local header information  */
      globs->zip->OverwriteLocalHeader (file_no);

#if 0      
      //  Re-encrypt the file
      if (header->IsEncrypted()) {
    	  printf("(II) VFSChmod: Re-encrypting the file...\n");
    	  if (! globs->zip->EncryptFile(file_no)) 
    		  printf("(EE) VFSChmod: Unable to encrypt the file\n");
      }
#endif
      globs->zip->RemoveCentralDirectoryFromArchive ();
      globs->zip->Flush ();

      printf ("(II) VFSChmod OK.\n");
      build_global_filelist (globs); 
      globs->archive_modified = TRUE;
      return TRUE;
    }
    catch (CZipException e) {
      globs->zip->CloseNewFile (true);
      fprintf (stderr, "(EE) VFSChmod: permissions modification of the file '%s' failed: [%d] %s, archive closed = %d.\n", 
                       FileName, e.m_iCause, (LPCTSTR)e.GetErrorDescription(), globs->zip->IsClosed());
      zip_error_to_gerror (e, error);
      return FALSE;
    }
  }
  catch (...)
  {
    printf ("(EE) VFSChmod: permissions modification of the file '%s' failed.\n", FileName);
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Permissions modification of the file '%s' failed.", FileName);
    return FALSE;
  }
}


gboolean 
VFSChown (struct TVFSGlobs *globs, const char *FileName, guint32 UID, guint32 GID, GError **error)
{
  fprintf (stderr, "(EE) VFSChown: Owner changing is not supported in ZIP archives.\n");
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Owner changing is not supported in ZIP archives.");
  return FALSE;
}


gboolean 
VFSChangeTimes (struct TVFSGlobs *globs, const char *APath, guint32 mtime, guint32 atime, GError **error)
{
  char *AFile;
  long int file_no;
  CZipFileHeader *header;

  printf ("(II) VFSChangeTimes: Going to change date/times of the file '%s'...\n", APath);

  AFile = exclude_trailing_path_sep (APath);
  file_no = filelist_find_original_index_by_path (globs->files, AFile) - 1;
  g_free (AFile);

  if (file_no < 0) {
    printf ("(EE) VFSChangeTimes: can't find the file specified: '%s'\n", APath);
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Can't find the file specified.");
    return FALSE;
  }

  try {
    try {
      /*  read the local header information  */
      globs->zip->ReadLocalHeader (file_no);
      header = globs->zip->GetFileInfo (file_no);
      if (! header) {
        printf ("(EE) VFSChangeTimes: DateTime modification of the file '%s' failed: NULL returned by GetFileInfo()\n", APath);
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "DateTime modification of the file '%s' failed: NULL returned by GetFileInfo()", APath);
        return FALSE;
      }
      /*  change the header data  */
      header->SetTime (mtime);
      
/*      //  Re-encrypt the file
      if (header->IsEncrypted()) {
    	  printf("(II) VFSChangeTimes: Re-encrypting the file...\n");
    	  if (! globs->zip->EncryptFile(file_no)) 
    		  printf("(EE) VFSChangeTimes: Unable to encrypt the file\n");
      }
*/
      /*  write local header information  */
      globs->zip->OverwriteLocalHeader (file_no);      
      globs->zip->RemoveCentralDirectoryFromArchive ();
     
      printf ("(II) VFSChangeTimes OK.\n");
      build_global_filelist (globs); 
      globs->archive_modified = TRUE;
      return TRUE;
    }
    catch (CZipException e) {
      globs->zip->CloseNewFile (true);
      fprintf (stderr, "(EE) VFSChangeTimes: DateTime modification of the file '%s' failed: [%d] %s, archive closed = %d.\n", 
                       APath, e.m_iCause, (LPCTSTR)e.GetErrorDescription (), globs->zip->IsClosed ());
      zip_error_to_gerror (e, error);
      return FALSE;
    }
  }
  catch (...) {
    printf ("(EE) VFSChangeTimes: DateTime modification of the file '%s' failed.\n", APath);
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "DateTime modification of the file '%s' failed.", APath);
    return FALSE;
  }
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////

#if 0
TVFSFileDes VFSOpenFile(struct TVFSGlobs *globs, const char *APath, int Mode, int *Error)
{
  *Error = cVFS_Not_Supported;
  return (TVFSFileDes)0;
}

TVFSResult VFSCloseFile(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor)
{
  return cVFS_Not_Supported;
}

u_int64_t VFSFileSeek(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor, u_int64_t AbsoluteOffset, int *Error)
{
  *Error = cVFS_Not_Supported;
  return 0;
}

int VFSReadFile(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor, void *Buffer, int ABlockSize, int *Error)
{
  *Error = cVFS_Not_Supported;
  return 0;
}

int VFSWriteFile(struct TVFSGlobs *globs, TVFSFileDes FileDescriptor, void *Buffer, int BytesCount, int *Error)
{
  *Error = cVFS_Not_Supported;
  return 0;
}
#endif

void 
VFSSetBlockSize (struct TVFSGlobs *globs, guint32 Value)
{
  if (globs)
    globs->block_size = Value;
}

gboolean 
VFSIsOnSameFS (struct TVFSGlobs *globs, const char *Path1, const char *Path2, gboolean FollowSymlinks)
{
  printf ("(II) VFSIsOnSameFS: Not supported in ZIP archives.\n");
  return TRUE;
}

gboolean
VFSTwoSameFiles (struct TVFSGlobs *globs, const char *Path1, const char *Path2, gboolean FollowSymlinks)
{
  printf ("(II) VFSTwoSameFiles: Not supported in ZIP archives, comparing by paths.\n");
  return compare_two_same_files (Path1, Path2);
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////


gboolean
VFSStartCopyOperation (struct TVFSGlobs *globs, GError **error)
{
  g_return_val_if_fail (globs != NULL, FALSE);

  printf ("(II) VFSStartCopyOperation: doing nothing for the moment.\n");
  
  return TRUE;
}


gboolean
VFSStopCopyOperation (struct TVFSGlobs *globs, GError **error)
{
  g_return_val_if_fail (globs != NULL, FALSE);
  
  if (globs->archive_modified) {
    printf ("(II) VFSStopCopyOperation: rebuilding tree.\n");
    globs->zip->Flush ();
    build_global_filelist (globs);
  } else {
    printf ("(II) VFSStartCopyOperation: doing nothing for the moment.\n");
  }

  return TRUE;
}


/*  Known issues: 
 *    - crashes when no space left on NFS mounts, probably unhandled exception in further ZipArchive code (repro: Gentoo, Ubuntu)
 *  
 **/
gboolean 
VFSCopyToLocal (struct TVFSGlobs *globs, const char *sSrcName, const char *sDstName, gboolean Append, GError **error)
{
  gboolean try_again;
  long int file_no;
  char *s;
  char *dest_path;
  char *dest_filename;
  char *passwd;
  gboolean res;

  
  if (sSrcName == NULL || sDstName == NULL || strlen (sSrcName) < 1 || strlen (sDstName) < 1) {
    printf ("(EE) VFSCopyToLocal: The value of 'sSrcName' or 'sDstName' is NULL or empty\n");
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "The value of 'sSrcName' or 'sDstName' is NULL or empty.");
    return FALSE;
  }
  
  printf ("(II) VFSCopyToLocal: copying file '%s' out to '%s'\n", sSrcName, sDstName);

  file_no = filelist_find_original_index_by_path (globs->files, sSrcName) - 1;
  if (file_no < 0) {
    printf ("(EE) VFSCopyToLocal: can't find source file '%s'\n", sSrcName);
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "cannot find file '%s'", sSrcName);
    return FALSE;
  }

  s = exclude_trailing_path_sep (sDstName);
  dest_path = g_path_get_dirname (s);
  dest_filename = g_path_get_basename (s);
  g_free (s);

  /*  Perform extract  */
  try {
    do {
      try {
        try_again = FALSE; 
        if (! globs->zip->ExtractFile (file_no, dest_path, false, dest_filename, globs->block_size)) {
          globs->zip->CloseFile (NULL, true);
          fprintf (stderr, "(EE) VFSCopyToLocal: Error while copying out, archive closed = %d.\n", globs->zip->IsClosed ());
          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error while copying out.");
          return FALSE;
        } 
        fprintf (stderr, "(II) VFSCopyToLocal: copy OK, archive closed = %d.\n", globs->zip->IsClosed ());
      }
      catch (CZipException e) {
        globs->zip->CloseFile (NULL, true);
        fprintf (stderr, "(EE) VFSCopyToLocal: Error while copying out: [%d] %s, archive closed = %d.\n", 
                         e.m_iCause, (LPCTSTR)e.GetErrorDescription(), globs->zip->IsClosed());
        switch (e.m_iCause) {
          case CZipException::badPassword:
            if (globs->callback_ask_password) {
              passwd = NULL;
              res = globs->callback_ask_password ("The archive is encrypted and requires password",
                                                  NULL, NULL, NULL, (TVFSAskPasswordFlags)(VFS_ASK_PASSWORD_NEED_PASSWORD | VFS_ASK_PASSWORD_ARCHIVE_MODE),
                                                  NULL, &passwd, NULL, NULL, NULL,
                                                  globs->callback_data);
              if (res && passwd) {
                fprintf (stderr, "  (II) VFSCopyToLocal: setting password to '%s'\n", passwd);
                globs->zip->SetPassword (passwd);
                try_again = TRUE;
                break;
              } else {
                g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Operation has been cancelled.");                
                return FALSE;  
              }
            }
          default:
            zip_error_to_gerror (e, error);
            return FALSE;
        }
      }
    } while (try_again);
  }
  catch (...)
  {
    fprintf (stderr, "(EE) VFSCopyToLocal: Fatal error while copying out..., archive closed = %d.\n", globs->zip->IsClosed());
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Fatal error while copying out.");                
    return FALSE;
  }  
  g_free (dest_path);
  g_free (dest_filename);

  return TRUE;
}


/*  Known issues:
 *   - archive corruption when no space left on device
 *   - encrypted files are unreadable after copy in
 *   
 **/
gboolean 
VFSCopyFromLocal (struct TVFSGlobs *globs, const char *sSrcName, const char *sDstName, gboolean Append, GError **error)
{
  gboolean try_again;
  char *s;
  char *passwd;
  gboolean res;

  if (sSrcName == NULL || sDstName == NULL || strlen (sSrcName) < 1 || strlen (sDstName) < 1) {
    printf ("(EE) VFSCopyFromLocal: The value of 'sSrcName' or 'sDstName' is NULL or empty\n");
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "The value of 'sSrcName' or 'sDstName' is NULL or empty.");
    return FALSE;
  }

  printf ("(II) VFSCopyFromLocal: copying file '%s' in to '%s'\n", sSrcName, sDstName);

  try {
    do {
      try {
        try_again = FALSE; 
        s = exclude_leading_path_sep (sDstName);
        if (! globs->zip->AddNewFile (sSrcName, s, -1, CZipArchive::zipsmSafeSmart, globs->block_size)) {
          globs->zip->CloseNewFile (true);
          globs->zip->CloseFile (NULL, true);
          fprintf (stderr, "(EE) VFSCopyFromLocal: Error while copying in, archive closed = %d.\n", globs->zip->IsClosed ());
          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Error while copying in.");
          return FALSE;
        }

        printf ("(II) VFSCopyFromLocal: copy OK, archive closed = %d.\n", globs->zip->IsClosed ());
        globs->archive_modified = TRUE;

  /*      
        //  Encrypt the file if archive contains any encrypted files
        if (globs->need_password) {
            unsigned long int file_no = filelist_find_index_by_path(globs->files, s) - 1;
            if (file_no < 0) {
                    printf("(EE) VFSCopyFromLocal: unable to find index for newly written file '%s'\n", sSrcName);
                    return cVFS_WriteErr;
            }
            printf("(II) VFSCopyFromLocal: Encrypting the newly written file...\n");
            if (! globs->zip->EncryptFile(file_no)) 
                    printf("(EE) VFSCopyFromLocal: Unable to encrypt the newly written file\n");
        }  
    */
        
        g_free (s);
      }
      catch (CZipException e) {
        globs->zip->CloseNewFile (true);
        globs->zip->CloseFile (NULL, true);
        fprintf (stderr, "(EE) VFSCopyFromLocal: Error while copying in: [%d] %s, archive closed = %d.\n", 
                         e.m_iCause, (LPCTSTR)e.GetErrorDescription(), globs->zip->IsClosed());
        switch (e.m_iCause) {
          case CZipException::badPassword:
            if (globs->callback_ask_password) {
              passwd = NULL;
              res = globs->callback_ask_password ("The archive is encrypted and requires password",
                                                  NULL, NULL, NULL, (TVFSAskPasswordFlags)(VFS_ASK_PASSWORD_NEED_PASSWORD | VFS_ASK_PASSWORD_ARCHIVE_MODE),
                                                  NULL, &passwd, NULL, NULL, NULL,
                                                  globs->callback_data);
              if (res && passwd) {
                fprintf (stderr, "  (II) VFSCopyFromLocal: setting password to '%s'\n", passwd);
                globs->zip->SetPassword (passwd);
                try_again = TRUE;
                break;
              } else {
                g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Operation has been cancelled.");                
                return FALSE;  
              }
            }
          default:
            zip_error_to_gerror (e, error);
            return FALSE;
        }
      }
    } while (try_again);
  }
  catch (...) {
    fprintf (stderr, "(EE) VFSCopyFromLocal: Fatal error while copying in..., archive closed = %d.\n", globs->zip->IsClosed());
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Fatal error while copying in.");                
    return FALSE;
  }

  return TRUE;
}


///////////////////////////////
//  end of extern "C"
}




/***
* Todo:
* 
* - UTF-8, FName/FDisplayName and absolute/relative paths revision needed!
*    (check http://www.artpol-software.com/ZipArchive/KB/0610051525.aspx )
* - implement an "opened" flag - is it really needed? + add tests to all functions
* - readonly checking IsReadOnly() + add tests to all functions modifying the archive
* - after VFS API update implement archive testing, compression level setting, retrieving compression info for each file and archive, readonly flags, begin-action, end-action (no refresh, faster operations) ...
* - moving files from one directory to another within the archive
* - do something like start/stop operation for write, so that chmod/chown/utime calls can find newly packed file.
*
*/