This file is indexed.

/usr/bin/tunes2pod is in gnupod-tools 0.99.8-2.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl
#  Copyright (C) 2002-2007 Adrian Ulrich <pab at blinkenlights.ch>
#  Part of the gnupod-tools collection
#
#  URL: http://www.gnu.org/software/gnupod/
#
#    GNUpod is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    GNUpod is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.#
#
# iTunes and iPod are trademarks of Apple
#
# This product is not supported/written/published by Apple!

use strict;
use GNUpod::iTunesDB;
use GNUpod::XMLhelper;
use GNUpod::FooBar;
use Getopt::Long;
use Data::Dumper;

use constant MODE_SONGS => 1;
use constant MODE_OLDPL => 2;
use constant MODE_NEWPL => 3;

use vars qw(%opts);
$| = 1;


print "tunes2pod Version 0.99.8 (C) Adrian Ulrich\n";

$opts{mount} = $ENV{IPOD_MOUNTPOINT};

GetOptions(\%opts, "version", "force", "help|h", "mount|m=s");
GNUpod::FooBar::GetConfig(\%opts, {mount=>'s', force=>'b', model=>'s'}, "tunes2pod");


usage() if $opts{help};
version() if $opts{version};

convert();


sub convert {
	$opts{_no_sync} = 1;
	my $con = GNUpod::FooBar::connect(\%opts);
	usage("$con->{status}\n") if $con->{status};
	
	#We disabled all autosyncing (_no_sync set to 1), so we do a test
	#ourself
	if(!$opts{force} && !(GNUpod::FooBar::ItunesDBNeedsSync($con))) {
		print "I don't think that you have to run tunes2pod\n";
		print "The GNUtunesDB looks up-to-date\n";
		print "\n";
		print "If you think i'm wrong, use '$0 --force'\n";
		exit(1);
	}
	
	open(ITUNES, $con->{itunesdb}) or usage("Could not open $con->{itunesdb}");
	
	while(<ITUNES>) {}; sysseek(ITUNES,0,0); # the iPod is a sloooow mass-storage device, slurp it into the fs-cache
	
	my $self = { ctx => {}, mode => 0, playlist => {}, pc_playlist => {}, count_songs_done => 0, count_songs_total => 0 };
	bless($self,__PACKAGE__);
	$self->ResetPlaylists;
	
	# Define callbacks
	my $obj = { offset => 0, childs => 1, fd=>*ITUNES,
	               callback => {
	                              PACKAGE=>$self, mhod => { item => 'MhodItem' }, mhit => { start => 'MhitStart', end => 'MhitEnd' },
	                              mhsd => { start => 'MhsdStart' },               mhip => { item => 'MhipItem' }, 
	                              mhyp => { item => 'MhypItem', end=>'MhypEnd' }, mhlt => { item => 'MhltItem' },
	                            }
	           };
	GNUpod::iTunesDB::ParseiTunesDB($obj,0);    # Parses the iTunesDB
	GNUpod::XMLhelper::writexml($con);          # Writes out the new XML file
	
	GNUpod::FooBar::SetItunesDBAsInSync($con);     # GNUtunesDB.xml is in-sync with iTunesDB
	GNUpod::FooBar::SetOnTheGoAsValid($con);       # ..and so is the OnTheGo data
	
	#The iTunes is now set to clean .. maybe we have to
	#update the otg..
	$opts{_no_sync}   = 0;
	$opts{_no_cstest} = 1;
	GNUpod::FooBar::connect(\%opts);
	
	print "\n Done\n";
	close(ITUNES) or die "Failed to close filehandle of $con->{itunesdb} : $!\n";
	exit(0);
}

#######################################################################
# Cleans current playlist buffer
sub ResetPlaylists {
	my($self) = @_;
	$self->{playlist}    = { name => 'Lost and Found', plid => 0, mpl => 0, podcast => 0, content => [], spl => {} };
	$self->{pc_playlist} = { index => 0, lists => {} };
}

#######################################################################
# Set name of current playlist
sub SetPlaylistName {
	my($self,$arg) = @_;
	$self->{playlist}->{name} = $arg if length($arg) != 0;
}

#######################################################################
# Set SmartPlaylists preferences for current playlist
sub SetSplPreferences {
	my($self,$ref) = @_;
	$self->{playlist}->{spl}->{preferences} = $ref;
}

#######################################################################
# Sets content of current SmartPlaylist
sub SetSplData {
	my($self,$ref) = @_;
	$self->{playlist}->{spl}->{data} = $ref;
}

#######################################################################
# Sets Matchrule for current SmartPlaylist
sub SetSplMatchrule {
	my($self,$ref) = @_;
	$self->{playlist}->{spl}->{matchrule} = $ref;
}

#######################################################################
# Sets current podcast index
sub SetPodcastIndex {
	my($self,$index) = @_;
	return if $index == 0;
	$self->{pc_playlist}->{index} = $index;
	$self->{pc_playlist}->{lists}->{$index} = { name => 'Lost and Found', content => [] };
}

#######################################################################
sub SetPodcastName {
	my($self,$name) = @_;
	my $index = $self->{pc_playlist}->{index};
	return if $index == 0;
	$self->{pc_playlist}->{lists}->{$index}->{name} = $name if length($name) != 0;
}

#######################################################################
# Append item to podcast playlist
sub AppendPodcastItem {
	my($self,$index,$item) = @_;
	my $index = $self->{pc_playlist}->{index};
	return if $index == 0;
	push(@{$self->{pc_playlist}->{lists}->{$index}->{content}},$item);
}


#######################################################################
# Dumps object content
sub Dumpit {
	my($self,%args) = @_;
	print Data::Dumper::Dumper(\%args);
}

#######################################################################
# Switch to current mhsd mode
sub MhsdStart {
	my($self,%args) = @_;
	my $type = int($args{ref}->{type});
	my $old  = $self->{mode};
	$self->{mode} = $type;
	
	if($old == MODE_SONGS) { print "\r> $self->{count_songs_done} of $self->{count_songs_total} files found, searching playlists\n" }
}

#######################################################################
# A mhit, holds information about size, length.. etc.. Should have a
# mhod as child
sub MhitStart {
	my($self, %args) = @_;
	if($self->{mode} == MODE_SONGS) {
		$self->{ctx} = $args{ref}->{ref};                                 # Swallow-in mhit reference
	}
	else {
		warn "unknown mode: $self->{mode}\n";
	}
}

#######################################################################
# We've seen all mhit childs, so we can write the <file /> item itself
sub MhitEnd {
	my($self, %args) = @_;
	if($self->{mode} == MODE_SONGS) {
		GNUpod::XMLhelper::mkfile({file=>$self->{ctx}});                  # Add <file element to xml
		$self->{ctx} = ();                                                # And drop this buffer
		my $i = ++$self->{count_songs_done};
		if($i % 32 == 0) {
			printf("\r> %d files left, %d%% done    ", $self->{count_songs_total}-$i, ($i/(1+$self->{count_songs_total})*100));
		}
	}
	else {
		warn "unknown mode: $self->{mode}\n";
	}
}

sub MhltItem {
	my($self, %args) = @_;
	$self->{count_songs_total} = $args{ref}->{childs};
}

#######################################################################
# A DataObject
sub MhodItem {
	my($self, %args) = @_;
	
	if($self->{mode} == MODE_SONGS) {
		# -> Songs mode, just add string to current context
		my $key = $args{ref}->{type_string};
		if(length($key)) {
			$self->{ctx}->{$key} = $args{ref}->{string}; # Add mhod item
		}
		else {
			warn "$0: skipping unknown entry of type '$args{ref}->{type}'\n";
		}
	}
	elsif($self->{mode} == MODE_OLDPL) {
		# Legacy playlist
		if($args{ref}->{type_string} eq 'title') {
			# -> Set title of playlist following
			$self->SetPlaylistName($args{ref}->{string});
		}
		elsif($args{ref}->{type} == 50) {
			# -> Remember spl preferences
			$self->SetSplPreferences($args{ref}->{splpref});
		}
		elsif($args{ref}->{type} == 51) {
			# -> Remember spl data
			$self->SetSplData($args{ref}->{spldata});
			$self->SetSplMatchrule($args{ref}->{matchrule});
		}
	}
	elsif($self->{mode} == MODE_NEWPL) {
		# -> Newstyle playlist
		if($args{ref}->{type_string} eq 'title' && $self->{playlist}->{podcast}) {
			# Title of playlist: create it
			$self->SetPodcastName($args{ref}->{string});
		}
	}
}


#######################################################################
# Playlist item
sub MhipItem {
	my($self, %args) = @_;
	if($self->{mode} == MODE_OLDPL) {
		# -> Old playlist. Add SongID to current content container
		push(@{$self->{playlist}->{content}}, $args{ref}->{sid});
	}
	elsif($self->{mode} == MODE_NEWPL && $self->{playlist}->{podcast}) {
		# Only read podcasts in this mode (we do normal playlists in MODE_OLDPL)
		if($args{ref}->{podcast_group} == 256 && $args{ref}->{podcast_group_ref} == 0) {
			# -> Podcast index found
			$self->SetPodcastIndex($args{ref}->{plid});
		}
		elsif($args{ref}->{podcast_group} == 0 && $args{ref}->{podcast_group_ref} != 0) {
			# -> New item for an index found, add it
			$self->AppendPodcastItem($args{ref}->{podcast_group_ref}, $args{ref}->{sid});
		}
	}
}

#######################################################################
# Playlist 'uberblock'
sub MhypItem {
	my($self, %args) = @_;
	$self->{playlist}->{plid}    = $args{ref}->{plid};
	$self->{playlist}->{mpl}     = ($args{ref}->{is_mpl} != 0 ? 1 : 0 );
	$self->{playlist}->{podcast} = ($args{ref}->{podcast} != 0 ? 1 : 0);
}

#######################################################################
# Write out whole playlist
sub MhypEnd {
	my($self, %args) = @_;
	if($self->{mode} == MODE_OLDPL) {
		if($self->{playlist}->{mpl} == 0 && $self->{playlist}->{podcast} == 0) {
			# -> 'Old' non-podcast playlist
			my $plname = $self->{playlist}->{name};
			
			if(ref($self->{playlist}->{spl}->{preferences}) eq "HASH" && ref($self->{playlist}->{spl}->{data}) eq "ARRAY") {
				# -> Handle this as a smart-playlist
				print ">> Smart-Playlist '$plname'";
				my $pref = $self->{playlist}->{spl}->{preferences};
				my $ns   = 0;
				my $nr   = 0;
				GNUpod::XMLhelper::addspl($plname, { liveupdate => $pref->{live}, moselected => $pref->{mos}, limititem=>$pref->{iitem},
				                                      limitsort=>$pref->{isort}, limitval=>$pref->{value},
				                                      matchany=>$self->{playlist}->{spl}->{matchrule},
				                                      checkrule=>$pref->{checkrule}, plid=>$self->{playlist}->{plid} } );
				foreach my $splitem (@{$self->{playlist}->{spl}->{data}}) {
					GNUpod::XMLhelper::mkfile({spl=>$splitem}, {splname=> $plname});
					$nr++;
				}
				foreach my $id (@{$self->{playlist}->{content}}) {
					GNUpod::XMLhelper::mkfile({splcont=>{id=>$id}}, {splname=>$plname});
					$ns++;
				}
				print " with $nr rules and $ns songs\n";
			}
			else {
				# -> This is a normal playlist
				print ">> Playlist '$plname'";
				my $ns = 0;
				GNUpod::XMLhelper::addpl($plname, {plid=>$self->{playlist}->{plid}});
				foreach my $id (@{$self->{playlist}->{content}}) {
					GNUpod::XMLhelper::mkfile({add => { id => $id } },{plname=>$self->{playlist}->{name}});
					$ns++;
				}
				print " with $ns songs\n";
			}
		}
	}
	elsif($self->{mode} == MODE_NEWPL) {
		# -> We are supposed to have a complete podcasts list here..
		foreach my $pci (sort keys(%{$self->{pc_playlist}->{lists}})) {
			my $cl = $self->{pc_playlist}->{lists}->{$pci};
			my $ns = 0;
			print ">> Podcast-Playlist '$cl->{name}'";
			GNUpod::XMLhelper::addpl($cl->{name}, {podcast=>1});
			foreach my $i (@{$cl->{content}}) {
				GNUpod::XMLhelper::mkfile({add => { id => $i } }, {plname=>$cl->{name}});
				$ns ++;
			}
			print " with $ns songs\n";
		}
	}
	$self->ResetPlaylists; # Resets podcast and normal playlist data
}







sub usage {
	my($rtxt) = @_;
die << "EOF";
$rtxt
Usage: tunes2pod [-h] [-m directory]

   -h, --help              display this help and exit
       --version           output version information and exit
   -m, --mount=directory   iPod mountpoint, default is \$IPOD_MOUNTPOINT
       --force             Disable 'sync' checking

Report bugs to <bug-gnupod\@nongnu.org>
EOF
}

sub version {
die << "EOF";
tunes2pod (gnupod) 0.99.8
Copyright (C) Adrian Ulrich 2002-2007

This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

EOF
}