tempfile.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include "fs.h"
  3. #include "file.h"
  4. #include <util/folder/path.h>
  5. #include <util/generic/string.h>
  6. class TTempFile {
  7. public:
  8. inline TTempFile(const TString& fname)
  9. : Name_(fname)
  10. {
  11. }
  12. inline ~TTempFile() {
  13. NFs::Remove(Name());
  14. }
  15. inline const TString& Name() const noexcept {
  16. return Name_;
  17. }
  18. private:
  19. const TString Name_;
  20. };
  21. class TTempFileHandle: public TTempFile, public TFile {
  22. public:
  23. TTempFileHandle();
  24. TTempFileHandle(const TString& fname);
  25. static TTempFileHandle InCurrentDir(const TString& filePrefix = "yandex", const TString& extension = "tmp");
  26. static TTempFileHandle InDir(const TFsPath& dirPath, const TString& filePrefix = "yandex", const TString& extension = "tmp");
  27. private:
  28. TFile CreateFile() const;
  29. };
  30. /*
  31. * Creates a unique temporary filename in specified directory.
  32. * If specified directory is NULL or empty, then system temporary directory is used.
  33. *
  34. * Note, that the function is not race-free, the file is guaranteed to exist at the time the function returns, but not at the time the returned name is first used.
  35. * Throws TSystemError on error.
  36. *
  37. * Returned filepath has such format: dir/prefixXXXXXX.extension or dir/prefixXXXXXX
  38. * But win32: dir/preXXXX.tmp (prefix is up to 3 characters, extension is always tmp).
  39. */
  40. TString MakeTempName(const char* wrkDir = nullptr, const char* prefix = "yandex", const char* extension = "tmp");