Program: 142339
Project: 142339
Buggy source:
public static void checkForLegacyInstall(Context c,SqlIndexedStorageUtility<ApplicationRecord> currentAppStorage) throws StorageFullException {
  SharedPreferences globalPreferences=PreferenceManager.getDefaultSharedPreferences(c);
  if (globalPreferences.getString(LEGACY_UPGRADE_PROGRESS,"String_Node_Str").equals(UPGRADE_COMPLETE)) {
    return;
  }
  if (!c.getDatabasePath(GlobalConstants.CC_DB_NAME).exists()) {
    globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    return;
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  int installedApps=currentAppStorage.getNumRecords();
  ApplicationRecord record=null;
  if (installedApps == 0) {
  }
 else   if (installedApps == 1) {
    for (    ApplicationRecord r : currentAppStorage) {
      int status=r.getStatus();
      if (status == ApplicationRecord.STATUS_SPECIAL_LEGACY) {
        record=r;
      }
    }
    if (record == null) {
      globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      return;
    }
  }
 else {
    globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    return;
  }
  final android.database.sqlite.SQLiteDatabase olddb=new LegacyCommCareOpenHelper(c).getReadableDatabase();
  LegacyDbHelper ldbh=new LegacyDbHelper(c){
    @Override public android.database.sqlite.SQLiteDatabase getHandle(){
      return olddb;
    }
  }
;
  LegacySqlIndexedStorageUtility<Resource> legacyResources=new LegacySqlIndexedStorageUtility<Resource>("String_Node_Str",Resource.class,ldbh);
  boolean hasProfile=false;
  boolean allInstalled=true;
  int oldDbSize=0;
  for (  Resource r : legacyResources) {
    if (r.getStatus() != Resource.RESOURCE_STATUS_INSTALLED) {
      allInstalled=false;
    }
    if (r.getResourceId().equals("String_Node_Str")) {
      hasProfile=true;
    }
    oldDbSize++;
  }
  if (hasProfile && allInstalled) {
  }
 else {
    globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    return;
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  if (record == null) {
    record=new ApplicationRecord("String_Node_Str",ApplicationRecord.STATUS_SPECIAL_LEGACY);
  }
  CommCareApp app=new CommCareApp(record);
  app.setupSandbox();
  SqlIndexedStorageUtility<Resource> newInstallTable=app.getStorage("String_Node_Str",Resource.class);
  SqlIndexedStorageUtility.cleanCopy(legacyResources,newInstallTable);
  LegacySqlIndexedStorageUtility<FormInstance> legacyFixtures=new LegacySqlIndexedStorageUtility<FormInstance>("String_Node_Str",FormInstance.class,ldbh);
  SqlIndexedStorageUtility<FormInstance> newFixtures=app.getStorage("String_Node_Str",FormInstance.class);
  SqlIndexedStorageUtility.cleanCopy(legacyFixtures,newFixtures);
  LegacySqlIndexedStorageUtility<AndroidLogEntry> legacyLogs=new LegacySqlIndexedStorageUtility<AndroidLogEntry>(AndroidLogEntry.STORAGE_KEY,AndroidLogEntry.class,ldbh);
  if (legacyLogs.isEmpty()) {
  }
 else {
    SqlIndexedStorageUtility<AndroidLogEntry> newLogs=app.getStorage(AndroidLogEntry.STORAGE_KEY,AndroidLogEntry.class);
    SqlIndexedStorageUtility.cleanCopy(legacyLogs,newLogs);
    legacyLogs.removeAll();
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  File oldRoot=new File(getOldFileSystemRoot());
  String newRoot=app.fsPath("String_Node_Str");
  oldRoot.renameTo(new File(newRoot));
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  Cursor ef=c.getContentResolver().query(FormsProviderAPI.FormsColumns.CONTENT_URI,new String[]{FormsProviderAPI.FormsColumns.FORM_FILE_PATH,FormsProviderAPI.FormsColumns._ID},null,null,null);
  ArrayList<Pair<Uri,String>> toReplace=new ArrayList<Pair<Uri,String>>();
  while (ef.moveToNext()) {
    String filePath=ef.getString(ef.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_FILE_PATH));
    String newFilePath=replaceOldRoot(filePath,getOldFileSystemRoot(),newRoot);
    if (!newFilePath.equals(filePath)) {
      Uri uri=ContentUris.withAppendedId(FormsProviderAPI.FormsColumns.CONTENT_URI,ef.getLong(ef.getColumnIndex(FormsProviderAPI.FormsColumns._ID)));
      toReplace.add(new Pair<Uri,String>(uri,newFilePath));
    }
  }
  for (  Pair<Uri,String> p : toReplace) {
    ContentValues cv=new ContentValues();
    cv.put(FormsProviderAPI.FormsColumns.FORM_FILE_PATH,p.second);
    int updated=c.getContentResolver().update(p.first,cv,null,null);
    if (updated != 1) {
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + updated);
    }
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + toReplace.size() + "String_Node_Str");
  Map<String,?> oldPrefs=globalPreferences.getAll();
  Editor e=app.getAppPreferences().edit();
  for (  String k : oldPrefs.keySet()) {
    Object o=oldPrefs.get(k);
    if (o instanceof String) {
      e.putString(k,(String)o);
    }
 else     if (o instanceof Integer) {
      e.putInt(k,(Integer)o);
    }
 else     if (o instanceof Long) {
      e.putLong(k,(Long)o);
    }
 else     if (o instanceof Boolean) {
      e.putBoolean(k,(Boolean)o);
    }
 else     if (o instanceof Float) {
      e.putFloat(k,(Float)o);
    }
  }
  e.commit();
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  SqlIndexedStorageUtility<UserKeyRecord> newUserKeyRecords=app.getStorage(UserKeyRecord.class);
  LegacySqlIndexedStorageUtility<User> legacyUsers=new LegacySqlIndexedStorageUtility<User>("String_Node_Str",User.class,ldbh);
  ArrayList<User> oldUsers=new ArrayList<User>();
  for (  User u : legacyUsers) {
    oldUsers.add(u);
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + oldUsers.size() + "String_Node_Str");
  olddb.close();
  User preferred=null;
  for (  User u : oldUsers) {
    if (newUserKeyRecords.getIDsForValue(UserKeyRecord.META_USERNAME,u.getUsername()).size() > 0) {
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + u.getUsername() + "String_Node_Str");
      continue;
    }
    if (preferred == null || u.getUsername().toLowerCase().equals(globalPreferences.getString(CommCarePreferences.LAST_LOGGED_IN_USER,"String_Node_Str").toLowerCase())) {
      preferred=u;
    }
    if (newUserKeyRecords.getIDsForValue(UserKeyRecord.META_USERNAME,u.getUsername()).size() == 0) {
      UserKeyRecord ukr=new UserKeyRecord(u.getUsername(),u.getPassword(),u.getWrappedKey(),new Date(0),new Date(Long.MAX_VALUE),u.getUniqueId(),UserKeyRecord.TYPE_LEGACY_TRANSITION);
      newUserKeyRecords.write(ukr);
    }
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  app.writeInstalled();
  globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  if (preferred != null) {
    for (    UserKeyRecord ukr : newUserKeyRecords.getRecordsForValues(new String[]{UserKeyRecord.META_USERNAME},new String[]{preferred.getUsername()})) {
      if (ukr.getType() == UserKeyRecord.TYPE_LEGACY_TRANSITION) {
        try {
          transitionLegacyUserStorage(c,app,generateOldTestKey().getEncoded(),ukr);
        }
 catch (        RuntimeException re) {
          re.printStackTrace();
        }
        break;
      }
    }
  }
  String toSkip=preferred == null ? null : preferred.getUsername();
  for (  UserKeyRecord ukr : newUserKeyRecords) {
    if (ukr.getUsername().equals(toSkip)) {
      continue;
    }
    if (ukr.getType() == UserKeyRecord.TYPE_LEGACY_TRANSITION) {
      try {
        transitionLegacyUserStorage(c,app,generateOldTestKey().getEncoded(),ukr);
      }
 catch (      RuntimeException re) {
        re.printStackTrace();
      }
      break;
    }
  }
  app.teardownSandbox();
}

Fixed version (ground truth):
public static void checkForLegacyInstall(Context c,SqlIndexedStorageUtility<ApplicationRecord> currentAppStorage) throws StorageFullException {
  SharedPreferences globalPreferences=PreferenceManager.getDefaultSharedPreferences(c);
  if (globalPreferences.getString(LEGACY_UPGRADE_PROGRESS,"String_Node_Str").equals(UPGRADE_COMPLETE)) {
    return;
  }
  if (!c.getDatabasePath(GlobalConstants.CC_DB_NAME).exists()) {
    globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    return;
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  int installedApps=currentAppStorage.getNumRecords();
  ApplicationRecord record=null;
  if (installedApps == 0) {
  }
 else   if (installedApps == 1) {
    for (    ApplicationRecord r : currentAppStorage) {
      int status=r.getStatus();
      if (status == ApplicationRecord.STATUS_SPECIAL_LEGACY) {
        record=r;
      }
    }
    if (record == null) {
      globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      return;
    }
  }
 else {
    globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    return;
  }
  final android.database.sqlite.SQLiteDatabase olddb=new LegacyCommCareOpenHelper(c).getReadableDatabase();
  LegacyDbHelper ldbh=new LegacyDbHelper(c){
    @Override public android.database.sqlite.SQLiteDatabase getHandle(){
      return olddb;
    }
  }
;
  LegacySqlIndexedStorageUtility<Resource> legacyResources=new LegacySqlIndexedStorageUtility<Resource>("String_Node_Str",Resource.class,ldbh);
  boolean hasProfile=false;
  boolean allInstalled=true;
  int oldDbSize=0;
  for (  Resource r : legacyResources) {
    if (r.getStatus() != Resource.RESOURCE_STATUS_INSTALLED) {
      allInstalled=false;
    }
    if (r.getResourceId().equals("String_Node_Str")) {
      hasProfile=true;
    }
    oldDbSize++;
  }
  if (hasProfile && allInstalled) {
  }
 else {
    globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    return;
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  if (record == null) {
    record=new ApplicationRecord("String_Node_Str",ApplicationRecord.STATUS_SPECIAL_LEGACY);
  }
  CommCareApp app=new CommCareApp(record);
  app.setupSandbox();
  SqlIndexedStorageUtility<Resource> newInstallTable=app.getStorage("String_Node_Str",Resource.class);
  SqlIndexedStorageUtility.cleanCopy(legacyResources,newInstallTable);
  LegacySqlIndexedStorageUtility<FormInstance> legacyFixtures=new LegacySqlIndexedStorageUtility<FormInstance>("String_Node_Str",FormInstance.class,ldbh);
  SqlIndexedStorageUtility<FormInstance> newFixtures=app.getStorage("String_Node_Str",FormInstance.class);
  SqlIndexedStorageUtility.cleanCopy(legacyFixtures,newFixtures);
  LegacySqlIndexedStorageUtility<AndroidLogEntry> legacyLogs=new LegacySqlIndexedStorageUtility<AndroidLogEntry>(AndroidLogEntry.STORAGE_KEY,AndroidLogEntry.class,ldbh);
  if (legacyLogs.isEmpty()) {
  }
 else {
    SqlIndexedStorageUtility<AndroidLogEntry> newLogs=CommCareApplication._().getGlobalStorage(AndroidLogEntry.STORAGE_KEY,AndroidLogEntry.class);
    SqlIndexedStorageUtility.cleanCopy(legacyLogs,newLogs);
    legacyLogs.removeAll();
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  File oldRoot=new File(getOldFileSystemRoot());
  String newRoot=app.fsPath("String_Node_Str");
  oldRoot.renameTo(new File(newRoot));
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  Cursor ef=c.getContentResolver().query(FormsProviderAPI.FormsColumns.CONTENT_URI,new String[]{FormsProviderAPI.FormsColumns.FORM_FILE_PATH,FormsProviderAPI.FormsColumns._ID},null,null,null);
  ArrayList<Pair<Uri,String>> toReplace=new ArrayList<Pair<Uri,String>>();
  while (ef.moveToNext()) {
    String filePath=ef.getString(ef.getColumnIndex(FormsProviderAPI.FormsColumns.FORM_FILE_PATH));
    String newFilePath=replaceOldRoot(filePath,getOldFileSystemRoot(),newRoot);
    if (!newFilePath.equals(filePath)) {
      Uri uri=ContentUris.withAppendedId(FormsProviderAPI.FormsColumns.CONTENT_URI,ef.getLong(ef.getColumnIndex(FormsProviderAPI.FormsColumns._ID)));
      toReplace.add(new Pair<Uri,String>(uri,newFilePath));
    }
  }
  for (  Pair<Uri,String> p : toReplace) {
    ContentValues cv=new ContentValues();
    cv.put(FormsProviderAPI.FormsColumns.FORM_FILE_PATH,p.second);
    int updated=c.getContentResolver().update(p.first,cv,null,null);
    if (updated != 1) {
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + updated);
    }
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + toReplace.size() + "String_Node_Str");
  Map<String,?> oldPrefs=globalPreferences.getAll();
  Editor e=app.getAppPreferences().edit();
  for (  String k : oldPrefs.keySet()) {
    Object o=oldPrefs.get(k);
    if (o instanceof String) {
      e.putString(k,(String)o);
    }
 else     if (o instanceof Integer) {
      e.putInt(k,(Integer)o);
    }
 else     if (o instanceof Long) {
      e.putLong(k,(Long)o);
    }
 else     if (o instanceof Boolean) {
      e.putBoolean(k,(Boolean)o);
    }
 else     if (o instanceof Float) {
      e.putFloat(k,(Float)o);
    }
  }
  e.commit();
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  SqlIndexedStorageUtility<UserKeyRecord> newUserKeyRecords=app.getStorage(UserKeyRecord.class);
  LegacySqlIndexedStorageUtility<User> legacyUsers=new LegacySqlIndexedStorageUtility<User>("String_Node_Str",User.class,ldbh);
  ArrayList<User> oldUsers=new ArrayList<User>();
  for (  User u : legacyUsers) {
    oldUsers.add(u);
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + oldUsers.size() + "String_Node_Str");
  olddb.close();
  User preferred=null;
  for (  User u : oldUsers) {
    if (newUserKeyRecords.getIDsForValue(UserKeyRecord.META_USERNAME,u.getUsername()).size() > 0) {
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + u.getUsername() + "String_Node_Str");
      continue;
    }
    if (preferred == null || u.getUsername().toLowerCase().equals(globalPreferences.getString(CommCarePreferences.LAST_LOGGED_IN_USER,"String_Node_Str").toLowerCase())) {
      preferred=u;
    }
    if (newUserKeyRecords.getIDsForValue(UserKeyRecord.META_USERNAME,u.getUsername()).size() == 0) {
      UserKeyRecord ukr=new UserKeyRecord(u.getUsername(),u.getPassword(),u.getWrappedKey(),new Date(0),new Date(Long.MAX_VALUE),u.getUniqueId(),UserKeyRecord.TYPE_LEGACY_TRANSITION);
      newUserKeyRecords.write(ukr);
    }
  }
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  app.writeInstalled();
  globalPreferences.edit().putString(LEGACY_UPGRADE_PROGRESS,UPGRADE_COMPLETE).commit();
  app.getAppPreferences().edit().putLong(CommCarePreferences.LOG_LAST_DAILY_SUBMIT,0).commit();
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
  if (preferred != null) {
    for (    UserKeyRecord ukr : newUserKeyRecords.getRecordsForValues(new String[]{UserKeyRecord.META_USERNAME},new String[]{preferred.getUsername()})) {
      if (ukr.getType() == UserKeyRecord.TYPE_LEGACY_TRANSITION) {
        try {
          transitionLegacyUserStorage(c,app,generateOldTestKey().getEncoded(),ukr);
        }
 catch (        RuntimeException re) {
          re.printStackTrace();
        }
        break;
      }
    }
  }
  String toSkip=preferred == null ? null : preferred.getUsername();
  for (  UserKeyRecord ukr : newUserKeyRecords) {
    if (ukr.getUsername().equals(toSkip)) {
      continue;
    }
    if (ukr.getType() == UserKeyRecord.TYPE_LEGACY_TRANSITION) {
      try {
        transitionLegacyUserStorage(c,app,generateOldTestKey().getEncoded(),ukr);
      }
 catch (      RuntimeException re) {
        re.printStackTrace();
      }
      break;
    }
  }
  app.teardownSandbox();
}