@@ -54,28 +54,41 @@ fn fix_windows_path(path string) string {
54
54
// open_file can be used to open or create a file with custom flags and permissions and returns a `File` object.
55
55
pub fn open_file (path string , mode string , options ...int) ? File {
56
56
mut flags := 0
57
+ mut seek_to_end := false
57
58
for m in mode {
58
59
match m {
59
- `w` { flags |= o_create | o_trunc }
60
- `a` { flags |= o_create | o_append }
61
- `r` { flags |= o_rdonly }
62
- `b` { flags |= o_binary }
63
- `s` { flags |= o_sync }
64
- `n` { flags |= o_nonblock }
65
- `c` { flags |= o_noctty }
66
- `+` { flags |= o_rdwr }
60
+ `w` {
61
+ flags |= o_create | o_trunc | o_wronly
62
+ }
63
+ `a` {
64
+ flags |= o_create | o_append | o_wronly
65
+ seek_to_end = true
66
+ }
67
+ `r` {
68
+ flags |= o_rdonly
69
+ }
70
+ `b` {
71
+ flags |= o_binary
72
+ }
73
+ `s` {
74
+ flags |= o_sync
75
+ }
76
+ `n` {
77
+ flags |= o_nonblock
78
+ }
79
+ `c` {
80
+ flags |= o_noctty
81
+ }
82
+ `+` {
83
+ flags &= ~ o_wronly
84
+ flags |= o_rdwr
85
+ }
67
86
else {}
68
87
}
69
88
}
70
89
if mode == 'r+' {
71
90
flags = o_rdwr
72
91
}
73
- if mode == 'w' {
74
- flags = o_wronly | o_create | o_trunc
75
- }
76
- if mode == 'a' {
77
- flags = o_wronly | o_create | o_append
78
- }
79
92
mut permission := 0o666
80
93
if options.len > 0 {
81
94
permission = options[0 ]
@@ -92,10 +105,19 @@ pub fn open_file(path string, mode string, options ...int) ?File {
92
105
if fd == - 1 {
93
106
return error (posix_get_error_msg (C.errno))
94
107
}
95
- cfile := C.fdopen (fd, & char (mode.str))
108
+ fdopen_mode := mode.replace ('b' , '' )
109
+ cfile := C.fdopen (fd, & char (fdopen_mode.str))
96
110
if isnil (cfile) {
97
111
return error ('Failed to open or create file "$path "' )
98
112
}
113
+ if seek_to_end {
114
+ // ensure appending will work, even on bsd/macos systems:
115
+ $if windows {
116
+ C._fseeki64 (cfile, 0 , C.SEEK_END)
117
+ } $else {
118
+ C.fseeko (cfile, 0 , C.SEEK_END)
119
+ }
120
+ }
99
121
return File{
100
122
cfile: cfile
101
123
fd: fd
0 commit comments