Skip to content

Commit 1c56c1f

Browse files
committed
GNATCOLL.OS.FS.Open: truncate files in write mode
Change-Id: If5c1cc1019a9844db211191bb0c1d5e23b983803 TN: V330-031
1 parent 6c968fb commit 1c56c1f

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/os/gnatcoll-os-fs-open__unix.adb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- G N A T C O L L --
33
-- --
4-
-- Copyright (C) 2020-2021, AdaCore --
4+
-- Copyright (C) 2020-2022, AdaCore --
55
-- --
66
-- This library is free software; you can redistribute it and/or modify it --
77
-- under terms of the GNU General Public License as published by the Free --
@@ -48,7 +48,7 @@ begin
4848
O_Mode := O_RDONLY or O_CLOEXEC;
4949
Perm := 0;
5050
when Write_Mode =>
51-
O_Mode := O_WRONLY or O_CREAT or O_CLOEXEC;
51+
O_Mode := O_WRONLY or O_CREAT or O_CLOEXEC or O_TRUNC;
5252
Perm := S_IRUSR or S_IWUSR or S_IRGRP or
5353
S_IWGRP or S_IROTH or S_IWOTH;
5454
when Append_Mode =>

src/os/gnatcoll-os-fs-open__win32.adb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- G N A T C O L L --
33
-- --
4-
-- Copyright (C) 2020-2021, AdaCore --
4+
-- Copyright (C) 2020-2022, AdaCore --
55
-- --
66
-- This library is free software; you can redistribute it and/or modify it --
77
-- under terms of the GNU General Public License as published by the Free --
@@ -54,7 +54,8 @@ begin
5454
O_Mode := Win32.Files.O_WRONLY
5555
or Win32.Files.O_CREAT
5656
or Win32.Files.O_NOINHERIT
57-
or Win32.Files.O_BINARY;
57+
or Win32.Files.O_BINARY
58+
or Win32.Files.O_TRUNC;
5859
O_Perm := Win32.Files.S_IWRITE;
5960
when Append_Mode =>
6061
O_Mode := Win32.Files.O_WRONLY

testsuite/tests/os/fs/test.adb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,41 @@ begin
5757
end;
5858
Close (FD);
5959

60+
-- Check that opening a file in write mode truncates the file to size 0
61+
62+
declare
63+
Name : constant String := "non_empty_file";
64+
begin
65+
-- First create a file that contains several KB of data (just in case
66+
-- it makes a difference).
67+
68+
FD := Open (Name, Mode => Write_Mode);
69+
for C in Character range 'A' .. 'Z' loop
70+
for Dummy in 1 .. 10 loop
71+
Write (FD, (1 .. 80 => C));
72+
Write (FD, (1 => ASCII.LF));
73+
end loop;
74+
end loop;
75+
76+
Close (FD);
77+
78+
-- Then try to overwrite it
79+
80+
FD := Open (Name, Mode => Write_Mode);
81+
Write (FD, "not much here");
82+
Close (FD);
83+
84+
-- Now, check that it contains data from the last write session only
85+
86+
FD := Open (Name, Mode => Read_Mode);
87+
declare
88+
File_Content : constant String := Read (FD);
89+
begin
90+
A.Assert (File_Content, "not much here");
91+
end;
92+
Close (FD);
93+
end;
94+
6095
end;
6196
return A.Report;
6297
end Test;

0 commit comments

Comments
 (0)