9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re:  [9fans] non-truncating create
@ 2006-10-09 22:38 G. David Butler
  2006-10-09 23:36 ` Joel Salomon
  2006-10-10  0:03 ` Russ Cox
  0 siblings, 2 replies; 13+ messages in thread
From: G. David Butler @ 2006-10-09 22:38 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

And if you are interested in a much deeper discussion, look at the archives from 1999.

>-----Original Message-----
>From: Russ Cox [mailto:rsc@swtch.com]
>Sent: Monday, October 9, 2006 10:57 AM
>To: 'Fans of the OS Plan 9 from Bell Labs'
>Subject: Re: [9fans] non-truncating create
>
>Rc says something like:
>
>if ((fd = open(file, OWRITE)) < 0 && (fd = create(file, OWRITE, 0666)) < 0) {
>    complain(...);
>    return;
>}
>seek(fd, 0, 2);
>
>You don't really mean ~0 as the argument to create.
>
>Russ
>
>
>On 10/9/06, Joel "chesky" Salomon <JoelCSalomon@gmail.com> wrote:
>> > Rc just tries the open, and if that fails, tries the create.
>> > It doesn't even bother looking at the error code.
>> > If the open failed due to permissions, so will the create.
>>
>> Drawterm barfed on me and I lost the bit of code this suggested; when
>> I tried to recreate it I ended up checking for OTRUNC a few times.
>> Not pretty.
>>
>> > Or use create with OEXCL if you really care.
>>
>> Sort of the way create(2) calls create(5) first, then open(5) if that
>> failed?  My code now looks like:
>>         o = create(oname, omode|OEXCL, ~0);     /* fails if file exists */
>>         if(o < 0)
>>                 o = open(oname, omode);
>>         if(o < 0){
>>                 complain("can't open %s for writing: %r", oname);
>>                 return;
>>         }
>> which has no special-case code depending on OTRUNC being set or no.
>>
>> I suppose I ought to look at /sys/src/cmd/rc to see how it's really
>> done.  Mañana.
>>
>> Thanks for the assist.
>>
>> --Joel
>>
>>
>




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09 22:38 [9fans] non-truncating create G. David Butler
@ 2006-10-09 23:36 ` Joel Salomon
  2006-10-10  0:03 ` Russ Cox
  1 sibling, 0 replies; 13+ messages in thread
From: Joel Salomon @ 2006-10-09 23:36 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 10/9/06, G. David Butler <gdb@dbsystems.com> wrote:
> And if you are interested in a much deeper discussion, look at the archives from 1999.

Looking at the subject headers at <http://9fans.net/archive/1999>, I'm
not seeing anything directly related to open or create.  Could you
please be a bit more specific?

--Joel


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09 22:38 [9fans] non-truncating create G. David Butler
  2006-10-09 23:36 ` Joel Salomon
@ 2006-10-10  0:03 ` Russ Cox
  1 sibling, 0 replies; 13+ messages in thread
From: Russ Cox @ 2006-10-10  0:03 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> And if you are interested in a much deeper discussion,
> look at the archives from 1999.

Actually it was 1998: http://9fans.net/archive/1998/02
especially http://9fans.net/archive/1998/02/17

We explicitly considered this when working on 9P2000
and added the OEXCL flag to create to address this problem.

Russ


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09  6:09   ` Joel “chesky” Salomon
@ 2006-10-09 15:57     ` Russ Cox
  0 siblings, 0 replies; 13+ messages in thread
From: Russ Cox @ 2006-10-09 15:57 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Rc says something like:

if ((fd = open(file, OWRITE)) < 0 && (fd = create(file, OWRITE, 0666)) < 0) {
    complain(...);
    return;
}
seek(fd, 0, 2);

You don't really mean ~0 as the argument to create.

Russ


On 10/9/06, Joel "chesky" Salomon <JoelCSalomon@gmail.com> wrote:
> > Rc just tries the open, and if that fails, tries the create.
> > It doesn't even bother looking at the error code.
> > If the open failed due to permissions, so will the create.
>
> Drawterm barfed on me and I lost the bit of code this suggested; when
> I tried to recreate it I ended up checking for OTRUNC a few times.
> Not pretty.
>
> > Or use create with OEXCL if you really care.
>
> Sort of the way create(2) calls create(5) first, then open(5) if that
> failed?  My code now looks like:
>         o = create(oname, omode|OEXCL, ~0);     /* fails if file exists */
>         if(o < 0)
>                 o = open(oname, omode);
>         if(o < 0){
>                 complain("can't open %s for writing: %r", oname);
>                 return;
>         }
> which has no special-case code depending on OTRUNC being set or no.
>
> I suppose I ought to look at /sys/src/cmd/rc to see how it's really
> done.  Mañana.
>
> Thanks for the assist.
>
> --Joel
>
>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09  6:46 Joel “chesky” Salomon
@ 2006-10-09  7:30 ` Steve Simon
  0 siblings, 0 replies; 13+ messages in thread
From: Steve Simon @ 2006-10-09  7:30 UTC (permalink / raw)
  To: 9fans

> Probably not.  The interesting part of sysfatal is the
> terminating the program, not the printing of the error.

I believe this depends upon the life expectancy of the
program, I have written a shell for another OS and found
syswarn() quite "handy"; this OS didn't have %r though. 

-Steve


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
@ 2006-10-09  6:46 Joel “chesky” Salomon
  2006-10-09  7:30 ` Steve Simon
  0 siblings, 1 reply; 13+ messages in thread
From: Joel “chesky” Salomon @ 2006-10-09  6:46 UTC (permalink / raw)
  To: 9fans

> > Of tertiary interest: Is there a general use for a library function
> > complain() that behaves identically to sysfatal(2) except for not
> > terminating the program?
> 
> Probably not.  The interesting part of sysfatal is the
> terminating the program, not the printing of the error.

Got it.  Complain() goes into my private library, together with emalloc & co. from back in June... ☺

--Joel



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09  4:00 ` Russ Cox
@ 2006-10-09  6:09   ` Joel “chesky” Salomon
  2006-10-09 15:57     ` Russ Cox
  0 siblings, 1 reply; 13+ messages in thread
From: Joel “chesky” Salomon @ 2006-10-09  6:09 UTC (permalink / raw)
  To: 9fans

> Rc just tries the open, and if that fails, tries the create.
> It doesn't even bother looking at the error code.
> If the open failed due to permissions, so will the create.

Drawterm barfed on me and I lost the bit of code this suggested; when
I tried to recreate it I ended up checking for OTRUNC a few times.
Not pretty.

> Or use create with OEXCL if you really care.

Sort of the way create(2) calls create(5) first, then open(5) if that
failed?  My code now looks like:
	o = create(oname, omode|OEXCL, ~0);	/* fails if file exists */
	if(o < 0)
		o = open(oname, omode);
	if(o < 0){
		complain("can’t open %s for writing: %r", oname);
		return;
	}
which has no special-case code depending on OTRUNC being set or no.

I suppose I ought to look at /sys/src/cmd/rc to see how it’s really
done.  Mañana.

Thanks for the assist.

--Joel



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09  4:01 ` Felipe Bichued
@ 2006-10-09  5:39   ` Joel Salomon
  0 siblings, 0 replies; 13+ messages in thread
From: Joel Salomon @ 2006-10-09  5:39 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 10/9/06, Felipe Bichued <bichued@gmail.com> wrote:
> You might want to read this: http://lsub.org/who/nemo/9.intro.pdf

It's a great resource; I've been referring to it extensively.

BTW: Thanks, nemo!

--Joel


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09  3:13 Joel “chesky” Salomon
  2006-10-09  4:00 ` Russ Cox
@ 2006-10-09  4:01 ` Felipe Bichued
  2006-10-09  5:39   ` Joel Salomon
  1 sibling, 1 reply; 13+ messages in thread
From: Felipe Bichued @ 2006-10-09  4:01 UTC (permalink / raw)
  To: 9fans

Hello,

You might want to read this: http://lsub.org/who/nemo/9.intro.pdf

Regards.

On 10/9/06, Joel "chesky" Salomon <JoelCSalomon@gmail.com> wrote:
> Still for the Brain-Dead SHell homework assignment...
>
> I'm trying to implement output redirection, > and >>.  Truncating
> redirect (>) is easy; just use create(2) whether the file exists or
> no.  The non-truncing option is puzzling me, though.  If the file
> exists, I need to o = open(oname, OWRITE), then seek(o, 0, 2); if the
> file does not exist I need to create it.
>
> How do I check for the existence of a file?  I'm thinking about the
> following code snippet:
>         o = open(oname, OWRITE)
>         rerrstr(errbuf, ERRMAX);
>         if((strstr(errbuf, "file does not exist") == 0){
>                 complain("cannot open %s: %r", oname);
>                 return;
>         }else   o = create(oname, OWRITE, ~0);
>         if(o < 0)
>                 ...
> Is there a cleaner way to accomplish this?  Some form of stat(2) that has an explicit  "file does not exist" code outside parsing errstr
>
> Of secondary interest: Not that this is production code, but should I
> be concerned about race conditions where the file is created by
> somebody else between the failed open(2) and the create(2)?
>
> Of tertiary interest: Is there a general use for a library function
> complain() that behaves identically to sysfatal(2) except for not
> terminating the program?
>
>
> --Joel
>
>


-- 
Felipe


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9fans] non-truncating create
  2006-10-09  3:13 Joel “chesky” Salomon
@ 2006-10-09  4:00 ` Russ Cox
  2006-10-09  6:09   ` Joel “chesky” Salomon
  2006-10-09  4:01 ` Felipe Bichued
  1 sibling, 1 reply; 13+ messages in thread
From: Russ Cox @ 2006-10-09  4:00 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> I'm trying to implement output redirection, > and >>.  Truncating
> redirect (>) is easy; just use create(2) whether the file exists or
> no.  The non-truncing option is puzzling me, though.  If the file
> exists, I need to o = open(oname, OWRITE), then seek(o, 0, 2); if the
> file does not exist I need to create it.
>
> How do I check for the existence of a file?  I'm thinking about the
> following code snippet:

Rc just tries the open, and if that fails, tries the create.
It doesn't even bother looking at the error code.
If the open failed due to permissions, so will the create.
Or use create with OEXCL if you really care.

> Of secondary interest: Not that this is production code, but should I
> be concerned about race conditions where the file is created by
> somebody else between the failed open(2) and the create(2)?

Nope.  This is Plan 9.  Everyone has private /tmp directories.

> Of tertiary interest: Is there a general use for a library function
> complain() that behaves identically to sysfatal(2) except for not
> terminating the program?

Probably not.  The interesting part of sysfatal is the
terminating the program, not the printing of the error.

Russ


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [9fans] non-truncating create
@ 2006-10-09  3:13 Joel “chesky” Salomon
  0 siblings, 0 replies; 13+ messages in thread
From: Joel “chesky” Salomon @ 2006-10-09  3:13 UTC (permalink / raw)
  To: 9fans

Still for the Brain-Dead SHell homework assignment...

I’m trying to implement output redirection, > and >>.  Truncating
redirect (>) is easy; just use create(2) whether the file exists or
no.  The non-truncing option is puzzling me, though.  If the file
exists, I need to o = open(oname, OWRITE), then seek(o, 0, 2); if the
file does not exist I need to create it.

How do I check for the existence of a file?  I’m thinking about the
following code snippet:
	o = open(oname, OWRITE)
	rerrstr(errbuf, ERRMAX);
	if((strstr(errbuf, "file does not exist") == 0){
		complain("cannot open %s: %r", oname);
		return;
	}else	o = create(oname, OWRITE, ~0);
	if(o < 0)
		...
Is there a cleaner way to accomplish this?  Some form of stat(2) that
has an explicit “file does not exist” code outside parsing errstr

Of secondary interest: Not that this is production code, but should I
be concerned about race conditions where the file is created by
somebody else between the failed open(2) and the create(2)?

Of tertiary interest: Is there a general use for a library function
complain() that behaves identically to sysfatal(2) except for not
terminating the program?


--Joel



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [9fans] non-truncating create
@ 2006-10-09  3:13 Joel “chesky” Salomon
  0 siblings, 0 replies; 13+ messages in thread
From: Joel “chesky” Salomon @ 2006-10-09  3:13 UTC (permalink / raw)
  To: 9fans

Still for the Brain-Dead SHell homework assignment...

I’m trying to implement output redirection, > and >>.  Truncating
redirect (>) is easy; just use create(2) whether the file exists or
no.  The non-truncing option is puzzling me, though.  If the file
exists, I need to o = open(oname, OWRITE), then seek(o, 0, 2); if the
file does not exist I need to create it.

How do I check for the existence of a file?  I’m thinking about the
following code snippet:
	o = open(oname, OWRITE)
	rerrstr(errbuf, ERRMAX);
	if((strstr(errbuf, "file does not exist") == 0){
		complain("cannot open %s: %r", oname);
		return;
	}else	o = create(oname, OWRITE, ~0);
	if(o < 0)
		...
Is there a cleaner way to accomplish this?  Some form of stat(2) that
has an explicit “file does not exist” code outside parsing errstr

Of secondary interest: Not that this is production code, but should I
be concerned about race conditions where the file is created by
somebody else between the failed open(2) and the create(2)?

Of tertiary interest: Is there a general use for a library function
complain() that behaves identically to sysfatal(2) except for not
terminating the program?


--Joel



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [9fans] non-truncating create
@ 2006-10-09  3:13 Joel “chesky” Salomon
  2006-10-09  4:00 ` Russ Cox
  2006-10-09  4:01 ` Felipe Bichued
  0 siblings, 2 replies; 13+ messages in thread
From: Joel “chesky” Salomon @ 2006-10-09  3:13 UTC (permalink / raw)
  To: 9fans

Still for the Brain-Dead SHell homework assignment...

I’m trying to implement output redirection, > and >>.  Truncating
redirect (>) is easy; just use create(2) whether the file exists or
no.  The non-truncing option is puzzling me, though.  If the file
exists, I need to o = open(oname, OWRITE), then seek(o, 0, 2); if the
file does not exist I need to create it.

How do I check for the existence of a file?  I’m thinking about the
following code snippet:
	o = open(oname, OWRITE)
	rerrstr(errbuf, ERRMAX);
	if((strstr(errbuf, "file does not exist") == 0){
		complain("cannot open %s: %r", oname);
		return;
	}else	o = create(oname, OWRITE, ~0);
	if(o < 0)
		...
Is there a cleaner way to accomplish this?  Some form of stat(2) that has an explicit  “file does not exist” code outside parsing errstr

Of secondary interest: Not that this is production code, but should I
be concerned about race conditions where the file is created by
somebody else between the failed open(2) and the create(2)?

Of tertiary interest: Is there a general use for a library function
complain() that behaves identically to sysfatal(2) except for not
terminating the program?


--Joel



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2006-10-10  0:03 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-09 22:38 [9fans] non-truncating create G. David Butler
2006-10-09 23:36 ` Joel Salomon
2006-10-10  0:03 ` Russ Cox
  -- strict thread matches above, loose matches on Subject: below --
2006-10-09  6:46 Joel “chesky” Salomon
2006-10-09  7:30 ` Steve Simon
2006-10-09  3:13 Joel “chesky” Salomon
2006-10-09  3:13 Joel “chesky” Salomon
2006-10-09  3:13 Joel “chesky” Salomon
2006-10-09  4:00 ` Russ Cox
2006-10-09  6:09   ` Joel “chesky” Salomon
2006-10-09 15:57     ` Russ Cox
2006-10-09  4:01 ` Felipe Bichued
2006-10-09  5:39   ` Joel Salomon

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).