Exemple #1
0
 private void FixItemHeaderForUnlockedFile(UserDataFile file)
 {
     if (ItemHeaderIsMissingUnlockedFile(file))
     {
         file.Header.IsUnlocked = false;
     }
 }
Exemple #2
0
        public async Task WriteDecryptedAsync(UserDataFile file, string sourcePath, string destinationPath)
        {
            var hash = await UserDataFile.WriteUserDataFileAsync(sourcePath, destinationPath,
                                                                 Header.MasterPassword.Password, file.Header.TargetCipherIV);

            file.Header.TargetAuthentication = hash;
        }
Exemple #3
0
        private bool ItemHeaderIsMissingUnlockedFile(UserDataFile item)
        {
            if (!item.Header.IsUnlocked)
            {
                return(true);
            }

            return(!File.Exists(Path.Combine(UnlockedFolderPath, item.Header.SecuredPlainName.PlainName)));
        }
Exemple #4
0
        public void RenameFile(UserDataFile file, string name)
        {
            if (name.Contains("/"))
            {
                throw new NotANameException(Strings.Vault_RenameFile_Argument_isn_t_a_name);
            }
            var dir = NDirectory.GetPathParentDir(file.Header.SecuredPlainName.PlainName);

            MoveFile(file, dir + name);
        }
Exemple #5
0
        public async Task RemoveFile(UserDataFile file)
        {
            var success = UserDataFiles.TryTake(out file);

            if (!success)
            {
                throw new FileNotFoundException(Strings.Vault_RemoveFile_Couldn_t_take_out_file_);
            }
            File.Delete(Path.Combine(EncryptedFolderPath, file.Header.TargetPath));
            if (file.Header.IsUnlocked)
            {
                await EliminateExtracted(file);
            }
        }
Exemple #6
0
        public async Task AddFileAsync(string sourcePath, string path = "")
        {
            if (!File.Exists(sourcePath))
            {
                throw new FileNotFoundException(Strings.Vault_AddFileAsync_File_not_found, sourcePath);
            }

            var name    = Path.GetFileName(sourcePath);
            var newFile = new UserDataFile(UserDataHeader.Create(name, path));

            if (PlainNameAlreadyExists(newFile.Header.SecuredPlainName.PlainName))
            {
                FileAlreadyExists();
            }

            var destinationPath = Path.Combine(EncryptedFolderPath, newFile.Header.TargetPath);

            await WriteDecryptedAsync(newFile, sourcePath, destinationPath);

            UserDataFiles.Add(newFile);
        }
Exemple #7
0
        public void MoveFile(UserDataFile file, string destination)
        {
            // TODO: check if destination is a path by ending with a /
            var relativePath = NPath.RemoveRelativeParts(destination);
            var prevFileName = file.Header.SecuredPlainName.PlainName;

            if (PlainNameAlreadyExists(relativePath))
            {
                FileAlreadyExists();
            }

            file.Move(relativePath);

            if (file.Header.IsUnlocked)
            {
                var srcPath  = Path.Combine(UnlockedFolderPath, prevFileName);
                var destPath = Path.Combine(UnlockedFolderPath, file.Header.SecuredPlainName.PlainName);
                NDirectory.CreateMissingDirs(destPath);
                File.Move(srcPath, destPath);
            }
        }
Exemple #8
0
        public async Task EliminateExtracted(UserDataFile file)
        {
            if (!file.Header.IsUnlocked)
            {
                throw new FileNotUnlockedException();
            }
            var plainTextPath = file.Header.SecuredPlainName.PlainName;

            var path = Path.Combine(UnlockedFolderPath, plainTextPath);

            if (!File.Exists(path))
            {
                file.Header.IsUnlocked = false;
                throw new FileNotFoundException(Strings.Vault_EliminateExtracted_Decrypted_file_was_not_found, plainTextPath);
            }

            await NFile.Purge(path);

            file.Header.IsUnlocked = false;

            var parentDir = Path.Combine(UnlockedFolderPath, NDirectory.GetPathParentDir(plainTextPath));

            NDirectory.DeleteDirIfEmpty(parentDir, UnlockedFolderName);
        }
Exemple #9
0
        public async Task <ExtractStatus> ExtractFile(UserDataFile file)
        {
            FixItemHeaderForUnlockedFile(file);

            var encryptedSourcePath = Path.Combine(EncryptedFolderPath, file.Header.TargetPath);
            var unlockedTarget      = UserDataPathToUnlocked(file);

            if (file.Header.IsUnlocked)
            {
                return(ExtractStatus.Duplicate);
            }

            var hash = await UserDataFile.ExtractUserDataFile(encryptedSourcePath, unlockedTarget,
                                                              Header.MasterPassword.Password, file.Header.TargetCipherIV);

            var now = DateTime.Now;

            File.SetLastWriteTime(encryptedSourcePath, now);
            File.SetLastWriteTime(unlockedTarget, now);

            file.Header.IsUnlocked = true;

            return(hash.ContentEqualTo(file.Header.TargetAuthentication) ? ExtractStatus.Ok : ExtractStatus.HashNoMatch);
        }
Exemple #10
0
 public string UserDataPathToEncrypted(UserDataFile file)
 {
     return(Path.Combine(EncryptedFolderPath, file.Header.TargetPath));
 }
Exemple #11
0
 public string UserDataPathToUnlocked(UserDataFile file)
 {
     return(Path.Combine(UnlockedFolderPath, file.Header.SecuredPlainName.PlainName));
 }