This file is indexed.

/usr/share/perl5/SyncUtil/SyncEngine.pm is in syncbbdb 2.6-1.

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

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
#------------------------------------------------------------------------------
#   $Date: 2005/05/21 18:08:02 $
#   RCS: $Id: SyncEngine.pm,v 1.2 2005/05/21 18:08:02 aaronkaplan Exp $
#------------------------------------------------------------------------------

package SyncEngine;

use strict;

sub new {
  my $type = shift;
  my $self = {};

  bless($self, $type);

  $self->{'host'}    = shift;
  $self->{'localDB'} = shift;
  $self->{'pilotDB'} = shift;
  
  $self->{'last-time'} = 0;

  $self;
}

sub start {
  my $self = shift;

#    print "Info: " . 
#  	 $self->{'pilotDB'}->lastSyncPC() . " - " .
#  	 $self->{'host'}->thisPC() . "\n";
#    print "      " . 
#  	 $self->{'pilotDB'}->lastSyncDate() . " - " .
#  	 $self->{'host'}->lastSyncDate() ."\n";

  my $fast = 0;

  if (defined($self->{'localDB'}->lastSyncDate()) &&
      defined($self->{'pilotDB'}->lastSyncDate()) &&
      $self->{'localDB'}->lastSyncDate() eq $self->{'pilotDB'}->lastSyncDate())
  { $fast = 1; }

  if ($fast) {
    $self->{'host'}->output("Doing Fast Sync!\n");
  } else {
    $self->{'host'}->output("Doing Full Sync!\n");
  }

#   $fast = 0;

#   if ($fast) {
#     $self->{'host'}->output("Doing Fast Sync!\n");
#   } else {
#     $self->{'host'}->output("Doing Full Sync!\n");
#   }
    
  ## let them know what sort of sync they will be doing as well
  ## as the other database they will be syncing with..
  $self->{'localDB'}->setup($fast, $self->{'pilotDB'});
  $self->{'pilotDB'}->setup($fast, $self->{'localDB'});

  $self->getLocal();
  $self->getPilot();

  $self->sync();

  $self->{'pilotDB'}->finish();
  $self->{'localDB'}->finish();
}

sub getLocal {
  my $self    = shift;
  my $localDB = $self->{'localDB'};

  $self->{'local-new'} = $localDB->getNew();
  $self->{'local-mod'} = $localDB->getMod();
  $self->{'local-del'} = $localDB->getDel();
}

sub getPilot {
  my $self    = shift;
  my $pilotDB = $self->{'pilotDB'};

  $self->{'pilot-new'} = $pilotDB->getNew();
  $self->{'pilot-mod'} = $pilotDB->getMod();
  $self->{'pilot-del'} = $pilotDB->getDel();
}

sub sync {
  my $self = shift;

  my $host    = $self->{'host'};
  my $localDB = $self->{'localDB'};
  my $pilotDB = $self->{'pilotDB'};

  # build a hash of 'deleted' records.  We may override these if
  # they are also modified on the other device...
  my %lDelHash;
  foreach my $id (@{$self->{'local-del'}}) {
    $lDelHash{$id} = 1;
    $self->update();
  }

  my %pDelHash;
  foreach my $id (@{$self->{'pilot-del'}}) {
    $pDelHash{$id} = 1;
    $self->update();
  }
  
  my %lModHash;
  foreach my $rec (@{$self->{'local-mod'}}) {
      my @ids = @{$localDB->getPilotIDs($rec)};

      foreach my $id (@ids) {
	  delete $pDelHash{$id}; ## Don't delete this if it was also modified
	  $lModHash{$id} = $rec;
      }

    $self->update();
  }

  my $totalRecs = 0;
  $totalRecs += scalar(@{$self->{'pilot-new'}});
  $totalRecs += scalar(@{$self->{'pilot-mod'}});
  $totalRecs += scalar(@{$self->{'pilot-del'}});

  $totalRecs += scalar(@{$self->{'local-new'}});
  $totalRecs += scalar(@{$self->{'local-mod'}});
  $totalRecs += scalar(@{$self->{'local-del'}});

  my $cnt=0;

  if (scalar @{$self->{'pilot-new'}}) {
    $host->{'prefix'} = "";
    $host->output("Adding Records to " . $localDB->name() . ":\n");

    $host->{'prefix'} = "    ";
    foreach my $rec (@{$self->{'pilot-new'}}) {
      $host->status("Syncing New Records", int(100 * $cnt++/$totalRecs));

      $pilotDB->output($rec);
      $localDB->syncRec($rec);
    }
  }

  if (scalar @{$self->{'local-new'}}) {
    $host->{'prefix'} = "";
    $host->output("Adding Records to " . $pilotDB->name() . ":\n");

    $host->{'prefix'} = "    ";
    foreach my $rec (@{$self->{'local-new'}}) {
      $host->status("Syncing New Records", int(100 * $cnt++/$totalRecs));

      $localDB->output($rec);
      $pilotDB->syncRec($rec);
    }
  }

  $host->{'prefix'} = "ALERT: ";

  if (scalar @{$self->{'pilot-mod'}}) {
    $host->{'prefix'} = "";
    $host->output("Updating Records in " . $localDB->name() .":\n");
    $host->{'prefix'} = "    ";

    foreach my $prec (@{$self->{'pilot-mod'}}) {
      $host->status("Merging Records", int(100 * $cnt++/$totalRecs));

      my $id = $pilotDB->getPilotID($prec);
      my $lrec = $localDB->getRec($id);
      
      delete $lDelHash{$id};	## Don't delete this if it was also modified
      
      if (not defined $lModHash{$id}) {

	## just modified on palm...
	$pilotDB->output($prec);
	$localDB->syncRec($prec, $lrec);
      } else {

	## modified both places, so copy records and update both...
	$host->{'prefix'} = "Duplicating Record: ";
	$pilotDB->output($prec);
	$host->{'prefix'} = "    ";

	# The order and pairing is important here...
	my $lcpy = $localDB->dupRec($lrec, $prec);
	my $pcpy = $pilotDB->dupRec($prec, $lrec, $lcpy);
	
	$localDB->syncRec($pcpy, $lcpy);
	$pilotDB->syncRec($lrec, $prec);

	# Don't Sync for local already done both...
	delete $lModHash{$id};
      }
      
      $self->update();
    }
  }

  if (scalar keys %lModHash) {
    $host->{'prefix'} = "";
    $host->output("Updating Records in " . $pilotDB->name() . ":\n");
    $host->{'prefix'} = "    ";

    foreach my $id (keys %lModHash) {
      $host->status("Merging Records", int(100 * $cnt++/$totalRecs));

      my $lrec = $localDB->getRec($id);
      my $prec = $pilotDB->getRec($id);

      # This can happen if two records are merged.
      # this probably means I need to be more careful to
      # only do one operation per local Rec...
      next if (!defined $lrec);

      $localDB->output($lrec);
      
      ## just modified locally...
      $pilotDB->syncRec($lrec, $prec);
    }
  }

  if (scalar keys %pDelHash) {
    $host->{'prefix'} = "";
    $host->output("Deleting Records from " . $localDB->name() . ":\n");
    $host->{'prefix'} = "    ";

    foreach my $id (keys %pDelHash) {
      $host->status("Deleting Records", int(100 * $cnt++/$totalRecs));

      my $rec = $localDB->getRec($id);
      if (defined $rec) 
	{
	  $localDB->output($rec);
	  $localDB->deleteRec($id);
	}
    }
  }

  if (scalar keys %lDelHash) {
    $host->{'prefix'} = "";
    $host->output("Deleting Records from " . $pilotDB->name() . ":\n");
    $host->{'prefix'} = "    ";

    foreach my $id (keys %lDelHash) {
      $host->status("Deleting Records", int(100 * $cnt++/$totalRecs));

      my $rec = $pilotDB->getRec($id);
      if (defined $rec) 
	{
	  $pilotDB->output($rec);
	  $pilotDB->deleteRec($id);
	}
    }
  }

  $host->status("Update Completed", 100);
}

sub dump {
  my $self = shift;

  my $host    = $self->{'host'};
  my $localDB = $self->{'localDB'};
  my $pilotDB = $self->{'pilotDB'};

  $host->output("New Local\n");
  my @lnew = @{$self->{'local-new'}};
  foreach my $rec (@lnew) {
    $localDB->output($rec);
  }

  $host->output("Modified Local\n");
  my @lmod = @{$self->{'local-mod'}};
  foreach my $rec (@lmod) {
    $localDB->output($rec);
  }
  
  $host->output("Deleted Local\n");
  my @ldel = @{$self->{'local-del'}};
  foreach my $id (@ldel) {
    my $rec = $pilotDB->getRec($id);
    if (defined $rec) {
      $pilotDB->output($rec);
    } else {
      $host->output("ID: $id\n");
    }
  }

  # Pilot stufff

  $host->output("New Pilot:\n");
  my @pnew = @{$self->{'pilot-new'}};
  foreach my $rec (@pnew)	{
    $pilotDB->output($rec);
  }

  $host->output( "Mod Pilot:\n");
  my @pmod = @{$self->{'pilot-mod'}};
  foreach my $rec (@pmod)	{
    $pilotDB->output($rec);
  }
  
  $host->output( "Del Pilot:\n");
  my @pdel = @{$self->{'pilot-del'}};
  foreach my $id (@pdel)	{
    my $rec = $localDB->getRec($id);
    if (defined $rec) {
      $localDB->output($rec);
    } else {
      $host->output("ID: $id\n");
    }
  }
}

sub update {
  my $self = shift;
  my $ctime = time();
  # at least once a second give host some time...
  if ($self->{'last-time'} != $ctime)
    {
      $self->{'host'}->update();
      $self->{'last-time'} = $ctime;
    }
}

1;