test_fileutl.cc 912 B

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