zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: echo "true" > ~/.zshrc from ~/.zshrc
Date: Sat, 11 Jun 2016 11:05:01 -0700	[thread overview]
Message-ID: <160611110501.ZM12996@torch.brasslantern.com> (raw)
In-Reply-To: <CAKc7PVBsCerq5-qAG21De2fQq53MNFOOShjTYQV6eS5stBCGpQ@mail.gmail.com>

On Jun 11,  6:23pm, Sebastian Gniazdowski wrote:
}
} How legal it is to do:
} 
} echo "true" > ~/.zshrc
} 
} From ~/.zshrc? What can be expected?

See my earlier mention of how scripts are executed line by line as they
are read.  If you truncate ~/.zshrc this way, then the only parts of it
that will be executed are the parts that were already slurped into the
operating system I/O buffer before the file was truncated.

If you truncate the file and then write more bytes to it than had fit
in the I/O buffer (or more than were in the file in the first place),
then when the buffer is next filled (at some indeterminate future read
by the zsh parser) you're likely to get just part of what you wrote,
i.e., the tail after skipping the number of bytes that were previously
buffered.

Or you might just get an I/O error of some kind, it depends on the OS.

Further, if you truncate and write FEWER bytes than previously read,
you may end up with a file that has a whole bunch of NUL ('\0') butes
at the end to pad it out to the size it had before you tried to write
it while it was still being read.  This used to happen a lot with NFS
filesystems.

This isn't zsh specific, this can happen with any file that is written
while another process is reading it.  It's a bit more likely to do odd
things if the SAME process is doing both the reading and the writing,
though, which is the situation you've created.

If you want to do this kind of thing, write to a new file and then
rename it to ~/.zshrc to avoid opening the same file twice.


      parent reply	other threads:[~2016-06-11 18:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-11 16:23 Sebastian Gniazdowski
2016-06-11 16:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-06-11 17:40   ` Sebastian Gniazdowski
2016-06-11 18:05 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160611110501.ZM12996@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).