81 lines
2.6 KiB
C#
81 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace DearFTP.Connection.Commands
|
|
{
|
|
class RenameCommand : ICommand
|
|
{
|
|
public string[] Aliases { get; } = new string[]
|
|
{
|
|
"RNFR"
|
|
};
|
|
|
|
public void Execute(Session session, FtpStream stream, string alias, string argument)
|
|
{
|
|
var sourceFile = session.NavigablePath.GetSystemFilePath(argument);
|
|
|
|
if (sourceFile.navigablePath == null)
|
|
{
|
|
stream.Send(ResponseCode.FileUnavailable, "File does not exist.");
|
|
return;
|
|
}
|
|
|
|
if (sourceFile.navigablePath.CurrentDirectory == "/")
|
|
{
|
|
stream.Send(ResponseCode.FileUnavailable, "Operation not allowed: can't remove a share.");
|
|
return;
|
|
}
|
|
|
|
if (!session.WritablesShares.Contains(sourceFile.navigablePath.CurrentShare))
|
|
{
|
|
stream.Send(ResponseCode.FileUnavailable, "You don't have write access to this file.");
|
|
return;
|
|
}
|
|
|
|
stream.Send(ResponseCode.PendingFurtherInformation, "Waiting destination.");
|
|
|
|
(string command, string destination) = stream.Receive();
|
|
|
|
if (command.ToUpper() != "RNTO")
|
|
{
|
|
stream.Send(ResponseCode.BadSequence, "Expected a RNTO command.");
|
|
return;
|
|
}
|
|
|
|
var destinationPath = session.NavigablePath.GetNewSystemFilePath(destination);
|
|
|
|
if (destinationPath.realPath == null)
|
|
{
|
|
stream.Send(ResponseCode.FileUnavailable, "Invalid destination path.");
|
|
return;
|
|
}
|
|
|
|
if (File.Exists(destinationPath.realPath) || Directory.Exists(destinationPath.realPath))
|
|
{
|
|
stream.Send(ResponseCode.FileUnavailable, "Destination path already exists.");
|
|
return;
|
|
}
|
|
|
|
if (!session.WritablesShares.Contains(destinationPath.navigablePath.CurrentShare))
|
|
{
|
|
stream.Send(ResponseCode.FileUnavailable, "You don't have write access to this file.");
|
|
return;
|
|
}
|
|
|
|
if (sourceFile.isDirectory)
|
|
{
|
|
Directory.Move(sourceFile.realPath, destinationPath.realPath);
|
|
}
|
|
else
|
|
{
|
|
File.Move(sourceFile.realPath, destinationPath.realPath);
|
|
}
|
|
|
|
stream.Send(ResponseCode.FileActionOK, "File successfully renamed.");
|
|
}
|
|
}
|
|
}
|