473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

POSIX Mistake: rewinddir returns no errors!

What is the best way to poll a directory for the arrival of files?

The OpenGroup
(http://www.opengroup.org/onlinepubs/...h/readdir.html)
says:

If a file is removed from or added to the directory after the most
recent call to opendir() or rewinddir(), whether a subsequent call to
readdir() returns an entry for that file is unspecified.

This would seem to identify rewinddir() as a reliable
synchronization point - a place where the system re-scans the directory on
the disk.

But rewinddir() doesn't return any error messages!

That would imply that no disk I/O is necessary.

This seems to me to be a specification bug. Is that correct?

A related question - what is the cost of an opendir()? If rewinddir()
really does check in with the disk, then what is the cost difference
between opendir() and rewinddir()?

Maybe I'll just use closedir()/opendir() to be safe (but that seems
unfortunate).
Feb 22 '07 #1
7 1769
unix_fan wrote:
What is the best way to poll a directory for the arrival of files?
The subject of this group is standard C which has no builtin concept
of "directories" and no portable way to monitor them. Please try
comp.unix.programmer or comp.programming.

Feb 22 '07 #2
unix_fan wrote, On 22/02/07 07:19:
What is the best way to poll a directory for the arrival of files?
<snip>

Using a POSIX specific method. You cannot do it in standard C which is
the topic of this group, fortunately for you there is
comp.unix.programmer for dealing with programming problems that are
specific to Unix.
--
Flash Gordon
Feb 22 '07 #3
On Feb 22, 2:19 am, unix_fan <tmell...@web.dewrote:
What is the best way to poll a directory for the arrival of files?

The OpenGroup
(http://www.opengroup.org/onlinepubs/...h/readdir.html)
says:

If a file is removed from or added to the directory after the most
recent call to opendir() or rewinddir(), whether a subsequent call to
readdir() returns an entry for that file is unspecified.

This would seem to identify rewinddir() as a reliable
synchronization point - a place where the system re-scans the directory on
the disk.
That is /your/ assumption, that the "system re-scans the directory on
disk".
Too bad your assumption is false-to-the-facts.
But rewinddir() doesn't return any error messages!
That would imply that no disk I/O is necessary.
Yes? So? Why would rewinddir() return any sort of error under these
circumstances? And why would (or should) it perform any sort of I/O?
rewinddir() simply resets it's position in the directory "stream" to
before the first entry That's all that is necessary to satisfy the
requirement, as the subsequent readdir() call will perform the actual
I/O and read the first dirent from the directory.
This seems to me to be a specification bug. Is that correct?
Not likely. More likely a bug in your understanding of what
rewinddir() does in order to accomplish the requirements of the
specification.
A related question - what is the cost of an opendir()? If rewinddir()
really does check in with the disk, then what is the cost difference
between opendir() and rewinddir()?
opendir() will perform a directory open, rewinddir() will not. Beyond
that, there is no "cost difference" as opendir() and rewinddir() does
the same thing: position to before the first directory entry, clear
all buffers, and prepare for the first readdir() call.
Maybe I'll just use closedir()/opendir() to be safe (but that seems
unfortunate).
Thats overkill for your misunderstanding of what opendir()/readdir()/
rewinddir()/closedir() do.

HTH
--
Lew
Feb 22 '07 #4
On 2007-02-22, unix_fan <tm******@web.dewrote:
If a file is removed from or added to the directory after the most
recent call to opendir() or rewinddir(), whether a subsequent call to
readdir() returns an entry for that file is unspecified.
This would seem to identify rewinddir() as a reliable
synchronization point - a place where the system re-scans the directory on
the disk.
But rewinddir() doesn't return any error messages!
Possible workaround (From the FreeBSD man page):

Since rewind() does not return a value, an application wishing to detect
errors should clear errno, then call rewind(), and if errno is
non-zero, assume an error has occurred.
That would imply that no disk I/O is necessary.
This seems to me to be a specification bug. Is that correct?
The manpage seems to indicate yes.
A related question - what is the cost of an opendir()?
Hard to say. Buffering implementations (most nowadays) might read up a
buffer full of DIRs ahead to save on syscalls on later readdirs. Some might
do it only on the first readdir.

Feb 22 '07 #5
Marco van de Voort <ma****@stack.nlwrites:
On 2007-02-22, unix_fan <tm******@web.dewrote:
> If a file is removed from or added to the directory after the most
recent call to opendir() or rewinddir(), whether a subsequent call to
readdir() returns an entry for that file is unspecified.
>This would seem to identify rewinddir() as a reliable
synchronization point - a place where the system re-scans the directory on
the disk.
>But rewinddir() doesn't return any error messages!

Possible workaround (From the FreeBSD man page):

Since rewind() does not return a value, an application wishing to detect
errors should clear errno, then call rewind(), and if errno is
non-zero, assume an error has occurred.
Of course rewind() and rewinddir() are two different functions. The
advice for rewind() might also apply to the non-standard rewinddir().

But it's not very good advice for rewind(). The standard says:

The rewind function sets the file position indicator for the
stream pointed to by stream to the beginning of the file. It is
equivalent to

(void)fseek(stream, 0L, SEEK_SET)

except that the error indicator for the stream is also cleared.

So if you want to do the equivalent of rewind() but with error
checking, just use fseek() instead.

There may not be an equivalent solution for rewinddir(). For more
information, try comp.unix.programmer -- where this entire discussion
should probably be.

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #6
Lew Pitcher wrote:
On Feb 22, 2:19 am, unix_fan <tmell...@web.dewrote:
.... snip ...
>
>Maybe I'll just use closedir()/opendir() to be safe (but that
seems unfortunate).

Thats overkill for your misunderstanding of what
opendir()/readdir()/rewinddir()/closedir() do.
There is no opendir/readir/rewinddir/closedir in the C language.
This is off-topic, and should not be in this news-group.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 23 '07 #7
In article <pa****************************@web.de>,
unix_fan <tm******@web.dewrote:
What is the best way to poll a directory for the arrival of files?

The OpenGroup
(http://www.opengroup.org/onlinepubs/...h/readdir.html)
says:

If a file is removed from or added to the directory after the most
recent call to opendir() or rewinddir(), whether a subsequent call to
readdir() returns an entry for that file is unspecified.

This would seem to identify rewinddir() as a reliable
synchronization point - a place where the system re-scans the directory on
the disk.
No it doesn't, it just has to reset its pointer to the beginning of the
directory. The above paragraph simply says that the system isn't
required to make a snapshot of the entire directory or lock the
directory -- if a change is made, you might or might not see it, and it
probably depends on how much buffering readdir() does.
>
But rewinddir() doesn't return any error messages!

That would imply that no disk I/O is necessary.
Right, it's just resetting a pointer. It's analogous to lseek(fd, 0,
SEEK_SET).
>
This seems to me to be a specification bug. Is that correct?

A related question - what is the cost of an opendir()? If rewinddir()
really does check in with the disk, then what is the cost difference
between opendir() and rewinddir()?
opendir() has to find the special file and verify that it's a directory.
Rewinddir() doesn't have to do any of this.
Maybe I'll just use closedir()/opendir() to be safe (but that seems
unfortunate).
The only benefit of that is if the the directory is renamed or deleted
and another directory given the original name. You'll get the new
directory instead of continuing to scan the old one.

--
Barry Margolin, ba****@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Feb 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
0
by: RN | last post by:
Hi, I don't know if this is the right place to ask this, but - what's the difference between posix and perl regular expressions? A good example is "aa|bb". Will this match "aa" or "ab" in...
11
by: FiLH | last post by:
Hello, I would like to know if posix semaphores are inter processes or just semaphores for threads inside the same process. I have not seen it defined in the posix specification I have found,...
6
by: darklupine | last post by:
I'm working on a project for a class at school. In order to complete the project, I have to create a new thread, but I'm not overly certain on how to do so. I have it coded, but it still throws an...
4
by: jensthiede | last post by:
My question is simple, but I guess the answer isn't. Why does the system() function on a POSIX system return the exit status of the called command as 2^8*n instead of n? Any good replies much...
8
by: Michele 'xjp' | last post by:
Hi there, I am using a POSIX-compliant system, Cygwin on Windows. I need the features of conio.h's getch() and clrscr(), but I can't compile programs with #include <conio.h(since conio.h is NOT...
7
by: CuTe_Engineer | last post by:
hii, can you tell me plzz why my programme doesn`t work ,i don`t have any errors and every thing is fine but i don`t know why it`s not working , soo plz can you help me un finding my mistake i...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.