<commit_before>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NAppUpdate.Framework.Common;

namespace NAppUpdate.Framework.Tasks
{
    [UpdateTaskAlias("fileUpdate")]
    public class FileUpdateTask : IUpdateTask
    {
        public FileUpdateTask()
        {
            UpdateConditions = new Conditions.BooleanCondition();
        }

        [NauField("localPath", "The local path of the file to update", true)]
        public string LocalPath { get; set; }

        [NauField("updateTo",
            "File name on the remote location; same name as local path will be used if left blank"
            , false)]
        public string UpdateTo { get; set; }

        [NauField("sha256-checksum", "SHA-256 checksum to validate the file after download (optional)", false)]
        public string Sha256Checksum { get; set; }

        [NauField("hotswap",
            "Default update action is a cold update; check here if a hot file swap should be attempted"
            , false)]
        public bool CanHotSwap { get; set; }

        internal string tempFile;
        private string destinationFile;

        #region IUpdateTask Members

        public string Description { get; set; }

        public Conditions.BooleanCondition UpdateConditions { get; set; }

        public bool Prepare(Sources.IUpdateSource source)
        {
            if (string.IsNullOrEmpty(LocalPath))
                return true; // Errorneous case, but there's nothing to prepare to...

            string fileName;
            if (!string.IsNullOrEmpty(UpdateTo))
                fileName = UpdateTo;
            else
                fileName = LocalPath;

            tempFile = null;

            try
            {
                string tempFileLocal = Path.Combine(UpdateManager.Instance.TempFolder, Guid.NewGuid().ToString());
                if (!source.GetData(fileName, UpdateManager.Instance.BaseUrl, ref tempFileLocal))
                    return false;

                tempFile = tempFileLocal;
            }
            catch (Exception ex)
            {
                throw new UpdateProcessFailedException("Couldn't get Data from source", ex);
            }

            if (!string.IsNullOrEmpty(Sha256Checksum))
            {
                string checksum = Utils.FileChecksum.GetSHA256Checksum(tempFile);
                if (!checksum.Equals(Sha256Checksum))
                    return false;
            }

            return true;
        }

        public bool Execute()
        {
            if (string.IsNullOrEmpty(LocalPath))
                return true;

            destinationFile = Path.Combine(Path.GetDirectoryName(UpdateManager.Instance.ApplicationPath), LocalPath);

			if (!Directory.Exists(Path.GetDirectoryName(destinationFile)))
				Utils.FileSystem.CreateDirectoryStructure(Path.GetDirectoryName(destinationFile), false);

            // Create a backup copy if target exists
            if (File.Exists(destinationFile))
                File.Copy(destinationFile, Path.Combine(UpdateManager.Instance.BackupFolder, LocalPath));

            // Only enable execution if the apply attribute was set to hot-swap
            if (CanHotSwap)<commit_msg>create subdirectories if necessary when backing up files<commit_after>﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NAppUpdate.Framework.Common;

namespace NAppUpdate.Framework.Tasks
{
    [UpdateTaskAlias("fileUpdate")]
    public class FileUpdateTask : IUpdateTask
    {
        public FileUpdateTask()
        {
            UpdateConditions = new Conditions.BooleanCondition();
        }

        [NauField("localPath", "The local path of the file to update", true)]
        public string LocalPath { get; set; }

        [NauField("updateTo",
            "File name on the remote location; same name as local path will be used if left blank"
            , false)]
        public string UpdateTo { get; set; }

        [NauField("sha256-checksum", "SHA-256 checksum to validate the file after download (optional)", false)]
        public string Sha256Checksum { get; set; }

        [NauField("hotswap",
            "Default update action is a cold update; check here if a hot file swap should be attempted"
            , false)]
        public bool CanHotSwap { get; set; }

        internal string tempFile;
        private string destinationFile;

        #region IUpdateTask Members

        public string Description { get; set; }

        public Conditions.BooleanCondition UpdateConditions { get; set; }

        public bool Prepare(Sources.IUpdateSource source)
        {
            if (string.IsNullOrEmpty(LocalPath))
                return true; // Errorneous case, but there's nothing to prepare to...

            string fileName;
            if (!string.IsNullOrEmpty(UpdateTo))
                fileName = UpdateTo;
            else
                fileName = LocalPath;

            tempFile = null;

            try
            {
                string tempFileLocal = Path.Combine(UpdateManager.Instance.TempFolder, Guid.NewGuid().ToString());
                if (!source.GetData(fileName, UpdateManager.Instance.BaseUrl, ref tempFileLocal))
                    return false;

                tempFile = tempFileLocal;
            }
            catch (Exception ex)
            {
                throw new UpdateProcessFailedException("Couldn't get Data from source", ex);
            }

            if (!string.IsNullOrEmpty(Sha256Checksum))
            {
                string checksum = Utils.FileChecksum.GetSHA256Checksum(tempFile);
                if (!checksum.Equals(Sha256Checksum))
                    return false;
            }

            return true;
        }

        public bool Execute()
        {
            if (string.IsNullOrEmpty(LocalPath))
                return true;

            destinationFile = Path.Combine(Path.GetDirectoryName(UpdateManager.Instance.ApplicationPath), LocalPath);

			if (!Directory.Exists(Path.GetDirectoryName(destinationFile)))
				Utils.FileSystem.CreateDirectoryStructure(Path.GetDirectoryName(destinationFile), false);

            // Create a backup copy if target exists
            if (File.Exists(destinationFile))
            {
                if (!Directory.Exists(Path.GetDirectoryName(Path.Combine(UpdateManager.Instance.BackupFolder, LocalPath))))
                    Utils.FileSystem.CreateDirectoryStructure(Path.GetDirectoryName(Path.Combine(UpdateManager.Instance.BackupFolder, LocalPath)), false);
                File.Copy(destinationFile, Path.Combine(UpdateManager.Instance.BackupFolder, LocalPath));
            }

            // Only enable execution if the apply attribute was set to hot-swap
            if (CanHotSwap)
{'fraction_non_alphanumeric': 0.08234946871310507, 'fraction_numerical': 0.005312868949232586, 'mean_word_length': 2.104443426477325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6953399', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1639, 'n_words': 456}