Back to index

php5  5.3.10
Defines | Functions | Variables
compress.c File Reference
#include "config.h"
#include "file.h"
#include "magic.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>

Go to the source code of this file.

Defines

#define NODATA   ((size_t)~0)

Functions

private ssize_t swrite (int, const void *, size_t)
protected ssize_t sread (int fd, void *buf, size_t n, int canbepipe)
protected int file_pipe2file (struct magic_set *ms, int fd, const void *startbuf, size_t nbytes)

Variables

struct {
const char magic [8]
size_t maglen
const char * argv [3]
int silent
compr []

Define Documentation

#define NODATA   ((size_t)~0)

Definition at line 89 of file compress.c.


Function Documentation

protected int file_pipe2file ( struct magic_set ms,
int  fd,
const void *  startbuf,
size_t  nbytes 
)

Definition at line 244 of file compress.c.

{
       char buf[4096];
       int r, tfd;

       (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf);
#ifndef HAVE_MKSTEMP
       {
              char *ptr = mktemp(buf);
              tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600);
              r = errno;
              (void)unlink(ptr);
              errno = r;
       }
#else
       tfd = mkstemp(buf);
       r = errno;
       (void)unlink(buf);
       errno = r;
#endif
       if (tfd == -1) {
              file_error(ms, errno,
                  "cannot create temporary file for pipe copy");
              return -1;
       }

       if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes)
              r = 1;
       else {
              while ((r = sread(fd, buf, sizeof(buf), 1)) > 0)
                     if (swrite(tfd, buf, (size_t)r) != r)
                            break;
       }

       switch (r) {
       case -1:
              file_error(ms, errno, "error copying from pipe to temp file");
              return -1;
       case 0:
              break;
       default:
              file_error(ms, errno, "error while writing to temp file");
              return -1;
       }

       /*
        * We duplicate the file descriptor, because fclose on a
        * tmpfile will delete the file, but any open descriptors
        * can still access the phantom inode.
        */
       if ((fd = dup2(tfd, fd)) == -1) {
              file_error(ms, errno, "could not dup descriptor for temp file");
              return -1;
       }
       (void)close(tfd);
       if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
              file_badseek(ms);
              return -1;
       }
       return fd;
}

Here is the call graph for this function:

protected ssize_t sread ( int  fd,
void *  buf,
size_t  n,
int  canbepipe 
)

Definition at line 179 of file compress.c.

{
       int rv;
#ifdef FIONREAD
       int t = 0;
#endif
       size_t rn = n;

       if (fd == STDIN_FILENO)
              goto nocheck;

#ifdef FIONREAD
       if ((canbepipe && (ioctl(fd, FIONREAD, &t) == -1)) || (t == 0)) {
#ifdef FD_ZERO
              int cnt;
              for (cnt = 0;; cnt++) {
                     fd_set check;
                     struct timeval tout = {0, 100 * 1000};
                     int selrv;

                     FD_ZERO(&check);
                     FD_SET(fd, &check);

                     /*
                      * Avoid soft deadlock: do not read if there
                      * is nothing to read from sockets and pipes.
                      */
                     selrv = select(fd + 1, &check, NULL, NULL, &tout);
                     if (selrv == -1) {
                            if (errno == EINTR || errno == EAGAIN)
                                   continue;
                     } else if (selrv == 0 && cnt >= 5) {
                            return 0;
                     } else
                            break;
              }
#endif
              (void)ioctl(fd, FIONREAD, &t);
       }

       if (t > 0 && (size_t)t < n) {
              n = t;
              rn = n;
       }
#endif

nocheck:
       do
              switch ((rv = read(fd, buf, n))) {
              case -1:
                     if (errno == EINTR)
                            continue;
                     return -1;
              case 0:
                     return rn - n;
              default:
                     n -= rv;
                     buf = ((char *)buf) + rv;
                     break;
              }
       while (n > 0);
       return rn;
}

Here is the call graph for this function:

Here is the caller graph for this function:

private ssize_t swrite ( int  fd,
const void *  buf,
size_t  n 
)

Definition at line 154 of file compress.c.

{
       int rv;
       size_t rn = n;

       do
              switch (rv = write(fd, buf, n)) {
              case -1:
                     if (errno == EINTR)
                            continue;
                     return -1;
              default:
                     n -= rv;
                     buf = ((const char *)buf) + rv;
                     break;
              }
       while (n > 0);
       return rn;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

private { ... } compr[]