public override bool Write(string destFolder) { string fileName = InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName); string fullPathToFile = Path.Combine(destFolder, fileName); string root = Path.GetDirectoryName(fullPathToFile); if (Directory.CreateDirectory(root) != null) { string targetFile = InternalWriteArchiveEntry.GetTargetFileToLink(_internalEntry.Data); string fullPathToTargetFile = Path.Combine(destFolder, targetFile); if (WindowsNativeLibrary.CreateSymbolicLink(fullPathToFile, fullPathToTargetFile, SYMBOLIC_LINK_FLAG.File)) { if ((_internalEntry.ExtractFlags & (uint)CpioExtractFlags.ARCHIVE_EXTRACT_TIME) > 0) { SetSymLinkLastWriteTime(fullPathToFile, _internalEntry.mTime); } return(true); } else { throw new Exception(string.Format("Symbolic link for file {0} no created. Win32Error: {1}", fullPathToFile, Marshal.GetLastWin32Error())); } } return(false); }
private bool SetSymLinkLastWriteTime(string fileName, DateTime lastWriteTime) { SafeFileHandle handle = WindowsNativeLibrary.CreateFile(fileName, WindowsNativeLibrary.FileAccess.FILE_WRITE_ATTRIBUTES, FileShare.None, IntPtr.Zero, (FileMode)3, FileAttributes.ReparsePoint, IntPtr.Zero); if (handle.IsInvalid) { return(false); } //long lpCreationTime = File.GetCreationTimeUtc(fileName).ToFileTimeUtc(); //long lpLastAccessTime = File.GetLastAccessTimeUtc(fileName).ToFileTimeUtc(); long lpLastWriteTime = lastWriteTime.ToFileTimeUtc(); if (!WindowsNativeLibrary.SetFileTime(handle, ref lpLastWriteTime, ref lpLastWriteTime, ref lpLastWriteTime)) { Console.WriteLine(Marshal.GetLastWin32Error()); return(false); } return(true); }
public override bool Write(string destFolder) { string fileName = InternalWriteArchiveEntry.GetFileName(_internalEntry.FileName); string fullPathToFile = Path.Combine(destFolder, fileName); string root = Path.GetDirectoryName(fullPathToFile); if (Directory.CreateDirectory(root) != null) { if (OriginalFilePath == null) { // write as simple file using (FileStream fs = new FileStream(fullPathToFile, FileMode.Create)) { if (_internalEntry.Data != null) { var data = _internalEntry.Data.Where(g => g != '\0').ToArray(); fs.Write(data, 0, data.Length); } } return(true); } else { string fullPathToTargetFile = Path.Combine(destFolder, OriginalFilePath); if (WindowsNativeLibrary.CreateHardLink(fullPathToFile, fullPathToTargetFile, IntPtr.Zero)) { if ((_internalEntry.ExtractFlags & (uint)CpioExtractFlags.ARCHIVE_EXTRACT_TIME) > 0) { File.SetLastWriteTimeUtc(fullPathToFile, _internalEntry.mTime); } return(true); } else { throw new Exception(string.Format("Hardlink for file {0} no created. Win32Error: {1}", fullPathToFile, Marshal.GetLastWin32Error())); } } } return(false); }