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 00089 printf("Sending file...\n"); 00090 ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL, parent_id); 00091 printf("\n"); 00092 if (ret != 0) { 00093 printf("Error sending file.\n"); 00094 LIBMTP_Dump_Errorstack(device); 00095 LIBMTP_Clear_Errorstack(device); 00096 } else { 00097 printf("New file ID: %d\n", genfile->item_id); 00098 } 00099 00100 LIBMTP_destroy_file_t(genfile); 00101 00102 return 0; 00103 } 00104 00105 void sendfile_command (int argc, char **argv) { 00106 if (argc < 3) { 00107 sendfile_usage(); 00108 return; 00109 } 00110 sendfile_function(argv[1],argv[2]); 00111 }