Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Based on sendfile.c from libmtp: http://libmtp.sourceforge.net |
| 11 | * Modified by Maurus Cuelenaere and Nicolas Pennequin. |
| 12 | * |
| 13 | * Copyright (C) 2005-2007 Linus Walleij |
| 14 | * Copyright (C) 2006 Chris A. Debenham |
| 15 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * as published by the Free Software Foundation; either version 2 |
| 19 | * of the License, or (at your option) any later version. |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 20 | * |
| 21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 22 | * KIND, either express or implied. |
| 23 | * |
| 24 | ****************************************************************************/ |
| 25 | |
| 26 | #define _LARGEFILE_SOURCE |
| 27 | #define _LARGEFILE64_SOURCE |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <libgen.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <fcntl.h> |
Nils Wallménius | ef726ce | 2009-02-07 21:00:18 +0000 | [diff] [blame] | 34 | #include <inttypes.h> |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 35 | #include "libmtp.h" |
| 36 | |
| 37 | LIBMTP_mtpdevice_t *device; |
| 38 | |
Nils Wallménius | ef726ce | 2009-02-07 21:00:18 +0000 | [diff] [blame] | 39 | static int progress(uint64_t const sent, uint64_t const total, |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 40 | void const *const data) |
| 41 | { |
| 42 | int percent = (sent * 100) / total; |
| 43 | #ifdef __WIN32__ |
| 44 | printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent); |
| 45 | #else |
Nils Wallménius | ef726ce | 2009-02-07 21:00:18 +0000 | [diff] [blame] | 46 | printf("Progress: %"PRIu64" of %"PRIu64" (%d%%)\r", sent, total, percent); |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 47 | #endif |
| 48 | fflush(stdout); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | void usage(void) |
| 53 | { |
| 54 | fprintf(stderr, "usage: sendfirm <local filename>\n"); |
| 55 | } |
| 56 | |
| 57 | int sendfile_function(char *from_path) |
| 58 | { |
| 59 | printf("Sending %s\n", from_path); |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 60 | uint64_t filesize; |
| 61 | #ifdef __USE_LARGEFILE64 |
| 62 | struct stat64 sb; |
| 63 | #else |
| 64 | struct stat sb; |
| 65 | #endif |
| 66 | LIBMTP_file_t *genfile; |
| 67 | int ret; |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 68 | |
| 69 | #ifdef __USE_LARGEFILE64 |
| 70 | if (stat64(from_path, &sb) == -1) |
| 71 | { |
| 72 | #else |
| 73 | if (stat(from_path, &sb) == -1) |
| 74 | { |
| 75 | #endif |
| 76 | fprintf(stderr, "%s: ", from_path); |
| 77 | perror("stat"); |
| 78 | exit(1); |
| 79 | } |
| 80 | |
| 81 | #ifdef __USE_LARGEFILE64 |
| 82 | filesize = sb.st_size; |
| 83 | #else |
| 84 | filesize = (uint64_t) sb.st_size; |
| 85 | #endif |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 86 | |
| 87 | genfile = LIBMTP_new_file_t(); |
| 88 | genfile->filesize = filesize; |
| 89 | genfile->filename = strdup("nk.bin"); |
| 90 | genfile->filetype = LIBMTP_FILETYPE_FIRMWARE; |
| 91 | |
| 92 | printf("Sending file...\n"); |
Nils Wallménius | ef726ce | 2009-02-07 21:00:18 +0000 | [diff] [blame] | 93 | |
| 94 | #ifdef OLDMTP |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 95 | ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, |
Nils Wallménius | ef726ce | 2009-02-07 21:00:18 +0000 | [diff] [blame] | 96 | NULL, 0); |
| 97 | #else |
| 98 | ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress, |
| 99 | NULL); |
| 100 | #endif |
| 101 | |
Nicolas Pennequin | 3e2c51d | 2007-11-22 18:07:40 +0000 | [diff] [blame] | 102 | printf("\n"); |
| 103 | if (ret != 0) |
| 104 | { |
| 105 | printf("Error sending file.\n"); |
| 106 | LIBMTP_Dump_Errorstack(device); |
| 107 | LIBMTP_Clear_Errorstack(device); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | printf("New file ID: %d\n", genfile->item_id); |
| 112 | } |
| 113 | |
| 114 | LIBMTP_destroy_file_t(genfile); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int main(int argc, char **argv) |
| 120 | { |
| 121 | if (argc < 2) |
| 122 | { |
| 123 | usage(); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | LIBMTP_Init(); |
| 128 | |
| 129 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 130 | |
| 131 | device = LIBMTP_Get_First_Device(); |
| 132 | if (device == NULL) |
| 133 | { |
| 134 | printf("No devices.\n"); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | sendfile_function(argv[1]); |
| 139 | |
| 140 | LIBMTP_Release_Device(device); |
| 141 | |
| 142 | exit(0); |
| 143 | } |