private async Task<bool> ImportBinderFilesAsync(StorageFolder fromDirectory) { if (fromDirectory != null) { try { // Check if you are restoring a Binder or something completely unrelated, which may cause trouble. // Make sure you restore a Binder and not just any directory! var srcFiles = await fromDirectory.GetFilesAsync().AsTask().ConfigureAwait(false); bool isSrcOk = srcFiles.Any(file => file.Name == DBManager.DB_FILE_NAME) && srcFiles.Any(file => file.Name == Binder.FILENAME); if (!isSrcOk) return false; var toDirectory = await BindersDirectory .CreateFolderAsync(fromDirectory.Name, CreationCollisionOption.ReplaceExisting) .AsTask().ConfigureAwait(false); await fromDirectory.CopyDirContentsAsync(toDirectory, CancToken).ConfigureAwait(false); return true; } catch (OperationCanceledException) { } catch (Exception ex) { Logger.Add_TPL(ex.ToString(), Logger.ForegroundLogFilename); } } return false; }
public Task<bool> ImportFoldersAsync(StorageFolder fromDirectory) { return RunFunctionIfOpenAsyncTB(async delegate { if (fromDirectory == null) return false; bool isOk = false; bool isDeleteTempDir = false; MergingBinder mergingBinder = null; StorageFolder tempDirectory = null; try { // I can only import from the app local folder, otherwise sqlite says "Cannot open", even in read-only mode. // So I copy the source files into the temp directory. if (fromDirectory.Path.Contains(ApplicationData.Current.LocalCacheFolder.Path) || fromDirectory.Path.Contains(ApplicationData.Current.LocalFolder.Path)) { tempDirectory = fromDirectory; } else { tempDirectory = await ApplicationData.Current.LocalCacheFolder .CreateFolderAsync(Guid.NewGuid().ToString(), CreationCollisionOption.ReplaceExisting) .AsTask().ConfigureAwait(false); await fromDirectory.CopyDirContentsAsync(tempDirectory, CancToken).ConfigureAwait(false); isDeleteTempDir = true; } if (CancToken.IsCancellationRequested) return false; mergingBinder = MergingBinder.CreateInstance(DBName, tempDirectory); await mergingBinder.OpenAsync().ConfigureAwait(false); var sw0 = new Stopwatch(); sw0.Start(); // parallelisation here seems ideal, but it screws with SQLite. // The following works but: // 1 it does not preserve the folder sequence and I think it causes dumps. // 2 it dumps sometimes //var tasks = new List<Task>(); //foreach (var fol in mergingBinder.Folders) //{ // tasks.Add(Task.Run(() => Import1FolderAsync(fol, fromDirectory), CancToken)); //} //await Task.WhenAll(tasks).ConfigureAwait(false); foreach (var fol in mergingBinder.Folders) { await Import1FolderAsync(fol, fromDirectory).ConfigureAwait(false); } sw0.Stop(); Debug.WriteLine("Binder merge took " + sw0.ElapsedMilliseconds + " msec"); isOk = true; } catch (OperationCanceledException) { } catch (Exception ex) { await Logger.AddAsync(ex.ToString(), Logger.FileErrorLogFilename).ConfigureAwait(false); } if (mergingBinder != null) { await mergingBinder.CloseAsync().ConfigureAwait(false); mergingBinder.Dispose(); } mergingBinder = null; if (isDeleteTempDir && tempDirectory != null) await tempDirectory.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask().ConfigureAwait(false); return isOk; // LOLLO sometimes it dumps after this, // it looks like http://stackoverflow.com/questions/4532457/program-and-debugger-quit-without-indication-of-problem }); }
public async Task<bool> ExportBinderAsync(string dbName, StorageFolder fromDirectory, StorageFolder toRootDirectory) { try { if (string.IsNullOrWhiteSpace(dbName) /*|| _dbNames?.Contains(dbName) == false */ || fromDirectory == null || toRootDirectory == null) return false; //var fromDirectory = await BindersDirectory // .GetFolderAsync(dbName) // .AsTask().ConfigureAwait(false); //if (fromDirectory == null) return false; //// what if you copy a directory to an existing one? Shouldn't you delete the contents first? No! But then, shouldn't you issue a warning? //var toDirectoryTest = await toRootDirectory.TryGetItemAsync(dbName).AsTask().ConfigureAwait(false); //if (toDirectoryTest != null) //{ // var confirmation = await UserConfirmationPopup.GetInstance().GetUserConfirmationBeforeExportingBinderAsync().ConfigureAwait(false); // if (confirmation == null || confirmation.Item1 == false || confirmation.Item2 == false) return false; //} var toDirectory = await toRootDirectory .CreateFolderAsync(dbName, CreationCollisionOption.ReplaceExisting) .AsTask().ConfigureAwait(false); if (toDirectory == null) return false; Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", toDirectory); await fromDirectory.CopyDirContentsAsync(toDirectory, CancToken).ConfigureAwait(false); return true; } catch (OperationCanceledException) { } catch (Exception ex) { Logger.Add_TPL(ex.ToString(), Logger.FileErrorLogFilename); } return false; }