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