text viewer: reworks screen access logics and some bugs fix.
[kugel-rb.git] / utils / MTP / sendfirm.c
blob5fe970797eb3a83ea1e5f76b5c89b85298809101
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 * 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.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
26 #define _LARGEFILE_SOURCE
27 #define _LARGEFILE64_SOURCE
29 #include <string.h>
30 #include <libgen.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include "libmtp.h"
37 LIBMTP_mtpdevice_t *device;
39 static int progress(uint64_t const sent, uint64_t const total,
40 void const *const data)
42 int percent = (sent * 100) / total;
43 #ifdef __WIN32__
44 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
45 #else
46 printf("Progress: %"PRIu64" of %"PRIu64" (%d%%)\r", sent, total, percent);
47 #endif
48 fflush(stdout);
49 return 0;
52 void usage(void)
54 fprintf(stderr, "usage: sendfirm <local filename>\n");
57 int sendfile_function(char *from_path)
59 printf("Sending %s\n", from_path);
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;
69 #ifdef __USE_LARGEFILE64
70 if (stat64(from_path, &sb) == -1)
72 #else
73 if (stat(from_path, &sb) == -1)
75 #endif
76 fprintf(stderr, "%s: ", from_path);
77 perror("stat");
78 exit(1);
81 #ifdef __USE_LARGEFILE64
82 filesize = sb.st_size;
83 #else
84 filesize = (uint64_t) sb.st_size;
85 #endif
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");
94 #ifdef OLDMTP
95 ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress,
96 NULL, 0);
97 #else
98 ret = LIBMTP_Send_File_From_File(device, from_path, genfile, progress,
99 NULL);
100 #endif
102 printf("\n");
103 if (ret != 0)
105 printf("Error sending file.\n");
106 LIBMTP_Dump_Errorstack(device);
107 LIBMTP_Clear_Errorstack(device);
109 else
111 printf("New file ID: %d\n", genfile->item_id);
114 LIBMTP_destroy_file_t(genfile);
116 return 0;
119 int main(int argc, char **argv)
121 if (argc < 2)
123 usage();
124 return 1;
127 LIBMTP_Init();
129 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
131 device = LIBMTP_Get_First_Device();
132 if (device == NULL)
134 printf("No devices.\n");
135 return 0;
138 sendfile_function(argv[1]);
140 LIBMTP_Release_Device(device);
142 exit(0);