Deutsch   English   Français   Italiano  
<q6CdnW6It640ug76nZ2dnZfqn_GdnZ2d@earthlink.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!feeder3.eternal-september.org!border-3.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 24 Jan 2025 05:39:53 +0000
Subject: Re: Tentative File Open & Safe Save
Newsgroups: comp.os.linux.misc
References: <vmungm$1sjgj$1@dont-email.me>
From: "186282@ud0s4.net" <186283@ud0s4.net>
Organization: wokiesux
Date: Fri, 24 Jan 2025 00:39:54 -0500
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
 Thunderbird/78.13.0
MIME-Version: 1.0
In-Reply-To: <vmungm$1sjgj$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Message-ID: <q6CdnW6It640ug76nZ2dnZfqn_GdnZ2d@earthlink.com>
Lines: 69
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 99.101.150.97
X-Trace: sv3-vAiGdeWpgcIN2y7LFvkwbpS+AbZPtemAB07nui6J3u0hy2qIKw/7pM5b6c2UGKXbQi4iJTyesoJhBJ+!5aSkk8dPJl9ltSgFIAolcDVLjOFybOwkS7KzEs5MRIHeIdt0osfjSlzsA8WF6fPG25hS4P9ft8pr!+xCfKqvcvh310ZNKTu/w
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40

On 1/23/25 7:39 PM, Lawrence D'Oliveiro wrote:
> When developing an app, saving changes that a user has made to a document
> needs to be managed carefully. Simply overwriting the existing file with
> the new data can cause trouble, if your app (or the system) should crash
> part-way through, because then the file ends up with some part of the old
> document overwritten with the new one, and so the user ends up without a
> valid copy of either the old or the new version -- in effect, all their
> work is lost.
> 
> A better technique is to rename any existing version of the file (e.g.
> appending a suffix such as “-old”) before saving the new document under
> the original file name. After the successful save of the new document, the
> old version might or might not be deleted.


   This has become pretty standard. Word and Excel tend
   to create (and forget) lots of such temp files.


> Alternatively, you might save the new document under the original file
> name but with some temporary suffix, e.g. “-new”, added. Then use the
> Linux RENAME_EXCHANGE option to the rename_at(2) call to simultaneously
> rename each file to the other name -- exchanging the names of the new and
> old files. After this, you can delete the file with the name ending in
> “-new”, since this is now the old version.
> 
> Another technique is to do “tentative” file creation. If you open a file
> with the O_TMPFILE option, then no entry is made in any directory; space
> is allocated on the destination volume, but if the process terminates for
> any reason without taking action to make the file permanent, it simply
> disappears from the filesystem (and any space it was using is reclaimed).

   Fair, but you CAN lose minor edits.

> Making the file permanent involves giving it an explicit name within the
> destination filesystem. This is done with a linkat(2) call. But this call
> requires an existing name to be linked to a new name; how do you specify
> the existing name when, by design the file doesn’t have one?
> 
> In fact, Linux gives it a name, by a mechanism called a “magic symlink”.
> If you look in /proc/«pid»/fd for a given process, it will show symlinks
> to the files the process has open. For a file opened with the O_TMPFILE
> option, this name can be used in a linkat(2) call to give the file a
> “real” name -- i.e. one that exists in the regular filesystem.
> 
> Some example C code that shows how to do this linking is on the openat(2)
> man page <https://manpages.debian.org/openat(2)>. I implemented a Python
> version of this code in the save_tmpfile() routine in the linuxfs module
> here <c>.
> 
> For an example program that uses this module to demonstrate various of the
> above options, see the safe_save script here
> <https://gitlab.com/ldo/python_linuxfs_examples/>.

   Good ... but maybe a little more complicated than
   usually required. Just making .tmp1, .tmp2 files
   before renaming has always been good enough
   for me.

   Of course systems CAN glitch at any time, often for
   totally mysterious reasons - power maybe, minor
   coding error only hit 1:1000 times, cosmic rays ....
   so if yer stuff is SUPER important, like tax docs
   or whatever .......

   Of late I've been trying to find the wisdom of
   people who do systems for deep space probes -
   where cosmic glitches become a very real issue.