Disable the pacbox chapter for the c200 manual.
[kugel-rb.git] / utils / MTP / sendfirm.c
blobf06a398696f39e82f59e1ea1bacddc9596fa36ab
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 "libmtp.h"
36 LIBMTP_mtpdevice_t *device;
38 static int progress(u_int64_t const sent, u_int64_t const total,
39 void const *const data)
41 int percent = (sent * 100) / total;
42 #ifdef __WIN32__
43 printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
44 #else
45 printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
46 #endif
47 fflush(stdout);
48 return 0;
51 void usage(void)
53 fprintf(stderr, "usage: sendfirm <local filename>\n");
56 int sendfile_function(char *from_path)
58 printf("Sending %s\n", from_path);
59 uint64_t filesize;
60 #ifdef __USE_LARGEFILE64
61 struct stat64 sb;
62 #else
63 struct stat sb;
64 #endif
65 LIBMTP_file_t *genfile;
66 int ret;
67 uint32_t parent_id = 0;
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");
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);