sendfile.c

00001 
00023 #define _LARGEFILE_SOURCE
00024 #define _LARGEFILE64_SOURCE
00025 
00026 #include <string.h>
00027 #include <libgen.h>
00028 #include <sys/stat.h>
00029 #include <sys/types.h>
00030 #include <fcntl.h>
00031 #include "common.h"
00032 #include "libmtp.h"
00033 #include "pathutils.h"
00034 
00035 extern LIBMTP_folder_t *folders;
00036 extern LIBMTP_file_t *files;
00037 extern LIBMTP_mtpdevice_t *device;
00038 
00039 int sendfile_function(char *, char *);
00040 void sendfile_command(int, char **);
00041 void sendfile_usage(void);
00042 
00043 void sendfile_usage(void)
00044 {
00045   fprintf(stderr, "usage: sendfile <local filename> <remote filename>\n");
00046 }
00047 
00048 int sendfile_function(char * from_path, char *to_path)
00049 {
00050   printf("Sending %s to %s\n",from_path,to_path);
00051   char *filename;
00052   uint64_t filesize;
00053 #ifdef __USE_LARGEFILE64
00054   struct stat64 sb;
00055 #else
00056   struct stat sb;
00057 #endif
00058   LIBMTP_file_t *genfile;
00059   int ret;
00060   uint32_t parent_id = 0;
00061 
00062 #ifdef __USE_LARGEFILE64
00063   if ( stat64(from_path, &sb) == -1 ) {
00064 #else
00065   if ( stat(from_path, &sb) == -1 ) {
00066 #endif
00067     fprintf(stderr, "%s: ", from_path);
00068     perror("stat");
00069     exit(1);
00070   }
00071 
00072 #ifdef __USE_LARGEFILE64
00073   filesize = sb.st_size;
00074 #else
00075   filesize = (uint64_t) sb.st_size;
00076 #endif
00077   filename = basename(from_path);
00078   parent_id = parse_path (to_path,files,folders);
00079   if (parent_id == -1) {
00080     printf("Parent folder could not be found, skipping\n");
00081     return 0;
00082   }
00083   
00084   genfile = LIBMTP_new_file_t();
00085   genfile->filesize = filesize;
00086   genfile->filename = strdup(filename);
00087   genfile->filetype = find_filetype (filename);
00088   genfile->parent_id = 0;
00089   genfile->storage_id = 0;
00090 
00091   printf("Sending file...\n");
00092   ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL);
00093   printf("\n");
00094   if (ret != 0) {
00095     printf("Error sending file.\n");
00096     LIBMTP_Dump_Errorstack(device);
00097     LIBMTP_Clear_Errorstack(device);
00098   } else {
00099     printf("New file ID: %d\n", genfile->item_id);
00100   }
00101 
00102   LIBMTP_destroy_file_t(genfile);
00103 
00104   return 0;
00105 }
00106 
00107 void sendfile_command (int argc, char **argv) {
00108   if (argc < 3) {
00109     sendfile_usage();
00110     return;
00111   }
00112   sendfile_function(argv[1],argv[2]);
00113 }

Generated on Tue Apr 24 12:37:52 2012 for libmtp by  doxygen 1.4.7