9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* 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 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
* [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  6:46 [9fans] non-truncating create Joel “chesky” Salomon
2006-10-09  7:30 ` Steve Simon
  -- strict thread matches above, loose matches on Subject: below --
2006-10-09 22:38 G. David Butler
2006-10-09 23:36 ` Joel Salomon
2006-10-10  0:03 ` Russ Cox
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).