From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: safe way to add contents to a file ? Date: Tue, 17 Dec 2019 22:59:15 -0500 Message-ID: References: <0FE61EAF-672C-4348-8107-F4C3D176FCF4@traduction-libre.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="81729"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 18 04:59:45 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1ihQUz-000L74-8k for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Dec 2019 04:59:45 +0100 Original-Received: from localhost ([::1]:49300 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihQUy-0006hv-47 for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Dec 2019 22:59:44 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53896) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihQUl-0006hp-NF for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 22:59:32 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihQUk-0007oA-M4 for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 22:59:31 -0500 Original-Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:44124 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihQUk-0007iR-EH for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 22:59:30 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1ihQUi-000Kq3-BJ for help-gnu-emacs@gnu.org; Wed, 18 Dec 2019 04:59:28 +0100 X-Injected-Via-Gmane: http://gmane.org/ Cancel-Lock: sha1:wqo4mScpJwTjYu9GWFFd+RmYtQs= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:122060 Archived-At: > 1) if APPEND is a number, that's the position from which my contents will > *overwrite* the rest of the file (I want to *insert* my contents) This is the way life works, it's not specific to Emacs: given the way files are typically represented on disk, there is simply no efficient way to *insert* into a file. > 2) there are weird things like the whole code where I call this function is > actually copied to the target file buffer and I have no idea how to > prevent that. I don't understand what you're describing, really, but from where I stand it sounds like you just had a weird bug in your code which made it do something you did not intend. > But I thought, it's not like such things don't happen all the time in emacs, > so there must be a better workflow. The normal workflow in Emacs is: - read the file into a buffer. - modify the buffer as you please (here, insertion is available and reasonably efficient). - save the buffer back into the file. The case you describe seems to fit this workflow perfectly and it'll be pretty close to optimal compared to what could theoretically be obtained from the POSIX API (my guess is that in the very worst case you'll be performing twice the minimum number of disk accesses). > But the reference is incredibly cryptic regarding that and the Intro does > not address any file i/o at all... Which part of which reference? What have you looked for? Given the structure of your problem, you have to read the file in order to find the precise place where you want to insert the new content and once you've read the file into a buffer, the rest seems to follow quite naturally. Stefan