tlibtar1.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. { Simple libtar test - creates a 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 = 'tlibtar1.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. TotalEntries = 4;
  13. var
  14. TW: TTarWriter;
  15. TA: TTarArchive;
  16. DirRec: TTarDirRec;
  17. Content: RawByteString;
  18. EntryCount: Integer;
  19. Errors: Integer;
  20. begin
  21. Errors := 0;
  22. WriteLn('Creating tar archive: ', TAR_FILENAME);
  23. try
  24. TW := TTarWriter.Create(TAR_FILENAME);
  25. try
  26. { Set default permissions }
  27. TW.Permissions := [tpReadByOwner, tpWriteByOwner, tpReadByGroup, tpReadByOther];
  28. { Add a simple text file }
  29. WriteLn(' Adding: hello.txt');
  30. TW.AddString(CONTENT_1, 'hello.txt', Now);
  31. { Add a file in a subdirectory }
  32. WriteLn(' Adding: subdir/data.txt');
  33. TW.AddString(CONTENT_2, 'subdir/data.txt', Now);
  34. { Add a directory entry }
  35. WriteLn(' Adding: emptydir/');
  36. TW.AddDir(CONTENT_4, Now);
  37. { Add a symbolic link }
  38. WriteLn(' Adding: link.txt -> hello.txt');
  39. TW.AddSymbolicLink('link.txt', CONTENT_3, Now);
  40. TW.Finalize;
  41. finally
  42. TW.Free;
  43. end;
  44. WriteLn('Done. Archive created successfully.');
  45. WriteLn;
  46. WriteLn('=== Reading and verifying archive ===');
  47. WriteLn;
  48. TA := TTarArchive.Create(TAR_FILENAME);
  49. try
  50. EntryCount := 0;
  51. while TA.FindNext(DirRec) do
  52. begin
  53. Inc(EntryCount);
  54. WriteLn('Entry ', EntryCount, ':');
  55. WriteLn(' Name: ', DirRec.Name);
  56. WriteLn(' Size: ', DirRec.Size);
  57. WriteLn(' Type: ', FILETYPE_NAME[DirRec.FileType]);
  58. if DirRec.FileType = ftSymbolicLink then
  59. begin
  60. if DirRec.LinkName <> CONTENT_3 then
  61. begin
  62. Inc(Errors);
  63. writeln(' Wrong link value');
  64. end;
  65. WriteLn(' Link: -> ', DirRec.LinkName);
  66. end;
  67. if DirRec.FileType = ftDirectory then
  68. begin
  69. if DirRec.Name <> CONTENT_4 then
  70. begin
  71. Inc(Errors);
  72. writeln(' Wrong directory Name');
  73. end;
  74. end;
  75. if DirRec.ChecksumOK then
  76. WriteLn(' Checksum: OK')
  77. else
  78. begin
  79. WriteLn(' Checksum: FAILED');
  80. Inc(Errors);
  81. end;
  82. { Verify content for regular files }
  83. if DirRec.FileType = ftNormal then
  84. begin
  85. Content := TA.ReadFile;
  86. if DirRec.Name = 'hello.txt' then
  87. begin
  88. if Content = CONTENT_1 then
  89. WriteLn(' Content: verified OK')
  90. else
  91. begin
  92. WriteLn(' Content: MISMATCH');
  93. Inc(Errors);
  94. end;
  95. end
  96. else if DirRec.Name = 'subdir/data.txt' then
  97. begin
  98. if Content = CONTENT_2 then
  99. WriteLn(' Content: verified OK')
  100. else
  101. begin
  102. WriteLn(' Content: MISMATCH');
  103. Inc(Errors);
  104. end;
  105. end;
  106. end;
  107. WriteLn;
  108. end;
  109. finally
  110. TA.Free;
  111. end;
  112. { === PART 3: Summary === }
  113. WriteLn('=== Summary ===');
  114. WriteLn;
  115. WriteLn('Total entries: ', EntryCount);
  116. if EntryCount <> TotalEntries then
  117. begin
  118. WriteLn('ERROR: Expected ',TotalEntries,' entries!');
  119. Inc(Errors);
  120. end;
  121. if Errors = 0 then
  122. WriteLn('All tests PASSED.')
  123. else
  124. WriteLn('FAILED with ', Errors, ' error(s).');
  125. WriteLn;
  126. finally
  127. { Cleanup }
  128. DeleteFile(TAR_FILENAME);
  129. end;
  130. Halt(Errors);
  131. end.