internal void SaveDirectoryInfo(SMBDirectoryInfo dirInfo, ISMBCredential credential = null) { var path = dirInfo.FullName; if (!path.TryResolveHostnameFromPath(out var ipAddress)) { throw new SMBException($"Failed to SaveDirectoryInfo for {path}", new ArgumentException($"Unable to resolve \"{path.Hostname()}\"")); } NTStatus status = NTStatus.STATUS_SUCCESS; if (credential == null) { credential = _credentialProvider.GetSMBCredential(path); } if (credential == null) { throw new SMBException($"Failed to SaveDirectoryInfo for {path}", new InvalidCredentialException($"Unable to find credential for path: {path}")); } ISMBFileStore fileStore = null; object handle = null; try { var shareName = path.ShareName(); var relativePath = path.RelativeSharePath(); _logger?.LogTrace($"Trying to SaveDirectoryInfo {{RelativePath: {relativePath}}} for {{ShareName: {shareName}}}"); using var connection = SMBConnection.CreateSMBConnection(_smbClientFactory, ipAddress, transport, credential, _maxBufferSize); fileStore = connection.SMBClient.TreeConnect(shareName, out status); status.HandleStatus(); status = fileStore.CreateFile(out handle, out FileStatus fileStatus, relativePath, AccessMask.GENERIC_WRITE, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null); status.HandleStatus(); var fileInfo = dirInfo.ToSMBFileInformation(credential); status = fileStore.SetFileInformation(handle, fileInfo); status.HandleStatus(); } catch (Exception ex) { throw new SMBException($"Failed to SaveDirectoryInfo for {path}", ex); } finally { FileStoreUtilities.CloseFile(fileStore, ref handle); } }
public void SetFileInformation(string filename, bool isDirectory, FileInformation fileInformation, object handle = null) { bool didCreateHandle = handle == null; if (handle == null) { handle = OpenFile(filename, isDirectory, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); } try { ThrowOnError(FileStore.SetFileInformation(handle, fileInformation)); } finally { if (didCreateHandle) { CloseFile(handle); } } }
public void Rename(string path, string newName) { NTStatus status = fileStore.CreateFile(out object fileHandle, out FileStatus fileStatus, path, AccessMask.GENERIC_ALL, FileAttributes.Normal, ShareAccess.Read, CreateDisposition.FILE_OPEN, 0, null); if (status != NTStatus.STATUS_SUCCESS) { throw new IOException($"Failed to rename file: {status}"); } FileRenameInformationType2 fileRenameInformation = new FileRenameInformationType2(); string parentPath = Path.GetDirectoryName(path) ?? string.Empty; fileRenameInformation.FileName = Path.Combine(parentPath, newName); fileRenameInformation.ReplaceIfExists = false; status = fileStore.SetFileInformation(fileHandle, fileRenameInformation); fileStore.CloseFile(fileHandle); if (status != NTStatus.STATUS_SUCCESS) { throw new IOException($"Failed to rename file: {status}"); } }
internal void SaveDirectoryInfo(SMBDirectoryInfo dirInfo, ISMBCredential credential = null) { var path = dirInfo.FullName; if (!path.TryResolveHostnameFromPath(out var ipAddress)) { throw new ArgumentException($"Unable to resolve \"{path.Hostname()}\""); } NTStatus status = NTStatus.STATUS_SUCCESS; if (credential == null) { credential = _credentialProvider.GetSMBCredential(path); } if (credential == null) { throw new Exception($"Unable to find credential for path: {path}"); } using var connection = SMBConnection.CreateSMBConnection(_smbClientFactory, ipAddress, transport, credential, _maxBufferSize); var shareName = path.ShareName(); var relativePath = path.RelativeSharePath(); ISMBFileStore fileStore = connection.SMBClient.TreeConnect(shareName, out status); status.HandleStatus(); status = fileStore.CreateFile(out object handle, out FileStatus fileStatus, relativePath, AccessMask.GENERIC_WRITE, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null); status.HandleStatus(); var fileInfo = dirInfo.ToSMBFileInformation(credential); status = fileStore.SetFileInformation(handle, fileInfo); status.HandleStatus(); }