test_fileutl.cc 934 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <apt-pkg/fileutl.h>
  2. #include <apt-pkg/strutl.h>
  3. #include <apt-pkg/error.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <iostream>
  11. #include <string>
  12. static void callsystem(std::string const &call)
  13. {
  14. auto ret = system(call.c_str());
  15. if (WIFEXITED(ret) == false || WEXITSTATUS(ret) != 0)
  16. _error->Error("Calling %s failed!", call.c_str());
  17. }
  18. int main(int, char ** argv)
  19. {
  20. auto const pid = getpid();
  21. std::string ls;
  22. strprintf(ls, "ls -l /proc/%d/fd", pid);
  23. callsystem(ls);
  24. FileFd t;
  25. t.Open(argv[1], FileFd::ReadOnly, FileFd::Extension);
  26. callsystem(ls);
  27. char buf[1024];
  28. unsigned long long act;
  29. while (t.Read(buf, sizeof(buf), &act))
  30. if (act == 0)
  31. break;
  32. callsystem(ls);
  33. t.Seek(5);
  34. callsystem(ls);
  35. t.Close();
  36. callsystem(ls);
  37. auto const ret = _error->PendingError();
  38. _error->DumpErrors();
  39. return ret;
  40. }