~ubuntu-branches/ubuntu/feisty/gnupod-tools/feisty

« back to all changes in this revision

Viewing changes to src/ext/FooBar.pm

  • Committer: Bazaar Package Importer
  • Author(s): Brian Nelson
  • Date: 2005-04-05 09:10:01 UTC
  • mto: (2.1.1 sarge)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050405091001-vjtr9oktjemr6mn6
Tags: upstream-0.98
ImportĀ upstreamĀ versionĀ 0.98

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package GNUpod::FooBar;
2
 
#  Copyright (C) 2002-2003 Adrian Ulrich <pab at blinkenlights.ch>
 
2
#  Copyright (C) 2002-2005 Adrian Ulrich <pab at blinkenlights.ch>
3
3
#  Part of the gnupod-tools collection
4
4
#
5
5
#  URL: http://www.gnu.org/software/gnupod/
24
24
 
25
25
use strict;
26
26
use Digest::MD5;
27
 
use GNUpod::iTunesDB;
28
27
 
29
28
#####################################################################
30
29
# Get paths / files
39
38
if(-d $opth->{mount}) {
40
39
  $rr->{mountpoint}     = $opth->{mount};
41
40
  $rr->{etc}            = $opth->{mount}."/iPod_Control/.gnupod";
 
41
 
42
42
  $rr->{xml}            = $opth->{mount}."/iPod_Control/.gnupod/GNUtunesDB";
 
43
  #It can also be called GNUtunesDB.xml
 
44
  $rr->{xml}            = $rr->{xml}.".xml" if !(-e $rr->{xml});
43
45
  $rr->{itunesdb}       = $opth->{mount}."/iPod_Control/iTunes/iTunesDB";
 
46
  $rr->{itunessd}       = $opth->{mount}."/iPod_Control/iTunes/iTunesSD";
44
47
  $rr->{playcounts}     = "$rr->{mountpoint}/iPod_Control/iTunes/Play Counts";
45
48
  $rr->{itunesdb_md5}   = "$rr->{etc}/.itunesdb_md5";
46
 
  $rr->{playcounts_md5} = "$rr->{etc}/.playcounts_md5";
 
49
  $rr->{onthego_invalid}  = "$rr->{etc}/.onthego_invalid";
47
50
  $rr->{onthego}        = "$rr->{mountpoint}/iPod_Control/iTunes/OTGPlaylistInfo";
48
51
  $rr->{status}         = undef;
49
52
 
 
53
  _check_casesensitive($rr->{mountpoint}); #Check if somebody mounted the iPod caseSensitive
 
54
      
50
55
 #Do an iTunesDB Sync if not disabled and needed
51
56
  do_itbsync($rr) if(!$opth->{_no_it_sync} && !$opth->{_no_sync} && _itb_needs_sync($rr));
52
57
 
61
66
}
62
67
 
63
68
#######################################################################
 
69
# Check if someone mounted the iPod CaseSensitive
 
70
sub _check_casesensitive {
 
71
 my($target) = @_;
 
72
 
 
73
 if(open(CSTEST,">$target/csTeSt")) {
 
74
   my $inode_a = (stat("$target/csTeSt"))[1]; #Get inode of just-creaded file
 
75
   my $inode_b = (stat("$target/CStEsT"))[1]; #Get inode of another file..
 
76
   unlink("$target/csTeSt"); #Boom!
 
77
  
 
78
   if($inode_a != $inode_b) { #Whops, different inodes? -> case sensitive fs
 
79
     #Nerv the user
 
80
     warn "warning: $target seems to be mounted *CASE SENSITIVE*\n";
 
81
     warn "         Mounting VFAT/HFS+ like this is a VERY BAD(tm) idea,\n";
 
82
     warn "         strange things may happen... GNUpod may not work correctly!\n";
 
83
     warn "         Please mount the Filesystem CASE *IN*SENSITIVE\n";
 
84
     warn "         (use 'mount ... -o check=r' for VFAT)\n";
 
85
     warn "         [Ignore this message if $target isn't a\n";
 
86
     warn "          VFAT or HFS+ Filesystem ;) ]\n";
 
87
   }
 
88
  
 
89
 }
 
90
 else {
 
91
   warn "warning: Could not write to $target, iPod mounted read-only? ($!)\n";
 
92
 }
 
93
}
 
94
 
 
95
#######################################################################
64
96
# Call tunes2pod
65
97
sub do_itbsync {
66
98
 my($con) = @_;
68
100
my $XBIN = "$con->{bindir}/tunes2pod.pl";
69
101
 
70
102
if(-x $XBIN) {
71
 
    my $OLDENV = $ENV{IPOD_MOUNTPOINT};
72
 
    $ENV{IPOD_MOUNTPOINT} = $con->{mountpoint};
 
103
  {
 
104
   local  $ENV{IPOD_MOUNTPOINT} = $con->{mountpoint};
73
105
   print "> GNUtunesDB sync needed...\n";
74
106
    if(system("$XBIN > /dev/null")) {
75
107
      die "Unexpected die of $XBIN\n
76
108
      You can disable auto-sync (=autorun of $XBIN)
77
109
      by removing '$con->{etc}/.itunesdb_md5'\n";
78
 
    }
79
 
    
80
 
    $ENV{IPOD_MOUNTPOINT} = $OLDENV;
 
110
    } 
 
111
  }
81
112
  print "> GNUtunesDB synced\n";
82
113
}
83
114
else {
95
126
my $XBIN = "$con->{bindir}/gnupod_otgsync.pl";
96
127
 
97
128
if(-x $XBIN) {
98
 
     my $OLDENV = $ENV{IPOD_MOUNTPOINT};
99
 
     $ENV{IPOD_MOUNTPOINT} = $con->{mountpoint};
 
129
  {
 
130
     local $ENV{IPOD_MOUNTPOINT} = $con->{mountpoint};
100
131
     print "> On-The-Go data sync needed...\n";
101
132
     if(system("$XBIN --top4secret")) {
102
133
      warn "** UUUPS **: $XBIN died! On-The-Go list lost, sorry!\n";
105
136
      print "> On-The-Go data synced\n";
106
137
     }
107
138
  
108
 
   $ENV{IPOD_MOUNTPOINT} = $OLDENV;
 
139
  }
109
140
}
110
141
else {
111
142
 warn "FooBar.pm: Could not execute $XBIN, autosync SKIPPED!\n";
143
174
# Returns '1' if we MAY have to sync..
144
175
sub _itb_needs_sync {
145
176
 my($rr) = @_;
146
 
warn "debug: _itb_needs_sync called($$)\n";
 
177
 
147
178
 if(-r $rr->{itunesdb_md5} && -r $rr->{itunesdb}) {
148
179
   my $itmd = getmd5($rr->{itunesdb});
149
180
   my $otmd = getmd5line($rr->{itunesdb_md5});
157
188
# Checks if we need to do an OTG-Sync
158
189
sub _otg_needs_sync {
159
190
 my($rr) = @_;
160
 
warn "debug: otgsync need? (request from $$)\n";
 
191
#warn "debug: otgsync need? (request from $$)\n";
161
192
 #OTG Sync needed
162
 
 return 1 if(GNUpod::iTunesDB::readOTG($rr->{onthego}));
 
193
 return 1 if( (-e $rr->{onthego}) && (-s $rr->{onthego}) > 0 );
163
194
 
164
195
 if(-e $rr->{playcounts}) { #PlayCounts file exists..
165
 
  if(-r "$rr->{playcounts_md5}") { #We got a md5 hash
166
 
   my $plmd = getmd5line("$rr->{playcounts_md5}");
167
 
   #MD5 is the same
168
 
   return 0 if $plmd eq getmd5($rr->{playcounts});
169
 
  }
170
 
  #Playcounts file, but no md5, parse it..
171
196
  return 1;
172
197
 }
173
198
 
177
202
 
178
203
 
179
204
######################################################################
 
205
# Check for broken onTheGo data (= GNUtunesDB <-> iTunesDB out of sync)
 
206
sub _otgdata_broken {
 
207
 my($rr) = @_;
 
208
 return (-e $rr->{onthego_invalid});
 
209
}
 
210
 
 
211
######################################################################
 
212
# Set otgdata synched
 
213
sub setvalid_otgdata {
 
214
 my($rr) = @_;
 
215
 return undef unless -e $rr->{onthego_invalid};
 
216
 unlink($rr->{onthego_invalid});
 
217
}
 
218
######################################################################
 
219
# Set otgdata synched
 
220
sub setINvalid_otgdata {
 
221
 my($rr) = @_;
 
222
 open(OTGINVALID, ">$rr->{onthego_invalid}") or die "Can't write $rr->{onthego_invalid}\n";
 
223
  print OTGINVALID undef;
 
224
 close(OTGINVALID);
 
225
 return undef;
 
226
}
 
227
 
 
228
 
 
229
######################################################################
180
230
# Getmd5line
181
231
sub getmd5line {
182
232
 my($file) = @_;
193
243
 my($rr) = @_;
194
244
 setsync_itunesdb($rr);
195
245
 setsync_playcounts($rr);
 
246
 setsync_otg($rr);
 
247
 setvalid_otgdata($rr);
 
248
}
 
249
 
 
250
######################################################################
 
251
# SetSync for onthego
 
252
sub setsync_otg {
 
253
my($rr) = @_;
 
254
 
 
255
 
 
256
 if( !(-e $rr->{onthego}) || unlink($rr->{onthego})) {
 
257
  return undef;
 
258
 }
 
259
 
 
260
warn "Could not setsync for onthego\n";
 
261
return 1;
196
262
}
197
263
 
198
264
######################################################################
199
265
# Set only playcounts in sync
200
266
sub setsync_playcounts {
201
267
my($rr) = @_;
202
 
warn "**** playcounts setsync call $$\n";
203
 
#We also create an MD5 sum of the playcounts file
204
 
 if(-r $rr->{playcounts}) { #Test this, because getmd5 would die
205
 
  open(MDX,">$rr->{playcounts_md5}") or die "Can't write pc-md5-sum, $!\n";
206
 
   print MDX getmd5($rr->{playcounts});
207
 
  close(MDX);
208
 
  return undef;
209
 
 }
 
268
 
 
269
if( !(-e $rr->{playcounts}) || unlink($rr->{playcounts})) {
 
270
 return undef;
 
271
}
 
272
 
210
273
 warn "Can't set sync for playcounts to true: file not found\n";
211
274
 return 1;
212
275
}
215
278
# Set only itunesdb sync
216
279
sub setsync_itunesdb {
217
280
my($rr) = @_;
218
 
warn "***** itunesdb setsync call $$\n";
219
281
 if(-r $rr->{itunesdb}) {
220
282
 #Write the file with md5sum content
221
283
 open(MDX,">$rr->{itunesdb_md5}") or die "Can't write md5-sum, $!\n";
228
290
}
229
291
 
230
292
 
 
293
 
231
294
######################################################################
232
295
# Get the MD5 sum of a file
233
296
sub getmd5 {
240
303
}
241
304
 
242
305
 
 
306
 
 
307
########################################################################
 
308
# Parse configuration
 
309
sub GetConfig {
 
310
 my($getopts, $doset, $name) = @_;
 
311
 
 
312
  my($topic,$val,$optarget);
 
313
  
 
314
  foreach my $filerc ( ("$ENV{HOME}/.gnupodrc", "$getopts->{mount}/iPod_Control/.gnupod/gnupodrc") ) {
 
315
    open(RCFILE, $filerc) or next;
 
316
     while (my $line = <RCFILE>) {
 
317
      chomp($line);
 
318
      next if !$line or $line =~ /^#/;
 
319
      
 
320
      #Ok, line is not a comment and has some content, read it..
 
321
      unless(($topic,$val) = $line =~ /^(\S+)\s*=\s*(.+)$/) {
 
322
       warn "warning: Invalid line '$line' found in $filerc\n";
 
323
       next;
 
324
      }
 
325
      
 
326
      #We matched and got $topic + $val, check it $topic has a
 
327
      #specific target (like 'mktunes.volume')
 
328
      if($topic =~ /^([^.]+)\.(.+)/) {
 
329
       $optarget = $1;
 
330
       $topic    = $2;
 
331
      }
 
332
      else { #No target found
 
333
       $optarget = undef;
 
334
      }
 
335
 
 
336
  #    warn "### PARSE($line): *$topic* -> *$val*\n";
 
337
  #    warn "### $topic with target $optarget\n";
 
338
      
 
339
         if ($optarget&&$name&&$name ne $optarget) { next}
 
340
      elsif ($getopts->{$topic})      { next } #this is a dup 
 
341
      elsif ($doset->{$topic} eq "s") { $getopts->{$topic} = $val }
 
342
      elsif ($doset->{$topic} eq "i") { $getopts->{$topic} = int($val) }
 
343
      elsif ($doset->{$topic} eq "b") { $getopts->{$topic} = 1 if($val && $val ne "no") }
 
344
     }
 
345
     close(RCFILE);
 
346
 #    warn "** Parser finished $filerc\n";
 
347
  }
 
348
  
 
349
 # foreach(keys(%$getopts)) {
 
350
 #  warn "CONF: $_ - $getopts->{$_}\n";
 
351
 # }
 
352
  
 
353
  
 
354
  return 1;
 
355
}
243
356
1;