#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Unhappy Sendfile function>> need some help


    Hi,
    could you please view my code and advise regarding my sendfile
    function.
    I've created 2 files : foo.txt & bar.txt.
    I'm tryng to copy the contence of foo.txt to bar.txt.
    from some reason it is not working ... :brrr:

    thanks in advanced

    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>  //used for exit()
    #include <string.h>
    
    #include <fcntl.h>
    #include <unistd.h>
    #include <assert.h>
    #include <error.h>
    
    #include <sys/sendfile.h>
    int main()
    {
    int in_fd, out_fd=0;
    char read_buffer[80];
    char write_buffer[]="HI There";
    //in_fd=open("foo.txt",O_RDWR |O_CREAT,0777) ;
    in_fd = open ("ss.txt",/* O_RDONLY */ O_RDWR  |O_CREAT,0775);
    
    if(in_fd < 0)
    {
    printf("error opening file: \n");
    exit(-1);
    }
    
    read (in_fd, read_buffer, sizeof (read_buffer));
    
    write (in_fd, write_buffer, strlen (write_buffer));
    
    printf("foo.txt contence is\n");
    
    system ("cat foo.txt");
    printf("\n");
    
    out_fd=open("bar.txt",O_RDWR |O_CREAT,0777);
    if(out_fd < 0)
    {
    printf("error opening file: \n");
    exit(-1);
    }
    read (out_fd, read_buffer, sizeof (read_buffer));
    sendfile(out_fd,in_fd,NULL,strlen (write_buffer));
    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    What version kernel do you have? According to the man page (http://linux.die.net/man/2/sendfile):
    In Linux kernels before 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33 it can be any file. If it is a regular file, then sendfile() changes the file offset appropriately
    Why are you doing those reads? That would change the offset into the files by 80. Which I guess is moot, since you've then changed in_fd's file pointer to right after where you had written the output. Again, from the man page:
    If offset is NULL, then data will be read from in_fd starting at the current file offset, and the file offset will be updated by the call.
    You would have to have changed the file pointer to point to the start of the string in write_buffer, if that was your intent.

    BTW, I had never even heard of sendfile before. All I know is what I read in the man pages.


    Also, the purpose of code tags is to preserve your code's indentation, thus keeping it readable. Nice that you used code tags from the start, but by not indenting your code you only defeat their purpose. Please, indent your code!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    186
    Rep Power
    82
    could you please view my code and advise regarding my sendfile
    Unfortunately, you have a lot of issues with your sendfile implementation. I've modified your code to provide a very basic implementation of this api call:

    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>  //used for exit()
    #include <string.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <assert.h>
    #include <error.h>
    #include <sys/sendfile.h>
    
    int main(void)
    {
        int in_fd, out_fd=0;
        char read_buffer[80];
        char write_buffer[]="HI There";
    
        struct stat stat_buf;
        off_t offset = 0;
    
    	//in_fd=open("foo.txt",O_RDWR |O_CREAT,0777) ;
    
        in_fd = open ("foo.txt", O_RDONLY);
    
        if(in_fd < 0)
        {
            printf("error opening file: \n");
            exit(-1);
        }
    
        /* Stat the input file to obtain its size.  */
        fstat (in_fd, &stat_buf);
    
    	//read (in_fd, read_buffer, sizeof (read_buffer));
    
        /* Open the output file for writing, with the same permissions as the
            source file.  */
        out_fd = open ("bar.txt", O_WRONLY | O_CREAT, stat_buf.st_mode);
    
        //    write (in_fd, write_buffer, strlen (write_buffer));
    
        /* Copy the bytes from one file to the other.  */
        sendfile (out_fd, in_fd, &offset, stat_buf.st_size);
    
        /* Close the files.  */
        close (in_fd);
        close (out_fd);
    
    	//   printf("foo.txt contence is\n");
        //  system ("cat foo.txt");
        //  printf("\n");
    	//   out_fd=open("bar.txt",O_RDWR |O_CREAT,0777);
        //  if(out_fd < 0)
        //  {
    	//       printf("error opening file: \n");
    	//       exit(-1);
        //  }
        //  read (out_fd, read_buffer, sizeof (read_buffer));
        //  sendfile(out_fd,in_fd,NULL,strlen (write_buffer));
        return 0;
    }
    Last edited by BobS0327; February 12th, 2013 at 12:04 PM.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    I'm sensing homework by successive approximation
    Take all the crumbs received on one forum, mash together and repost elsewhere. Repeat until someone succumbs and posts the complete answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo