tlibtar3.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. { Simple libtar test - creates a (PAX) tar archive with a few entries }
  2. program libtar_simple_test;
  3. {$mode objfpc}{$H+}
  4. uses
  5. SysUtils, libtar;
  6. const
  7. TAR_FILENAME = 'tlibtar3.tar';
  8. CONTENT_1 = 'Hello, World!';
  9. CONTENT_2 = 'Some data in a subdirectory';
  10. CONTENT_3 = 'hello.txt';
  11. CONTENT_4 = 'hello.txt';
  12. CONTENT_5 = 'Content of file with long path';
  13. TotalEntries = 5;
  14. var
  15. TW: TTarWriter;
  16. TA: TTarArchive;
  17. DirRec: TTarDirRec;
  18. Content: RawByteString;
  19. EntryCount: Integer;
  20. Errors: Integer;
  21. LongName: AnsiString;
  22. begin
  23. Errors := 0;
  24. { Create long filename for testing }
  25. LongName := 'very/deep/directory/structure/with/many/levels/' +
  26. StringOfChar('x', 100) + '/file.txt';
  27. WriteLn('Creating tar archive: ', TAR_FILENAME);
  28. try
  29. TW := TTarWriter.Create(TAR_FILENAME);
  30. try
  31. { Set default permissions }
  32. TW.Permissions := [tpReadByOwner, tpWriteByOwner, tpReadByGroup, tpReadByOther];
  33. { Add a simple text file }
  34. WriteLn(' Adding: hello.txt');
  35. TW.AddString(CONTENT_1, 'hello.txt', Now);
  36. { Add a file in a subdirectory }
  37. WriteLn(' Adding: subdir/data.txt');
  38. TW.AddString(CONTENT_2, 'subdir/data.txt', Now);
  39. { Add a directory entry }
  40. WriteLn(' Adding: emptydir/');
  41. TW.AddDir(CONTENT_4, Now);
  42. { Add a symbolic link }
  43. WriteLn(' Adding: link.txt -> hello.txt');
  44. TW.AddSymbolicLink('link.txt', CONTENT_3, Now);
  45. { Add a file in a subdirectory with long name }
  46. WriteLn(' Adding long name (', Length(LongName), ' chars)');
  47. TW.AddString(CONTENT_5, LongName, Now);
  48. TW.Finalize;
  49. finally
  50. TW.Free;
  51. end;
  52. WriteLn('Done. Archive created successfully.');
  53. WriteLn;
  54. WriteLn('=== Reading and verifying archive ===');
  55. WriteLn;
  56. TA := TTarArchive.Create(TAR_FILENAME);
  57. try
  58. EntryCount := 0;
  59. while TA.FindNext(DirRec) do
  60. begin
  61. Inc(EntryCount);
  62. WriteLn('Entry ', EntryCount, ':');
  63. WriteLn(' Name: ', DirRec.Name);
  64. WriteLn(' Size: ', DirRec.Size);
  65. WriteLn(' Type: ', FILETYPE_NAME[DirRec.FileType]);
  66. if DirRec.FileType = ftSymbolicLink then
  67. begin
  68. if DirRec.LinkName <> CONTENT_3 then
  69. begin
  70. Inc(Errors);
  71. writeln(' Wrong link value');
  72. end;
  73. WriteLn(' Link: -> ', DirRec.LinkName);
  74. end;
  75. if DirRec.FileType = ftDirectory then
  76. begin
  77. if DirRec.Name <> CONTENT_4 then
  78. begin
  79. Inc(Errors);
  80. writeln(' Wrong directory Name');
  81. end;
  82. end;
  83. if DirRec.ChecksumOK then
  84. WriteLn(' Checksum: OK')
  85. else
  86. begin
  87. WriteLn(' Checksum: FAILED');
  88. Inc(Errors);
  89. end;
  90. { Verify content for regular files }
  91. if DirRec.FileType = ftNormal then
  92. begin
  93. Content := TA.ReadFile;
  94. if DirRec.Name = 'hello.txt' then
  95. begin
  96. if Content = CONTENT_1 then
  97. WriteLn(' Content: verified OK')
  98. else
  99. begin
  100. WriteLn(' Content: MISMATCH');
  101. Inc(Errors);
  102. end;
  103. end
  104. else if DirRec.Name = 'subdir/data.txt' then
  105. begin
  106. if Content = CONTENT_2 then
  107. WriteLn(' Content: verified OK')
  108. else
  109. begin
  110. WriteLn(' Content: MISMATCH');
  111. Inc(Errors);
  112. end;
  113. end
  114. else if DirRec.Name = LongName then
  115. begin
  116. if Content = CONTENT_5 then
  117. WriteLn(' Content: verified OK')
  118. else
  119. begin
  120. WriteLn(' Content: MISMATCH');
  121. Inc(Errors);
  122. end;
  123. end
  124. else
  125. begin
  126. WriteLn(' Unexpected entry');
  127. Inc(Errors);
  128. end;
  129. end;
  130. WriteLn;
  131. end;
  132. finally
  133. TA.Free;
  134. end;
  135. { === PART 3: Summary === }
  136. WriteLn('=== Summary ===');
  137. WriteLn;
  138. WriteLn('Total entries: ', EntryCount);
  139. if EntryCount <> TotalEntries then
  140. begin
  141. WriteLn('ERROR: Expected ',TotalEntries,' entries!');
  142. Inc(Errors);
  143. end;
  144. if Errors = 0 then
  145. WriteLn('All tests PASSED.')
  146. else
  147. WriteLn('FAILED with ', Errors, ' error(s).');
  148. WriteLn;
  149. finally
  150. { Cleanup }
  151. DeleteFile(TAR_FILENAME);
  152. end;
  153. Halt(Errors);
  154. end.