01 #!/usr/local/bin/perl -w 02 use strict; 03 use MyDropbox; 04 use App::Daemon qw( daemonize ); 05 use Log::Log4perl qw(:easy); 06 use HTTP::Status qw(:constants); 07 use File::Temp qw(tempfile); 08 use Cwd qw(realpath); 09 use Sysadm::Install qw(:all); 10 use File::Basename; 11 12 Log::Log4perl->easy_init($DEBUG); 13 14 daemonize(); 15 16 my $mod_hash = undef; 17 my $poll_interval = 60; 18 19 my $box = MyDropbox->new(); 20 $box->context( "dropbox" ); 21 22 my($home) = glob "~"; 23 my $gitdir = realpath "$home/git"; 24 25 while( 1 ) { 26 my @hash_args = (); 27 @hash_args = ( hash => $mod_hash ) if 28 defined $mod_hash; 29 30 my $href = $box->list( { @hash_args }, 31 "/gitgetter" ); 32 33 if( $href->{http_response_code} eq 34 HTTP_NOT_MODIFIED ) { 35 DEBUG "Not modified"; 36 } else { 37 $mod_hash = $href->{hash}; 38 request_handler( $box ); 39 } 40 41 DEBUG "Sleeping ${poll_interval}s"; 42 sleep $poll_interval; 43 } 44 45 ########################################### 46 sub request_handler { 47 ########################################### 48 my($box) = @_; 49 50 my $content = 51 $box->getfile("gitgetter/requests.txt"); 52 53 my $pushed = 0; 54 55 for my $line ( split /\n/, $content ) { 56 $line =~ s/#.*//; 57 next if $line =~ /^\s*$/; 58 DEBUG "Found request: '$line'"; 59 60 my $file = realpath( "$gitdir/$line" ); 61 if( $file !~ /^$gitdir/ ) { 62 ERROR "Path $file denied."; 63 next; 64 } 65 66 DEBUG "Delivering $file"; 67 68 if( !-f $file ) { 69 ERROR "$file doesn't exist"; 70 next; 71 } 72 73 my $href = $box->putfile( $file, 74 "gitgetter" ); 75 $pushed++; 76 } 77 78 if( $pushed ) { 79 my($fh, $tmpfile) = tempfile( 80 UNLINK => 1 ); 81 blurt "# pending requests\n", $tmpfile; 82 my $href = $box->putfile( $tmpfile, 83 "gitgetter", "requests.txt" ); 84 } 85 }