bulletmark / edir

Program to rename, remove, and copy files and directories using your editor
139 stars 8 forks source link

[Question] Motivation for changing temp file write/read in 515f2e5 #6

Closed krackers closed 3 years ago

krackers commented 3 years ago

This isn't a bug, I was just curious as to why in 515f2e5 you switched from doing

    with tempfile.NamedTemporaryFile('r+t', suffix=suffix) as fp:
        Path.writefile(fp)
        editfile(fp.name)
        Path.readfile(fp)

to

    with tempfile.TemporaryDirectory() as fdir:
        fpath = pathlib.Path(f'{fdir}/{PROG}-{os.getpid()}{suffix}')
        with fpath.open('w') as fp:
            Path.writefile(fp)
        editfile(fpath)
        with fpath.open() as fp:
            Path.readfile(fp)

The two seem basically equivalent to me, except for in the former you have to manually flush/seek in between the write/read. Was switching to the latter motivated just based on clarity, or is there an actual bug that motivated the changed?

bulletmark commented 3 years ago

They are not the same. Somebody with a Mac reported that edir did not work. The reason is because the editor he was using likely created a completely new file when it wrote the changed contents and thus edir must reopen the file a second time.

krackers commented 3 years ago

Ah that makes sense, thanks. I think vim is one of the editors that by default creates a new file and then swaps the links.

Edit: Seems that the defaults depend on the os. On linux backupcopy is yes whereas on osx it seems to be auto (which in most cases is no).

bulletmark commented 3 years ago

I use vim and on all the Linux machines I've used edir I never saw a problem. Apparently, the guy that had the problem on a Mac was using vim though.