@@ -888,7 +888,7 @@ function Path:touch(opts)
888
888
889
889
if not not opts .parents then
890
890
local mode = type (opts .parents ) == " number" and opts .parents or nil --- @cast mode number ?
891
- _ = Path : new ( self :parent () ):mkdir { mode = mode , parents = true , exists_ok = true }
891
+ self :parent ():mkdir { mode = mode , parents = true , exists_ok = true }
892
892
end
893
893
894
894
local fd , err = uv .fs_open (self :absolute (), " w" , opts .mode )
@@ -1061,11 +1061,16 @@ function Path:_read_sync()
1061
1061
if data == nil then
1062
1062
error (err )
1063
1063
end
1064
+
1065
+ _ , err = uv .fs_close (fd )
1066
+ if err ~= nil then
1067
+ error (err )
1068
+ end
1064
1069
return data
1065
1070
end
1066
1071
1067
1072
--- @private
1068
- --- @param callback fun ( data : string ) callback to use for async version , nil for default
1073
+ --- @param callback fun ( data : string )
1069
1074
function Path :_read_async (callback )
1070
1075
uv .fs_open (self :absolute (), " r" , 438 , function (err_open , fd )
1071
1076
if err_open then
@@ -1081,7 +1086,12 @@ function Path:_read_async(callback)
1081
1086
if err_read or data == nil then
1082
1087
error (err_read )
1083
1088
end
1084
- callback (data )
1089
+ uv .fs_close (fd , function (err_close )
1090
+ if err_close then
1091
+ error (err_close )
1092
+ end
1093
+ callback (data )
1094
+ end )
1085
1095
end )
1086
1096
end )
1087
1097
end )
@@ -1201,6 +1211,38 @@ function Path:tail(lines)
1201
1211
return (table.concat (data ):gsub (" \n $" , " " ))
1202
1212
end
1203
1213
1214
+ --- write to file
1215
+ --- @param data string | string[] data to write
1216
+ --- @param flags uv.aliases.fs_access_flags | integer flag used to open file (eg. " w" or " a" )
1217
+ --- @param mode integer ? mode used to open file (default : ` 438` )
1218
+ --- @return number # bytes written
1219
+ function Path :write (data , flags , mode )
1220
+ vim .validate {
1221
+ txt = { data , { " s" , " t" } },
1222
+ flags = { flags , { " s" , " n" } },
1223
+ mode = { mode , " n" , true },
1224
+ }
1225
+
1226
+ mode = vim .F .if_nil (mode , 438 )
1227
+ local fd , err = uv .fs_open (self :absolute (), flags , mode )
1228
+ if fd == nil then
1229
+ error (err )
1230
+ end
1231
+
1232
+ local b
1233
+ b , err = uv .fs_write (fd , data , - 1 )
1234
+ if b == nil then
1235
+ error (err )
1236
+ end
1237
+
1238
+ _ , err = uv .fs_close (fd )
1239
+ if err ~= nil then
1240
+ error (err )
1241
+ end
1242
+
1243
+ return b
1244
+ end
1245
+
1204
1246
--- @param top_down boolean ? walk from current path down (default : ` true ` )
1205
1247
--- @return fun (): plenary.Path2 ?, string[] ?, string[] ? # iterator which yields (dirpath, dirnames, filenames)
1206
1248
function Path :walk (top_down )
@@ -1260,4 +1302,18 @@ function Path:walk(top_down)
1260
1302
end
1261
1303
end
1262
1304
1305
+ --[[
1306
+ Fail || Path2 write write string - windows (noshellslash)
1307
+ ./lua/plenary/path2.lua:896: EPERM: operation not permitted: C:\Users\jtrew\neovim\plenary.nvim\foobar
1308
+
1309
+ stack traceback:
1310
+ ...s/jtrew/neovim/plenary.nvim/tests/plenary/path2_spec.lua:859: in function <...s/jtrew/neovim/plenary.nvim/tests/plenary/path2_spec.lua:857>
1311
+
1312
+ ]]
1313
+
1314
+ vim .o .shellslash = false
1315
+ local p = Path :new " foobar"
1316
+ p :touch ()
1317
+ vim .o .shellslash = true
1318
+
1263
1319
return Path
0 commit comments