temp_file.odin 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 := 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: [10]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 := TEMP_ALLOCATOR_GUARD({ allocator })
  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: [10]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. temp_allocator := TEMP_ALLOCATOR_GUARD({})
  80. if len(dir) > 0 && is_path_separator(dir[len(dir)-1]) {
  81. return concatenate({dir, name}, temp_allocator,)
  82. }
  83. return concatenate({dir, Path_Separator_String, name}, temp_allocator)
  84. }