Frank Heckenbach wrote:
Hi,
there's a strange case when using relative file names and changing the directory. (Incidentally I was made aware of this by a bugfix in the ncurses library (C code) some while ago ... ;-) Consider this program:
program Foo;
var i: Integer; f: Text;
begin Assign (f, 'test.dat'); Rewrite (f); WriteLn (f, 42); Close (f); ChDir ('..'); Reset (f); ReadLn (f, i); WriteLn (i) end.
It fails under both GPC and BP because `Reset' tries to open a file `test.dat' in a different directory than it was created.
Of course, this effect isn't limited to `Assign'. The issue applies to any way of storing file names, provided that changing directories is possible at all.
A solution would be to store the expanded file name (i.e., turn it into an absolute path whenever possible -- I currently don't know any case where it wouldn't be possible).
Fails if current directory is deleted.
Of course, this wouldn't be strictly BP compatible, but I wonder if this is a "good" or "bad" incompatibility. (Of course, the difference would be subtler and more confusing if there actually was a file `test.dat' in the new directory.) Also, clearly a new `Assign' statement after the change of directory would refer to the new directory, as would be expected.
I expect to be able to move (rename) a directory in which a programm is running without disruption, provided the program uses relatiove paths in subdirectories. Change to absolute paths would break such assumption. I personaly always did `Assign' just before `Reset' or `Rewrite' and expect most programs to do the same. IMHO opening a file should be an atomic operation and splitting opening into `Assign' and `Reset' is prone to races and unexpected side effects. Note that current semantic does give a warranty: file with simple name will be open in current directory. Proposed change introduces a race: file will be open in the directory which has the name current direcory had at `Assign' time. Even if program stays in the same directory the name may change.
In short: I think that only tiny fraction of programs will "benefit" and that slightly more programs will be broken. For me clearily benefits do not justify implementing the change.