posix_io.h File Reference

POSIX-like I/O. More...

#include <stdint.h>
#include <gpxe/uaccess.h>

Go to the source code of this file.

Defines

#define POSIX_FD_MIN   ( 1 )
 Minimum file descriptor that will ever be allocated.
#define POSIX_FD_MAX   ( 31 )
 Maximum file descriptor that will ever be allocated.

Typedefs

typedef uint32_t fd_set
 File descriptor set as used for select().

Functions

 FILE_LICENCE (GPL2_OR_LATER)
int open (const char *uri_string)
 Open file.
ssize_t read_user (int fd, userptr_t buffer, off_t offset, size_t len)
 Read data from file.
int select (fd_set *readfds, int wait)
 Check file descriptors for readiness.
ssize_t fsize (int fd)
 Determine file size.
int close (int fd)
 Close file.
static __attribute__ ((always_inline)) void FD_ZERO(fd_set *set)
 Zero a file descriptor set.


Detailed Description

POSIX-like I/O.

Definition in file posix_io.h.


Define Documentation

#define POSIX_FD_MIN   ( 1 )

Minimum file descriptor that will ever be allocated.

Definition at line 16 of file posix_io.h.

Referenced by posix_find_free_fd(), and select().

#define POSIX_FD_MAX   ( 31 )

Maximum file descriptor that will ever be allocated.

Definition at line 19 of file posix_io.h.

Referenced by posix_find_free_fd(), and select().


Typedef Documentation

typedef uint32_t fd_set

File descriptor set as used for select().

Definition at line 22 of file posix_io.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

int open ( const char *  uri_string  ) 

Open file.

Parameters:
uri_string URI string
Return values:
fd File descriptor, or negative error number

Definition at line 187 of file posix_io.c.

References posix_file::data, DBG, EINPROGRESS, ENOMEM, posix_file::fd, refcnt::free, INIT_LIST_HEAD, posix_file::list, list_add, list_empty(), posix_file_finished(), posix_file_free(), posix_find_free_fd(), posix_file::rc, ref_put(), posix_file::refcnt, step(), posix_file::xfer, xfer_init(), xfer_open_uri_string(), and zalloc().

Referenced by int22(), and pxenv_file_open().

00187                                     {
00188         struct posix_file *file;
00189         int fd;
00190         int rc;
00191 
00192         /* Find a free file descriptor to use */
00193         fd = posix_find_free_fd();
00194         if ( fd < 0 )
00195                 return fd;
00196 
00197         /* Allocate and initialise structure */
00198         file = zalloc ( sizeof ( *file ) );
00199         if ( ! file )
00200                 return -ENOMEM;
00201         file->refcnt.free = posix_file_free;
00202         file->fd = fd;
00203         file->rc = -EINPROGRESS;
00204         xfer_init ( &file->xfer, &posix_file_xfer_operations,
00205                     &file->refcnt );
00206         INIT_LIST_HEAD ( &file->data );
00207 
00208         /* Open URI on data transfer interface */
00209         if ( ( rc = xfer_open_uri_string ( &file->xfer, uri_string ) ) != 0 )
00210                 goto err;
00211 
00212         /* Wait for open to succeed or fail */
00213         while ( list_empty ( &file->data ) ) {
00214                 step();
00215                 if ( file->rc == 0 )
00216                         break;
00217                 if ( file->rc != -EINPROGRESS ) {
00218                         rc = file->rc;
00219                         goto err;
00220                 }
00221         }
00222 
00223         /* Add to list of open files.  List takes reference ownership. */
00224         list_add ( &file->list, &posix_files );
00225         DBG ( "POSIX opened %s as file %d\n", uri_string, fd );
00226         return fd;
00227 
00228  err:
00229         posix_file_finished ( file, rc );
00230         ref_put ( &file->refcnt );
00231         return rc;
00232 }

ssize_t read_user ( int  fd,
userptr_t  buffer,
off_t  offset,
size_t  max_len 
)

Read data from file.

Parameters:
buffer Data buffer
offset Starting offset within data buffer
len Maximum length to read
Return values:
len Actual length read, or negative error number
This call is non-blocking; if no data is available to read then -EWOULDBLOCK will be returned.

Definition at line 277 of file posix_io.c.

References assert, copy_to_user(), io_buffer::data, posix_file::data, EBADF, EINPROGRESS, EWOULDBLOCK, free_iob(), iob_len(), iob_pull, io_buffer::list, list_del, list_empty(), list_for_each_entry, posix_file::pos, posix_fd_to_file(), posix_file::rc, and step().

Referenced by int22(), and pxenv_file_read().

00277                                                                              {
00278         struct posix_file *file;
00279         struct io_buffer *iobuf;
00280         size_t len;
00281 
00282         /* Identify file */
00283         file = posix_fd_to_file ( fd );
00284         if ( ! file )
00285                 return -EBADF;
00286 
00287         /* Try to fetch more data if none available */
00288         if ( list_empty ( &file->data ) )
00289                 step();
00290 
00291         /* Dequeue at most one received I/O buffer into user buffer */
00292         list_for_each_entry ( iobuf, &file->data, list ) {
00293                 len = iob_len ( iobuf );
00294                 if ( len > max_len )
00295                         len = max_len;
00296                 copy_to_user ( buffer, offset, iobuf->data, len );
00297                 iob_pull ( iobuf, len );
00298                 if ( ! iob_len ( iobuf ) ) {
00299                         list_del ( &iobuf->list );
00300                         free_iob ( iobuf );
00301                 }
00302                 file->pos += len;
00303                 assert ( len != 0 );
00304                 return len;
00305         }
00306 
00307         /* If file has completed, return (after returning all data) */
00308         if ( file->rc != -EINPROGRESS ) {
00309                 assert ( list_empty ( &file->data ) );
00310                 return file->rc;
00311         }
00312 
00313         /* No data ready and file still in progress; return -WOULDBLOCK */
00314         return -EWOULDBLOCK;
00315 }

int select ( fd_set readfds,
int  wait 
)

Check file descriptors for readiness.

Parameters:
readfds File descriptors to check
wait Wait until data is ready
Return values:
nready Number of ready file descriptors

Definition at line 241 of file posix_io.c.

References posix_file::data, EBADF, EINPROGRESS, posix_file::fd, list_empty(), POSIX_FD_MAX, POSIX_FD_MIN, posix_fd_to_file(), posix_file::rc, and step().

Referenced by int22(), and pxenv_file_select().

00241                                          {
00242         struct posix_file *file;
00243         int fd;
00244 
00245         do {
00246                 for ( fd = POSIX_FD_MIN ; fd <= POSIX_FD_MAX ; fd++ ) {
00247                         if ( ! FD_ISSET ( fd, readfds ) )
00248                                 continue;
00249                         file = posix_fd_to_file ( fd );
00250                         if ( ! file )
00251                                 return -EBADF;
00252                         if ( ( list_empty ( &file->data ) ) &&
00253                              ( file->rc == -EINPROGRESS ) )
00254                                 continue;
00255                         /* Data is available or status has changed */
00256                         FD_ZERO ( readfds );
00257                         FD_SET ( fd, readfds );
00258                         return 1;
00259                 }
00260                 step();
00261         } while ( wait );
00262 
00263         return 0;
00264 }

ssize_t fsize ( int  fd  ) 

Determine file size.

Parameters:
fd File descriptor
Return values:
size File size, or negative error number

Definition at line 323 of file posix_io.c.

References EBADF, posix_file::filesize, and posix_fd_to_file().

Referenced by int22(), and pxenv_get_file_size().

00323                          {
00324         struct posix_file *file;
00325 
00326         /* Identify file */
00327         file = posix_fd_to_file ( fd );
00328         if ( ! file )
00329                 return -EBADF;
00330 
00331         return file->filesize;
00332 }

int close ( int fd   ) 

Close file.

Parameters:
fd File descriptor
Return values:
rc Return status code

Definition at line 340 of file posix_io.c.

References EBADF, posix_file::list, list_del, posix_fd_to_file(), posix_file_finished(), ref_put(), and posix_file::refcnt.

Referenced by int22(), and pxenv_file_close().

00340                      {
00341         struct posix_file *file;
00342 
00343         /* Identify file */
00344         file = posix_fd_to_file ( fd );
00345         if ( ! file )
00346                 return -EBADF;
00347 
00348         /* Terminate data transfer */
00349         posix_file_finished ( file, 0 );
00350 
00351         /* Remove from list of open files and drop reference */
00352         list_del ( &file->list );
00353         ref_put ( &file->refcnt );
00354         return 0;
00355 }

static __attribute__ ( (always_inline)   )  [inline, static]

Zero a file descriptor set.

Set a bit within a file descriptor set.

Parameters:
set File descriptor set
fd File descriptor
set File descriptor set

Definition at line 36 of file posix_io.h.

00037                         {
00038         *set = 0;
00039 }


Generated on Tue Apr 6 20:01:52 2010 for gPXE by  doxygen 1.5.7.1