TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ktartest.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C) 2002, 2003 David Faure <faure@kde.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License version 2 as published by the Free Software Foundation;
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public License
  14. * along with this library; see the file COPYING.LIB. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301, USA.
  17. */
  18. #include "ktar.h"
  19. #include <stdio.h>
  20. #include <kinstance.h>
  21. #include <tqfile.h>
  22. #include <assert.h>
  23. void recursive_print( const KTarDirectory * dir, const TQString & path )
  24. {
  25. TQStringList l = dir->entries();
  26. TQStringList::Iterator it = l.begin();
  27. for( ; it != l.end(); ++it )
  28. {
  29. const KTarEntry* entry = dir->entry( (*it) );
  30. printf("mode=%07o %s %s %s%s %d isdir=%d\n", entry->permissions(), entry->user().latin1(), entry->group().latin1(), path.latin1(), (*it).latin1(),
  31. entry->isFile() ? static_cast<const KArchiveFile *>(entry)->size() : 0,
  32. entry->isDirectory());
  33. if (entry->isDirectory())
  34. recursive_print( (KTarDirectory *)entry, path+(*it)+"/" );
  35. }
  36. }
  37. int main( int argc, char** argv )
  38. {
  39. if (argc != 3)
  40. {
  41. printf("\n"
  42. " Usage :\n"
  43. " ./ktartest list /path/to/existing_file.tar.gz tests listing an existing tar.gz\n"
  44. " ./ktartest readwrite newfile.tar.gz will create the tar.gz, then close and reopen it.\n"
  45. " ./ktartest maxlength newfile.tar.gz tests the maximum filename length allowed.\n"
  46. " ./ktartest iodevice /path/to/existing_file.tar.gz tests KArchiveFile::device()\n");
  47. return 1;
  48. }
  49. TDEInstance instance("ktartest");
  50. TQString command = argv[1];
  51. if ( command == "list" )
  52. {
  53. KTarGz tar( argv[2] );
  54. if ( !tar.open( IO_ReadOnly ) )
  55. {
  56. printf("Could not open %s for reading\n", argv[2] );
  57. return 1;
  58. }
  59. const KTarDirectory* dir = tar.directory();
  60. //printf("calling recursive_print\n");
  61. recursive_print( dir, "" );
  62. //printf("recursive_print called\n");
  63. tar.close();
  64. return 0;
  65. }
  66. else if (command == "readwrite" )
  67. {
  68. KTarGz tar( argv[2] );
  69. if ( !tar.open( IO_WriteOnly ) )
  70. {
  71. printf("Could not open %s for writing\n", argv[1]);
  72. return 1;
  73. }
  74. tar.writeFile( "empty", "weis", "users", 0, "" );
  75. tar.writeFile( "test1", "weis", "users", 5, "Hallo" );
  76. tar.writeFile( "test2", "weis", "users", 8, "Hallo Du" );
  77. tar.writeFile( "mydir/test3", "weis", "users", 13, "Noch so einer" );
  78. tar.writeFile( "my/dir/test3", "dfaure", "hackers", 29, "I don't speak German (David)" );
  79. #define SIZE1 100
  80. // Now a medium file : 100 null bytes
  81. char medium[ SIZE1 ];
  82. memset( medium, 0, SIZE1 );
  83. tar.writeFile( "mediumfile", "user", "group", SIZE1, medium );
  84. // Another one, with an absolute path
  85. tar.writeFile( "/dir/subdir/mediumfile2", "user", "group", SIZE1, medium );
  86. // Now a huge file : 20000 null bytes
  87. int n = 20000;
  88. char * huge = new char[ n ];
  89. memset( huge, 0, n );
  90. tar.writeFile( "hugefile", "user", "group", n, huge );
  91. delete [] huge;
  92. tar.close();
  93. printf("-----------------------\n");
  94. if ( !tar.open( IO_ReadOnly ) )
  95. {
  96. printf("Could not open %s for reading\n", argv[1] );
  97. return 1;
  98. }
  99. const KTarDirectory* dir = tar.directory();
  100. recursive_print(dir, "");
  101. const KTarEntry* e = dir->entry( "mydir/test3" );
  102. Q_ASSERT( e && e->isFile() );
  103. const KTarFile* f = (KTarFile*)e;
  104. TQByteArray arr( f->data() );
  105. printf("SIZE=%i\n",arr.size() );
  106. TQString str( arr );
  107. printf("DATA=%s\n", str.latin1());
  108. tar.close();
  109. return 0;
  110. }
  111. else if ( command == "maxlength" )
  112. {
  113. KTarGz tar( argv[2] );
  114. if ( !tar.open( IO_WriteOnly ) )
  115. {
  116. printf("Could not open %s for writing\n", argv[1]);
  117. return 1;
  118. }
  119. // Generate long filenames of each possible length bigger than 98...
  120. // Also exceed 512 byte block size limit to see how well the ././@LongLink
  121. // implementation fares
  122. for (int i = 98; i < 514 ; i++ )
  123. {
  124. TQString str, num;
  125. str.fill( 'a', i-10 );
  126. num.setNum( i );
  127. num = num.rightJustify( 10, '0' );
  128. tar.writeFile( str+num, "testu", "testg", 3, "hum" );
  129. }
  130. // Result of this test : works perfectly now (failed at 482 formerly and
  131. // before that at 154).
  132. tar.close();
  133. printf("Now run 'tar tvzf %s'\n", argv[2]);
  134. return 0;
  135. }
  136. else if ( command == "iodevice" )
  137. {
  138. KTarGz tar( argv[2] );
  139. if ( !tar.open( IO_ReadOnly ) )
  140. return 1;
  141. const KTarDirectory* dir = tar.directory();
  142. assert(dir);
  143. const KTarEntry* entry = dir->entry( "my/dir/test3" );
  144. if ( entry && entry->isFile() )
  145. {
  146. TQIODevice *dev = static_cast<const KTarFile *>(entry)->device();
  147. TQByteArray contents = dev->readAll();
  148. printf("contents='%s'\n", TQCString(contents, contents.size()+1 ).data());
  149. } else
  150. printf("entry=%p - not found if 0, otherwise not a file\n", (void*)entry);
  151. return 0;
  152. }
  153. else
  154. printf("Unknown command\n");
  155. }