libmtp 1.1.23

sendfile.c
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "common.h"
#include "libmtp.h"
#include "pathutils.h"
#include "util.h"
#include "connect.h"
extern LIBMTP_folder_t *folders;
extern LIBMTP_file_t *files;
extern LIBMTP_mtpdevice_t *device;
void sendfile_usage(void)
{
fprintf(stderr, "usage: sendfile <local filename> <remote filename>\n");
}
int sendfile_function(char * from_path, char *to_path)
{
printf("Sending %s to %s\n",from_path,to_path);
char *filename;
char *from_path_for_filename;
uint64_t filesize;
struct stat sb;
LIBMTP_file_t *genfile;
int ret;
uint32_t parent_id = 0;
if ( stat(from_path, &sb) == -1 ) {
fprintf(stderr, "%s: ", from_path);
perror("stat");
return 1;
}
filesize = sb.st_size;
from_path_for_filename = strdup(from_path);
filename = basename(from_path_for_filename);
parent_id = parse_path (to_path,files,folders);
if (parent_id == -1) {
printf("Parent folder could not be found, skipping\n");
free(from_path_for_filename);
return 0;
}
genfile = LIBMTP_new_file_t();
genfile->filesize = filesize;
genfile->filename = strdup(filename);
genfile->filetype = find_filetype (filename);
genfile->parent_id = parent_id;
genfile->storage_id = 0;
free(from_path_for_filename);
printf("Sending file...\n");
ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL);
printf("\n");
if (ret != 0) {
printf("Error sending file.\n");
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
ret = 1;
} else {
printf("New file ID: %d\n", genfile->item_id);
}
LIBMTP_destroy_file_t(genfile);
return ret;
}
int sendfile_command (int argc, char **argv) {
if (argc < 3) {
sendfile_usage();
return 0;
}
checklang();
return sendfile_function(argv[1],argv[2]);
}
LIBMTP_Dump_Errorstack
void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2659
LIBMTP_Clear_Errorstack
void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2632
LIBMTP_destroy_file_t
void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
Definition libmtp.c:4323
LIBMTP_Send_File_From_File
int LIBMTP_Send_File_From_File(LIBMTP_mtpdevice_t *device, char const *const path, LIBMTP_file_t *const filedata, LIBMTP_progressfunc_t const callback, void const *const data)
Definition libmtp.c:5980
LIBMTP_new_file_t
LIBMTP_file_t * LIBMTP_new_file_t(void)
Definition libmtp.c:4299
LIBMTP_folder_t
struct LIBMTP_folder_struct LIBMTP_folder_t
Definition libmtp.h:469
LIBMTP_file_t
struct LIBMTP_file_struct LIBMTP_file_t
Definition libmtp.h:465
LIBMTP_mtpdevice_t
struct LIBMTP_mtpdevice_struct LIBMTP_mtpdevice_t
Definition libmtp.h:464
libmtp.h