temp_file.odin 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package os2
  2. import "base:runtime"
  3. @(private="file")
  4. MAX_ATTEMPTS :: 1<<13 // Should be enough for everyone, right?
  5. // Creates a new temperatory file in the directory `dir`.
  6. //
  7. // Opens the file for reading and writing, with 0o666 permissions, and returns the new `^File`.
  8. // The filename is generated by taking a pattern, and adding a randomized string to the end.
  9. // If the pattern includes an "*", the random string replaces the last "*".
  10. // If `dir` is an empty string, `temp_directory()` will be used.
  11. //
  12. // The caller must `close` the file once finished with.
  13. @(require_results)
  14. create_temp_file :: proc(dir, pattern: string) -> (f: ^File, err: Error) {
  15. TEMP_ALLOCATOR_GUARD()
  16. dir := dir if dir != "" else temp_directory(temp_allocator()) or_return
  17. prefix, suffix := _prefix_and_suffix(pattern) or_return
  18. prefix = temp_join_path(dir, prefix) or_return
  19. rand_buf: [32]byte
  20. name_buf := make([]byte, len(prefix)+len(rand_buf)+len(suffix), temp_allocator())
  21. attempts := 0
  22. for {
  23. name := concatenate_strings_from_buffer(name_buf[:], prefix, random_string(rand_buf[:]), suffix)
  24. f, err = open(name, {.Read, .Write, .Create, .Excl}, 0o666)
  25. if err == .Exist {
  26. close(f)
  27. attempts += 1
  28. if attempts < MAX_ATTEMPTS {
  29. continue
  30. }
  31. return nil, err
  32. }
  33. return f, err
  34. }
  35. }
  36. mkdir_temp :: make_directory_temp
  37. // Creates a new temporary directory in the directory `dir`, and returns the path of the new directory.
  38. //
  39. // The directory name is generated by taking a pattern, and adding a randomized string to the end.
  40. // If the pattern includes an "*", the random string replaces the last "*".
  41. // If `dir` is an empty tring, `temp_directory()` will be used.
  42. @(require_results)
  43. make_directory_temp :: proc(dir, pattern: string, allocator: runtime.Allocator) -> (temp_path: string, err: Error) {
  44. TEMP_ALLOCATOR_GUARD()
  45. dir := dir if dir != "" else temp_directory(temp_allocator()) or_return
  46. prefix, suffix := _prefix_and_suffix(pattern) or_return
  47. prefix = temp_join_path(dir, prefix) or_return
  48. rand_buf: [32]byte
  49. name_buf := make([]byte, len(prefix)+len(rand_buf)+len(suffix), temp_allocator())
  50. attempts := 0
  51. for {
  52. name := concatenate_strings_from_buffer(name_buf[:], prefix, random_string(rand_buf[:]), suffix)
  53. err = make_directory(name, 0o700)
  54. if err == nil {
  55. return clone_string(name, allocator)
  56. }
  57. if err == .Exist {
  58. attempts += 1
  59. if attempts < MAX_ATTEMPTS {
  60. continue
  61. }
  62. return "", err
  63. }
  64. if err == .Not_Exist {
  65. if _, serr := stat(dir, temp_allocator()); serr == .Not_Exist {
  66. return "", serr
  67. }
  68. }
  69. return "", err
  70. }
  71. }
  72. temp_dir :: temp_directory
  73. @(require_results)
  74. temp_directory :: proc(allocator: runtime.Allocator) -> (string, Error) {
  75. return _temp_dir(allocator)
  76. }
  77. @(private="file")
  78. temp_join_path :: proc(dir, name: string) -> (string, runtime.Allocator_Error) {
  79. if len(dir) > 0 && is_path_separator(dir[len(dir)-1]) {
  80. return concatenate({dir, name}, temp_allocator(),)
  81. }
  82. return concatenate({dir, Path_Separator_String, name}, temp_allocator())
  83. }