libmtp: sendfile.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# sendfile.c

    00001 #include <string.h>
    00002 #include <libgen.h>
    00003 #include <sys/stat.h>
    00004 #include <sys/types.h>
    00005 #include <fcntl.h>
    00006 #include "common.h"
    00007 #include "libmtp.h"
    00008 #include "pathutils.h"
    00009
    00010 extern LIBMTP_folder_t *folders;
    00011 extern LIBMTP_file_t *files;
    00012 extern LIBMTP_mtpdevice_t *device;
    00013
    00014 int sendfile_function(char *, char *);
    00015 void sendfile_command(int, char **);
    00016 void sendfile_usage(void);
    00017
    00018 void sendfile_usage(void)
    00019 {
    00020   fprintf(stderr, "usage: sendfile <local filename> <remote filename>\n");
    00021 }
    00022
    00023 int sendfile_function(char * from_path, char *to_path)
    00024 {
    00025   printf("Sending %s to %s\n",from_path,to_path);
    00026   char *filename;
    00027   uint64_t filesize;
    00028   struct stat sb;
    00029   LIBMTP_file_t *genfile;
    00030   int ret;
    00031   uint32_t parent_id = 0;
    00032
    00033   if ( stat(from_path, &sb) == -1 ) {
    00034     fprintf(stderr, "%s: ", from_path);
    00035     perror("stat");
    00036     exit(1);
    00037   }
    00038
    00039   filesize = (uint64_t) sb.st_size;
    00040
    00041   filename = basename(from_path);
    00042   parent_id = parse_path (to_path,files,folders);
    00043   if (parent_id == -1) {
    00044     printf("Parent folder could not be found, skipping\n");
    00045     return 0;
    00046   }
    00047
    00048
    00049   genfile = LIBMTP_new_file_t();
    00050   genfile->filesize = filesize;
    00051   genfile->filename = strdup(filename);
    00052   genfile->filetype = find_filetype (filename);
    00053
    00054   printf("Sending file...\n");
    00055   ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL, parent_id);
    00056
    00057   printf("\n");
    00058
    00059   LIBMTP_destroy_file_t(genfile);
    00060
    00061   return 0;
    00062 }
    00063
    00064 void sendfile_command (int argc, char **argv) {
    00065   if (argc < 3) {
    00066     sendfile_usage();
    00067     return;
    00068   }
    00069   sendfile_function(argv[1],argv[2]);
    00070 }