1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* Module: rbutil
* File: rbutil.cpp
*
* Copyright (C) 2005 Christi Alice Scarborough
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "rbutil.h"
#include "installlog.h"
// This class allows us to return directories as well as files to
// wxDir::Traverse
class wxDirTraverserIncludeDirs : public wxDirTraverser
{
public:
wxDirTraverserIncludeDirs(wxArrayString& files) : m_files(files) { }
virtual wxDirTraverseResult OnFile(const wxString& filename)
{
m_files.Add(filename);
return wxDIR_CONTINUE;
}
virtual wxDirTraverseResult OnDir(const wxString& dirname)
{
m_files.Add(dirname);
return wxDIR_CONTINUE;
}
private:
wxArrayString& m_files;
};
wxDEFINE_SCOPED_PTR_TYPE(wxZipEntry);
const wxChar* _rootmatch[] = {
wxT("rockbox.*"),
wxT("ajbrec.ajz"),
wxT("archos.mod"),
wxT(".scrobbler.*"),
wxT("battery_bench.txt"),
wxT("battery.dummy"),
};
const wxArrayString* rootmatch = new wxArrayString(
(size_t) (sizeof(_rootmatch) / sizeof(wxChar*)), _rootmatch);
bool InstallTheme(wxString Themesrc)
{
wxString dest,src,err;
int pos = Themesrc.Find('/',true);
wxString themename = Themesrc.SubString(pos+1,Themesrc.Length());
src = gv->themes_url + wxT("/") + Themesrc;
dest = gv->stdpaths->GetUserDataDir()
+ wxT("" PATH_SEP "download" PATH_SEP) + themename;
if( DownloadURL(src, dest) )
{
wxRemoveFile(dest);
ERR_DIALOG(wxT("Unable to download ") + src, wxT("Install Theme"));
return false;
}
if(!checkZip(dest))
{
ERR_DIALOG(wxT("The Zip ") + dest
+ wxT(" does not contain the correct dir structure"),
wxT("Install Theme"));
return false;
}
if(UnzipFile(dest,gv->curdestdir, true))
{
ERR_DIALOG(wxT("Unable to unzip ") + dest + wxT(" to ")
+ gv->curdestdir, wxT("Install Theme"));
return false;
}
return true;
}
bool checkZip(wxString zipname)
{
wxZipEntryPtr entry;
wxFFileInputStream* in_file = new wxFFileInputStream(zipname);
wxZipInputStream* in_zip = new wxZipInputStream(*in_file);
entry.reset(in_zip->GetNextEntry());
wxString name = entry->GetName();
if(entry->IsDir())
{
if(name.Contains(wxT(".rockbox")))
{
return true;
}
}
return false;
}
int DownloadURL(wxString src, wxString dest)
{
int input, errnum = 0, success = false;
wxString buf, errstr;
wxLogVerbose(wxT("=== begin DownloadURL(%s,%s)"), src.c_str(),
dest.c_str());
buf = wxT("Fetching ") + src;
wxProgressDialog* progress = new wxProgressDialog(wxT("Downloading"),
buf, 100, NULL, wxPD_APP_MODAL |
wxPD_AUTO_HIDE | wxPD_SMOOTH | wxPD_ELAPSED_TIME |
wxPD_REMAINING_TIME | wxPD_CAN_ABORT);
progress->SetSize(500,200);
progress->Update(0);
input = true;
wxURL* in_http = new wxURL(src);
if(gv->proxy_url != wxT(""))
in_http->SetProxy(gv->proxy_url);
if (in_http->GetError() == wxURL_NOERR)
{
wxFFileOutputStream* os = new wxFFileOutputStream(dest);
input = false;
if (os->IsOk())
{
wxInputStream* is = in_http->GetInputStream();
input = true;
if (is)
{
size_t filesize = is->GetSize();
input = true;
if (is->IsOk())
{
char buffer[FILE_BUFFER_SIZE + 1];
size_t current = 0;
while (! is->Eof())
{
is->Read(buffer, FILE_BUFFER_SIZE);
input = true;
if (is->LastRead() )
{
os->Write(buffer, is->LastRead());
input = false;
if (os->IsOk())
{
current += os->LastWrite();
if (!progress->Update(current * 100 / filesize))
{
errstr = wxT("Download aborted by user");
errnum = 1000;
break;
}
} else
{
errnum = os->GetLastError();
errstr = wxT("Can't write to output stream (")
+ stream_err_str(errnum) + wxT(")");
break;
}
} else
{
errnum = is->GetLastError();
if (errnum == wxSTREAM_EOF)
{
errnum = 0;
break;
}
errstr = wxT("Can't read from input stream (")
+ stream_err_str(errnum) + wxT(")");
}
}
os->Close();
if (! errnum)
{
errnum = os->GetLastError();
errstr = wxT("Can't close output file (")
+ stream_err_str(errnum) + wxT(")");
input = false;
}
if (! errnum) success = true;
} else
{
errnum = is->GetLastError();
errstr = wxT("Can't get input stream size (")
+ stream_err_str(errnum) + wxT(")");
}
} else
{
errnum = in_http->GetError();
errstr.Printf(wxT("Can't get input stream (%d)"), errnum);
}
delete is;
} else
{
errnum = os->GetLastError();
errstr = wxT("Can't create output stream (")
+ stream_err_str(errnum) + wxT(")");
}
delete os;
} else
{
errstr.Printf(wxT("Can't open URL %s (%d)"), src.c_str(),
in_http->GetError() );
errnum = 100;
}
delete in_http;
delete progress;
if (!success)
{
if (errnum == 0) errnum = 999;
if (input)
{
ERR_DIALOG(errstr + wxT(" reading\n") + src, wxT("Download URL"));
} else
{
ERR_DIALOG(errstr + wxT("writing to download\n/") + dest,
wxT("Download URL"));
}
}
wxLogVerbose(wxT("=== end DownloadURL"));
return errnum;
}
int UnzipFile(wxString src, wxString destdir, bool isInstall)
{
wxZipEntryPtr entry;
wxString in_str, progress_msg, buf;
int errnum = 0, curfile = 0, totalfiles = 0;
InstallLog* log = NULL;
wxLogVerbose(wxT("===begin UnzipFile(%s,%s,%i)"),
src.c_str(), destdir.c_str(), isInstall);
wxFFileInputStream* in_file = new wxFFileInputStream(src);
wxZipInputStream* in_zip = new wxZipInputStream(*in_file);
if (in_file->Ok() )
{
if (! in_zip->IsOk() )
{
errnum = in_zip->GetLastError();
ERR_DIALOG(wxT("Can't open ZIP stream ") + src
+ wxT(" for reading (") + stream_err_str(errnum)
+ wxT(")"), wxT("Unzip File") );
delete in_zip;
delete in_file;
return true;
}
totalfiles = in_zip->GetTotalEntries();
if (! in_zip->IsOk() )
{
errnum = in_zip->GetLastError();
ERR_DIALOG( wxT("Error Getting total ZIP entries for ")
+ src + wxT(" (") + stream_err_str(errnum) + wxT(")"),
wxT("Unzip File") );
delete in_zip;
delete in_file;
return true;
}
} else
{
errnum = in_file->GetLastError();
ERR_DIALOG(wxT("Can't open ") + src + wxT(" (")
+ stream_err_str(errnum) + wxT(")"), wxT("Unzip File") );
delete in_zip;
delete in_file;
return true;
}
wxProgressDialog* progress = new wxProgressDialog(wxT("Unpacking archive"),
wxT("Preparing to unpack the downloaded files to your audio"
"device"), totalfiles, NULL, wxPD_APP_MODAL |
wxPD_AUTO_HIDE | wxPD_SMOOTH | wxPD_ELAPSED_TIME |
wxPD_REMAINING_TIME | wxPD_CAN_ABORT);
progress->Update(0);
// We're not overly worried if the logging fails
if (isInstall)
{
log = new InstallLog(destdir + wxT("" PATH_SEP UNINSTALL_FILE));
}
while (! errnum &&
(entry.reset(in_zip->GetNextEntry()), entry.get() != NULL) )
{
curfile++;
wxString name = entry->GetName();
progress_msg = wxT("Unpacking ") + name;
if (! progress->Update(curfile, progress_msg) ) {
MESG_DIALOG(wxT("Unpacking cancelled by user"));
errnum = 1000;
break;
}
in_str = destdir + wxT("" PATH_SEP) + name;
if (entry->IsDir() ) {
if (!wxDirExists(in_str) ) {
if (! wxMkdir(in_str, 0777) ) {
buf = wxT("Unable to create directory ") + in_str;
errnum = 100;
break;
}
}
log->WriteFile(name, true); // Directory
continue;
}
wxFFileOutputStream* out = new wxFFileOutputStream(in_str);
if (! out->IsOk() )
{
buf = wxT("Can't open file ") + in_str + wxT(" for writing");
delete out;
return 100;
} else if (isInstall)
{
log->WriteFile(name);
}
in_zip->Read(*out);
if (! out->IsOk()) {
buf.Printf(wxT("Can't write to %s (%d)"), in_str.c_str(),
errnum = out->GetLastError() );
}
if (!in_zip->IsOk() && ! in_file->GetLastError() == wxSTREAM_EOF)
{
buf.Printf(wxT("Can't read from %s (%d)"), src.c_str(),
errnum = in_file->GetLastError() );
}
if (! out->Close() && errnum == 0)
{
buf.Printf(wxT("Unable to close %s (%d)"), in_str.c_str(),
errnum = out->GetLastError() );
}
delete out;
}
delete in_zip; delete in_file; delete progress;
if (errnum)
{
ERR_DIALOG(buf, wxT("Unzip File"));
}
if (log) delete log;
wxLogVerbose(wxT("=== end UnzipFile"));
return(errnum);
}
int Uninstall(const wxString dir, bool isFullUninstall) {
wxString buf, uninst;
unsigned int i;
bool errflag = false;
InstallLog *log = NULL;
wxArrayString* FilesToRemove = NULL;
wxLogVerbose(wxT("=== begin Uninstall(%s,%i)"), dir.c_str(), isFullUninstall);
wxProgressDialog* progress = new wxProgressDialog(wxT("Uninstalling"),
wxT("Reading uninstall data from jukebox"), 100, NULL,
wxPD_APP_MODAL | wxPD_AUTO_HIDE | wxPD_SMOOTH |
wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME | wxPD_CAN_ABORT);
progress->Update(0);
if (! isFullUninstall)
{
buf = dir + wxT("" PATH_SEP UNINSTALL_FILE);
log = new InstallLog(buf, false); // Don't create the log
FilesToRemove = log->GetInstalledFiles();
if (log) delete log;
if (FilesToRemove == NULL || FilesToRemove->GetCount() < 1) {
wxLogNull lognull;
if ( wxMessageDialog(NULL,
wxT("Rockbox Utility can't find any uninstall data on this "
"jukebox.\n"
"Would you like to attempt a full uninstall?\n"
"(WARNING: A full uninstall removes all files in your Rockbox "
"folder)"),
wxT("Standard uninstall not possible"),
wxICON_EXCLAMATION | wxYES_NO | wxNO_DEFAULT).ShowModal()
== wxID_YES)
{
isFullUninstall = true;
}
else {
MESG_DIALOG(wxT("Uninstall cancelled by user"));
delete progress;
return 1000;
}
}
}
if (isFullUninstall )
{
buf = dir + wxT("" PATH_SEP ".rockbox");
if (rm_rf(buf) )
{
WARN_DIALOG(wxT("Unable to completely remove Rockbox directory"),
wxT("Full uninstall") );
errflag = true;
}
wxDir* root = new wxDir(dir);
wxArrayString* special = new wxArrayString();
// Search for files for deletion in the jukebox root
for (i = 0; i < rootmatch->GetCount(); i++)
{
const wxString match = (*rootmatch)[i];
root->GetAllFiles(dir, special, match, wxDIR_FILES);
}
delete root;
// Sort in reverse order so we get directories last
special->Sort(true);
for (i = 0; i < special->GetCount(); i++)
{
if (wxDirExists((*special)[i]) )
{
// We don't check the return code since we don't want non
// empty dirs disappearing.
wxRmdir((*special)[i]);
} else if (wxFileExists((*special)[i]) )
{
if (! wxRemoveFile((*special)[i]) )
{
WARN_DIALOG(wxT("Can't delete ") + (*special)[i],
wxT("Full uninstall"));
errflag = true;
}
}
// Otherwise there isn't anything there, so we don't have to worry.
}
delete special;
} else
{
wxString instplat, this_path_sep;
unsigned int totalfiles, rc;
totalfiles = FilesToRemove->GetCount();
FilesToRemove->Sort(true); // Reverse alphabetical ie dirs after files
for (i = 0; i < totalfiles; i++)
{
// If we're running on the device, let's not delete our own
// installation, eh?
if (gv->portable &&
FilesToRemove->Item(i).StartsWith(PATH_SEP
wxT("RockboxUtility")) )
{
continue;
}
wxString* buf2 = new wxString;
buf = dir + FilesToRemove->Item(i);
buf2->Format(wxT("Deleting %s"), buf.c_str());
if (! progress->Update((i + 1) * 100 / totalfiles, *buf2) )
{
WARN_DIALOG(wxT("Cancelled by user"), wxT("Normal Uninstall"));
delete progress;
return true;
}
if (wxDirExists(buf) )
{
// If we're about to attempt to remove .rockbox. delete
// install data first
*buf2 = dir + wxT("" PATH_SEP ".rockbox");
if ( buf.IsSameAs(buf2->c_str()) )
{
*buf2 = dir +wxT("" PATH_SEP UNINSTALL_FILE);
wxRemoveFile(*buf2);
}
if ( (rc = ! wxRmdir(buf)) )
{
buf = buf.Format(wxT("Can't remove directory %s"),
buf.c_str());
errflag = true;
WARN_DIALOG(buf.c_str(), wxT("Standard uninstall"));
}
} else if (wxFileExists(buf) )
{
if ( (rc = ! wxRemoveFile(buf)) )
{
buf = buf.Format(wxT("Can't delete file %s"),
buf.c_str());
errflag = true;
WARN_DIALOG(buf.c_str(), wxT("Standard uninstall"));
}
} else
{
errflag = true;
buf = buf.Format(wxT("Can't find file or directory %s"),
buf.c_str() );
WARN_DIALOG(buf.c_str(), wxT("Standard uninstall") );
}
uninst = uninst.AfterFirst('\n');
}
if (errflag)
{
ERR_DIALOG(wxT("Unable to remove some files"),
wxT("Standard uninstall")) ;
}
if (FilesToRemove != NULL) delete FilesToRemove;
}
delete progress;
wxLogVerbose(wxT("=== end Uninstall"));
return errflag;
}
wxString stream_err_str(int errnum)
{
wxString out;
switch (errnum) {
case wxSTREAM_NO_ERROR:
out = wxT("wxSTREAM_NO_ERROR");
break;
case wxSTREAM_EOF:
out = wxT("wxSTREAM_EOF");
break;
case wxSTREAM_WRITE_ERROR:
out = wxT("wxSTREAM_WRITE_ERROR");
break;
case wxSTREAM_READ_ERROR:
out = wxT("wxSTREAM_READ_ERROR");
break;
default:
out = wxT("UNKNOWN");
break;
}
return out;
}
bool InstallRbutil(wxString dest)
{
wxArrayString filestocopy;
wxString str, buf, dstr, localpath, destdir;
unsigned int i;
wxDir dir;
bool copied_exe = false, made_rbdir = false;
InstallLog* log;
buf = dest + wxT("" PATH_SEP ".rockbox");
if (! wxDirExists(buf) )
{
wxMkdir(buf);
made_rbdir = true;
}
buf = dest + wxT("" PATH_SEP UNINSTALL_FILE);
log = new InstallLog(buf);
if (made_rbdir) log->WriteFile(wxT(".rockbox"), true);
destdir = dest + wxT("" PATH_SEP "RockboxUtility");
if (! wxDirExists(destdir) )
{
if (! wxMkdir(destdir, 0777) )
{
WARN_DIALOG( wxT("Unable to create directory for installer (")
+ destdir + wxT(")"), wxT("Portable install") );
return false;
}
log->WriteFile(wxT("RockboxUtility"), true);
}
dir.GetAllFiles(gv->ResourceDir, &filestocopy, wxT("*"),
wxDIR_FILES);
if (filestocopy.GetCount() < 1)
{
WARN_DIALOG(wxT("No files to copy"), wxT("Portable install") );
return false;
}
// Copy the contents of the program directory
for (i = 0; i < filestocopy.GetCount(); i++)
{
if (filestocopy[i].AfterLast(PATH_SEP_CHR) == EXE_NAME)
{
copied_exe = true;
}
dstr = destdir + wxT("" PATH_SEP)
+ filestocopy[i].AfterLast(PATH_SEP_CHR);
if (! wxCopyFile(filestocopy[i], dstr) )
{
WARN_DIALOG( wxT("Error copying file (")
+ filestocopy[i].c_str() + wxT(" -> ")
+ dstr + wxT(")"), wxT("Portable Install") );
return false;
}
buf = dstr;
buf.Replace(dest, wxEmptyString, false);
log->WriteFile(buf);
}
if (! copied_exe)
{
str = gv->AppDir + wxT("" PATH_SEP EXE_NAME);
dstr = destdir + wxT("" PATH_SEP EXE_NAME);
if (! wxCopyFile(str, dstr) )
{
WARN_DIALOG(wxT("Can't copy program binary ")
+ str + wxT(" -> ") + dstr, wxT("Portable Install") );
return false;
}
buf = dstr;
buf.Replace(dest, wxEmptyString, false);
log->WriteFile(buf);
}
// Copy the local ini file so that it knows that it's a portable copy
gv->UserConfig->Flush();
dstr = destdir + wxT("" PATH_SEP "RockboxUtility.cfg");
if (! wxCopyFile(gv->UserConfigFile, dstr) )
{
WARN_DIALOG(wxT("Unable to install user config file (")
+ gv->UserConfigFile + wxT(" -> ") + dstr + wxT(")"),
wxT("Portable Install") );
return false;
}
buf = dstr;
buf.Replace(dest, wxEmptyString, false);
log->WriteFile(buf);
delete log;
return true;
}
bool rm_rf(wxString file)
{
wxLogVerbose(wxT("=== begin rm-rf(%s)"), file.c_str() );
wxString buf;
wxArrayString selected;
wxDirTraverserIncludeDirs wxdtid(selected);
unsigned int rc = 0, i;
bool errflag = false;
if (wxFileExists(file) )
{
rc = ! wxRemoveFile(file);
} else if (wxDirExists(file) )
{
wxDir* dir = new wxDir(file);;
dir->Traverse(wxdtid);
delete dir;
// Sort into reverse alphabetical order for deletion in correct order
// (directories after files)
selected.Sort(true);
selected.Add(file);
wxProgressDialog* progress = new wxProgressDialog(wxT("Removing files"),
wxT("Deleting files"), selected.GetCount(), NULL,
wxPD_APP_MODAL | wxPD_AUTO_HIDE | wxPD_SMOOTH |
wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME | wxPD_CAN_ABORT);
for (i = 0; i < selected.GetCount(); i++)
{
wxLogVerbose(selected[i]);
if (progress != NULL)
{
buf = wxT("Deleting ") + selected[i];
if (! progress->Update(i, buf))
{
WARN_DIALOG(wxT("Cancelled by user"), wxT("Erase Files"));
delete progress;
return true;
}
}
if (wxDirExists(selected[i]) )
{
if ((rc = ! wxRmdir(selected[i])) )
{
errflag = true;
WARN_DIALOG(wxT("Can't remove directory ") + selected[i],
wxT("Erase files"));
}
} else if ((rc = ! wxRemoveFile(selected[i])) )
{
errflag = true;
WARN_DIALOG(wxT("Error deleting file ") + selected[i],
wxT("Erase files"));
}
}
delete progress;
} else
{
WARN_DIALOG(wxT("Can't find expected file ") + file,
wxT("Erase files"));
return true;
}
wxLogVerbose(wxT("=== end rm-rf"));
return rc ? true : false;
}
|