26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Advanced topics<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

Directory Function File Function Message (resmgr)<br />

closedir() close() _IO_CLOSE_DUP<br />

opendir() open() _IO_CONNECT<br />

readdir() read() _IO_READ<br />

rewinddir() lseek() _IO_LSEEK<br />

seekdir() lseek() _IO_LSEEK<br />

telldir() tell() _IO_LSEEK<br />

If we assume for a moment that the opendir() and closedir() functions will be handled<br />

automatically for us, we can focus on just the _IO_READ and _IO_LSEEK messages<br />

and related functions.<br />

Offsets<br />

The _IO_LSEEK message and related function is used to “seek” (or “move”) <strong>with</strong>in a<br />

file. It does the exact same thing <strong>with</strong>in a directory; you can move to the “first”<br />

directory entry (by explicitly giving an offset to seekdir() or by calling rewinddir()), or<br />

any arbitrary entry (by using seekdir()), or you can find out the current location in the<br />

directory entry list (by using telldir()).<br />

The “trick” <strong>with</strong> directories, however, is that the seek offsets are entirely up to you to<br />

define and manage. This means that you may decide to call your directory entry<br />

offsets “0,” “1,” “2” and so on, or you may instead call them “0,” “64,” “128” and so<br />

on. The only important thing here is that the offsets must be consistent in both the<br />

io_lseek() handler as well as the io_read() handler functions.<br />

In the example below, we’ll assume that we’re using the simple “0,” “1,” “2” . . .<br />

approach. (You might use the “0,” “64,” “128” . . . approach if those numbers<br />

correspond to, for example, some kind of on-media offsets. Your choice.)<br />

Contents<br />

So now all that’s left is to “simply” fill in the struct dirent <strong>with</strong> the “contents” of<br />

our directory. Here’s what the struct dirent looks like (from ):<br />

struct dirent {<br />

ino_t d_ino;<br />

off_t d_offset;<br />

uint16_t d_reclen;<br />

uint16_t d_namelen;<br />

char d_name [1];<br />

};<br />

Here’s a quick explanation of the various members:<br />

d_ino<br />

The “inode” — a mountpoint-unique serial number that cannot be<br />

zero (zero traditionally indicates that the entry corresponding to this<br />

inode is free/empty).<br />

274 Chapter 5 • Resource Managers April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!