TestUtil.cc 684 B

1234567891011121314151617181920212223242526272829
  1. #include "TestUtil.h"
  2. #include "a2io.h"
  3. #include "File.h"
  4. #include "StringFormat.h"
  5. #include "FatalException.h"
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <cerrno>
  10. #include <cstring>
  11. namespace aria2 {
  12. void createFile(const std::string& path, size_t length)
  13. {
  14. File(File(path).getDirname()).mkdirs();
  15. int fd = creat(path.c_str(), OPEN_MODE);
  16. if(fd == -1) {
  17. throw FatalException(StringFormat("Could not create file=%s. cause:%s",
  18. path.c_str(), strerror(errno)).str());
  19. }
  20. if(-1 == ftruncate(fd, length)) {
  21. throw FatalException(StringFormat("ftruncate failed. cause:%s",
  22. strerror(errno)).str());
  23. }
  24. close(fd);
  25. }
  26. };