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-2010 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 #include "util.h"
     36 #include "connect.h"
     37 
     38 extern LIBMTP_folder_t *folders;
     39 extern LIBMTP_file_t *files;
     40 extern LIBMTP_mtpdevice_t *device;
     41 
     42 void sendfile_usage(void)
     43 {
     44   fprintf(stderr, "usage: sendfile <local filename> <remote filename>\n");
     45 }
     46 
     47 int sendfile_function(char * from_path, char *to_path)
     48 {
     49   printf("Sending %s to %s\n",from_path,to_path);
     50   char *filename;
     51   uint64_t filesize;
     52   struct stat sb;
     53   LIBMTP_file_t *genfile;
     54   int ret;
     55   uint32_t parent_id = 0;
     56 
     57   if ( stat(from_path, &sb) == -1 ) {
     58     fprintf(stderr, "%s: ", from_path);
     59     perror("stat");
     60     return 1;
     61   }
     62 
     63   filesize = sb.st_size;
     64   filename = basename(from_path);
     65   parent_id = parse_path (to_path,files,folders);
     66   if (parent_id == -1) {
     67     printf("Parent folder could not be found, skipping\n");
     68     return 0;
     69   }
     70 
     71   genfile = LIBMTP_new_file_t();
     72   genfile->filesize = filesize;
     73   genfile->filename = strdup(filename);
     74   genfile->filetype = find_filetype (filename);
     75   genfile->parent_id = parent_id;
     76   genfile->storage_id = 0;
     77 
     78   printf("Sending file...\n");
     79   ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, NULL);
     80   printf("\n");
     81   if (ret != 0) {
     82     printf("Error sending file.\n");
     83     LIBMTP_Dump_Errorstack(device);
     84     LIBMTP_Clear_Errorstack(device);
     85     ret = 1;
     86   } else {
     87     printf("New file ID: %d\n", genfile->item_id);
     88   }
     89 
     90   LIBMTP_destroy_file_t(genfile);
     91 
     92   return ret;
     93 }
     94 
     95 int sendfile_command (int argc, char **argv) {
     96   if (argc < 3) {
     97     sendfile_usage();
     98     return 0;
     99   }
    100   checklang();
    101   return sendfile_function(argv[1],argv[2]);
    102 }
    103