Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file sendfile.c
      3  * Example program to send an arbitrary file to a device.
      4  *
      5  * Copyright (C) 2005-2009 Linus Walleij <triad (at) df.lth.se>
      6  * Copyright (C) 2006 Chris A. Debenham <chris (at) adebenham.com>
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Lesser General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Lesser General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Lesser General Public
     19  * License along with this library; if not, write to the
     20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     21  * Boston, MA 02111-1307, USA.
     22  */
     23 
     24 #include "config.h"
     25 
     26 #include <stdlib.h>
     27 #include <string.h>
     28 #include <libgen.h>
     29 #include <sys/stat.h>
     30 #include <sys/types.h>
     31 #include <fcntl.h>
     32 #include "common.h"
     33 #include "libmtp.h"
     34 #include "pathutils.h"
     35 
     36 extern LIBMTP_folder_t *folders;
     37 extern LIBMTP_file_t *files;
     38 extern LIBMTP_mtpdevice_t *device;
     39 
     40 int sendfile_function(char *, char *);
     41 void sendfile_command(int, char **);
     42 void sendfile_usage(void);
     43 
     44 void sendfile_usage(void)
     45 {
     46   fprintf(stderr, "usage: sendfile <local filename> <remote filename>\n");
     47 }
     48 
     49 int sendfile_function(char * from_path, char *to_path)
     50 {
     51   printf("Sending %s to %s\n",from_path,to_path);
     52   char *filename;
     53   uint64_t filesize;
     54   struct stat sb;
     55   LIBMTP_file_t *genfile;
     56   int ret;
     57   uint32_t parent_id = 0;
     58 
     59   if ( stat(from_path, &sb) == -1 ) {
     60     fprintf(stderr, "%s: ", from_path);
     61     perror("stat");
     62     exit(1);
     63   }
     64 
     65   filesize = sb.st_size;
     66   filename = basename(from_path);
     67   parent_id = parse_path (to_path,files,folders);
     68   if (parent_id == -1) {
     69     printf("Parent folder could not be found, skipping\n");
     70     return 0;
     71   }
     72 
     73   genfile = LIBMTP_new_file_t();
     74   genfile->filesize = filesize;
     75   genfile->filename = strdup(filename);
     76   genfile->filetype = find_filetype (filename);
     77   genfile->parent_id = parent_id;
     78   genfile->storage_id = 0;
     79 
     80   printf("Sending file...\n");
     81   ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL);
     82   printf("\n");
     83   if (ret != 0) {
     84     printf("Error sending file.\n");
     85     LIBMTP_Dump_Errorstack(device);
     86     LIBMTP_Clear_Errorstack(device);
     87   } else {
     88     printf("New file ID: %d\n", genfile->item_id);
     89   }
     90 
     91   LIBMTP_destroy_file_t(genfile);
     92 
     93   return 0;
     94 }
     95 
     96 void sendfile_command (int argc, char **argv) {
     97   if (argc < 3) {
     98     sendfile_usage();
     99     return;
    100   }
    101   sendfile_function(argv[1],argv[2]);
    102 }
    103