Add a note about Rockbox not running on Sansas v2 (FS#8477 by Marc Guay).
[Rockbox.git] / utils / MTP / sendfirm.c
blobe216037efcb02d3f7ea19c17812e31f22d3f7f2c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Based on sendfile.c from libmtp: http://libmtp.sourceforge.net
11 * Modified by Maurus Cuelenaere and Nicolas Pennequin.
13 * Copyright (C) 2005-2007 Linus Walleij
14 * Copyright (C) 2006 Chris A. Debenham
16 * All files in this archive are subject to the GNU General Public License.
17 * See the file COPYING in the source tree root for full license agreement.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #define _LARGEFILE_SOURCE
25 #define _LARGEFILE64_SOURCE
27 #include <string.h>
28 #include <libgen.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include "libmtp.h"
34 LIBMTP_mtpdevice_t *device;
36 static int progress(u_int64_t const sent, u_int64_t const total,
37 void const *const data)
39 int percent = (sent * 100) / total;
40 #ifdef __WIN32__
41 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
42 #else
43 printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
44 #endif
45 fflush(stdout);
46 return 0;
49 void usage(void)
51 fprintf(stderr, "usage: sendfirm <local filename>\n");
54 int sendfile_function(char *from_path)
56 printf("Sending %s\n", from_path);
57 char *filename;
58 uint64_t filesize;
59 #ifdef __USE_LARGEFILE64
60 struct stat64 sb;
61 #else
62 struct stat sb;
63 #endif
64 LIBMTP_file_t *genfile;
65 int ret;
66 uint32_t parent_id = 0;
68 #ifdef __USE_LARGEFILE64
69 if (stat64(from_path, &sb) == -1)
71 #else
72 if (stat(from_path, &sb) == -1)
74 #endif
75 fprintf(stderr, "%s: ", from_path);
76 perror("stat");
77 exit(1);
80 #ifdef __USE_LARGEFILE64
81 filesize = sb.st_size;
82 #else
83 filesize = (uint64_t) sb.st_size;
84 #endif
85 filename = basename(from_path);
87 genfile = LIBMTP_new_file_t();
88 genfile->filesize = filesize;
89 genfile->filename = strdup("nk.bin");
90 genfile->filetype = LIBMTP_FILETYPE_FIRMWARE;
92 printf("Sending file...\n");
93 ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress,
94 NULL, parent_id);
95 printf("\n");
96 if (ret != 0)
98 printf("Error sending file.\n");
99 LIBMTP_Dump_Errorstack(device);
100 LIBMTP_Clear_Errorstack(device);
102 else
104 printf("New file ID: %d\n", genfile->item_id);
107 LIBMTP_destroy_file_t(genfile);
109 return 0;
112 int main(int argc, char **argv)
114 if (argc < 2)
116 usage();
117 return 1;
120 LIBMTP_Init();
122 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
124 device = LIBMTP_Get_First_Device();
125 if (device == NULL)
127 printf("No devices.\n");
128 return 0;
131 sendfile_function(argv[1]);
133 LIBMTP_Release_Device(device);
135 exit(0);