summary refs log tree commit diff stats
path: root/lib/std/tempfiles.nim
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-04-29 04:42:56 -0700
committerGitHub <noreply@github.com>2021-04-29 13:42:56 +0200
commite4db733d80d0b70192ac7eb4094a46269fe7cd5b (patch)
treeb60bdb281a29ac34be393c38a00ebbe3e53c9402 /lib/std/tempfiles.nim
parent5edddd68d04648553bc02d52cd53d3a1a14102f3 (diff)
downloadNim-e4db733d80d0b70192ac7eb4094a46269fe7cd5b.tar.gz
fix #17888: remove undefined behavior for posix.open; fix tempfiles.createTempFile (#17889)
* fix #17888: remove undefined behavior for posix.open; fix tempfiles.createTempFile

* fix for tests/async/tasyncfile.nim

* hide mode for now

* add notice regarding stability
Diffstat (limited to 'lib/std/tempfiles.nim')
-rw-r--r--lib/std/tempfiles.nim13
1 files changed, 10 insertions, 3 deletions
diff --git a/lib/std/tempfiles.nim b/lib/std/tempfiles.nim
index 1e1bbd403..2a6fe7d83 100644
--- a/lib/std/tempfiles.nim
+++ b/lib/std/tempfiles.nim
@@ -8,6 +8,8 @@
 #
 
 ## This module creates temporary files and directories.
+##
+## Experimental API, subject to change.
 
 import os, random
 
@@ -44,6 +46,8 @@ else:
 
 proc safeOpen(filename: string): File =
   ## Open files exclusively.
+  # xxx this should be clarified; it doesn't in particular prevent other processes
+  # from opening the file, at least currently.
   when defined(windows):
     let dwShareMode = FILE_SHARE_DELETE or FILE_SHARE_READ or FILE_SHARE_WRITE
     let dwCreation = CREATE_NEW
@@ -64,9 +68,13 @@ proc safeOpen(filename: string): File =
       discard close_osfandle(fileHandle)
       raiseOSError(osLastError(), filename)
   else:
+    # xxx we need a `proc toMode(a: FilePermission): Mode`, possibly by
+    # exposing fusion/filepermissions.fromFilePermissions to stdlib; then we need
+    # to expose a `perm` param so users can customize this (e.g. the temp file may
+    # need execute permissions), and figure out how to make the API cross platform.
+    let mode = Mode(S_IRUSR or S_IWUSR)
     let flags = posix.O_RDWR or posix.O_CREAT or posix.O_EXCL
-
-    let fileHandle = posix.open(filename, flags)
+    let fileHandle = posix.open(filename, flags, mode)
     if fileHandle == -1:
       raiseOSError(osLastError(), filename)
 
@@ -93,7 +101,6 @@ proc createTempFile*(prefix, suffix: string, dir = ""): tuple[fd: File, path: st
   ## If failing to create a temporary file, `IOError` will be raised.
   ##
   ## .. note:: It is the caller's responsibility to remove the file when no longer needed.
-  ##
   var dir = dir
   if dir.len == 0:
     dir = getTempDir()
;id=688c54d8f158ff977161a234374b1cd321ba95f3'>^
7e747d11c ^
ddc6cec69 ^

7e747d11c ^
ddc6cec69 ^



















































ddc6cec69 ^














9d2707960 ^
































1ed7751da ^

ddc6cec69 ^























b8a436af9 ^
ddc6cec69 ^






9d2707960 ^


ddc6cec69 ^









ddc6cec69 ^


















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233