lkml.org 
[lkml]   [2006]   [Mar]   [29]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    Date
    From
    SubjectRe: [PATCH][RFC] splice support
    On Wed, Mar 29 2006, Jens Axboe wrote:
    > splice-in file
    > Splice file to stdout
    >
    > splice-out
    > Splice stdin to file
    >
    > splice-net hostname port
    > Splice stdin to hostname:port
    >
    > Examples - splice copying a file can be done as:
    >
    > # splice-in file | splice-out new_file
    >
    > Sending a file over the network
    >
    > # cat file | splice-net hostname port
    >
    > and then have the other end run eg netcat -l -p port to receive the
    > data.

    Which I naturally forgot to attach, here they are.

    --
    Jens Axboe

    /*
    * Splice argument file to stdout
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>

    #if defined(__i386__)
    #define __NR_splice 313
    #elif defined(__x86_64__)
    #define __NR_splice 275
    #elif defined(__powerpc__) || defined(__powerpc64__)
    #define __NR_splice 283
    #else
    #error unsupported arch
    #endif

    static inline int splice(int fdin, int fdout, size_t len, unsigned long flags)
    {
    return syscall(__NR_splice, fdin, fdout, len, flags);
    }

    int main(int argc, char *argv[])
    {
    struct stat sb;
    int fd;

    if (argc < 2) {
    printf("%s: infile\n", argv[0]);
    return 1;
    }

    fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
    perror("open");
    return 1;
    }

    if (fstat(fd, &sb) < 0) {
    perror("stat");
    return 1;
    }

    do {
    int ret = splice(fd, STDOUT_FILENO, sb.st_size, 0);

    if (ret < 0) {
    perror("splice");
    break;
    } else if (!ret)
    break;

    sb.st_size -= ret;
    } while (1);

    close(fd);
    return 0;
    }
    /*
    * Splice stdout to file
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>

    #define SPLICE_SIZE (64*1024)

    #if defined(__i386__)
    #define __NR_splice 313
    #elif defined(__x86_64__)
    #define __NR_splice 275
    #elif defined(__powerpc__) || defined(__powerpc64__)
    #define __NR_splice 283
    #else
    #error unsupported arch
    #endif

    static inline int splice(int fdin, int fdout, size_t len, unsigned long flags)
    {
    return syscall(__NR_splice, fdin, fdout, len, flags);
    }

    int main(int argc, char *argv[])
    {
    int fd;

    if (argc < 2) {
    printf("%s: outfile\n", argv[0]);
    return 1;
    }

    fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) {
    perror("open");
    return 1;
    }

    do {
    int ret = splice(STDIN_FILENO, fd, SPLICE_SIZE, 0);

    if (ret < 0) {
    perror("splice");
    break;
    } else if (ret < SPLICE_SIZE)
    break;
    } while (1);

    close(fd);
    return 0;
    }
    /*
    * Splice stdin to net
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string.h>

    #define SPLICE_SIZE (64*1024)

    #if defined(__i386__)
    #define __NR_splice 313
    #elif defined(__x86_64__)
    #define __NR_splice 275
    #elif defined(__powerpc__) || defined(__powerpc64__)
    #define __NR_splice 283
    #else
    #error unsupported arch
    #endif

    static inline int splice(int fdin, int fdout, size_t len, unsigned long flags)
    {
    return syscall(__NR_splice, fdin, fdout, len, flags);
    }

    int main(int argc, char *argv[])
    {
    struct sockaddr_in addr;
    unsigned short port;
    int fd;

    if (argc < 3) {
    printf("%s: target port\n", argv[0]);
    return 1;
    }

    port = atoi(argv[2]);

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);

    if (inet_aton(argv[1], &addr.sin_addr) != 1) {
    struct hostent *hent = gethostbyname(argv[1]);

    if (!hent) {
    perror("gethostbyname");
    return 1;
    }

    memcpy(&addr.sin_addr, hent->h_addr, 4);
    }

    printf("Connecting to %s/%d\n", argv[1], port);

    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
    perror("socket");
    return 1;
    }

    if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    perror("connect");
    return 1;
    }

    do {
    int ret = splice(STDIN_FILENO, fd, SPLICE_SIZE, 0);

    if (ret < 0) {
    perror("splice");
    break;
    } else if (!ret) {
    break;
    }
    } while (1);

    close(fd);
    return 0;
    }
    \
     
     \ /
      Last update: 2006-03-29 14:32    [W:0.029 / U:0.772 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site