Merge branch 'fix_feature/fileCommands_CP' into dev

This commit is contained in:
HorlogeSkynet
2017-03-05 22:48:00 +01:00
2 changed files with 73 additions and 15 deletions

@ -16,7 +16,7 @@ from socketCommands import sendData
__authors__ = "HorlogeSkynet, Tatyik"
__copyright__ = "Copyright 2017, ACMS"
__license__ = "GPLv3"
__version__ = "0.1.0"
__version__ = "0.1.3"
__status__ = "Development"
__date__ = "03/03/2017"
@ -31,7 +31,7 @@ def removeFile(command, client):
os.rmdir(client.currentWorkingDir + command[1])
except:
sendData(client.sock, "The directory is not empty.")
sendData(client.sock, "The directory is not empty. Use \'-r\' option.")
else:
sendData(client.sock, command[1] + " has been removed.")
@ -125,24 +125,28 @@ def changeDirectory(command, client):
sendData(client.sock, "NEW_PATH:" + '/' + client.currentWorkingDir.partition(client.HOME_DIR)[2])
elif nbArgs == 2 and command[1].count('..') == 0:
elif nbArgs == 2 and command[1].count('.') == 0:
if os.path.exists(client.currentWorkingDir + command[1]):
client.currentWorkingDir += command[1] + '/'
client.currentWorkingDir += command[1]
if not client.currentWorkingDir.endswith('/'):
client.currentWorkingDir += '/'
sendData(client.sock, "NEW_PATH:" + '/' + client.currentWorkingDir.partition(client.HOME_DIR)[2])
else:
sendData(client.sock, "Unknown path.")
else:
sendData(client.sock, "Wrong number of arguments.")
sendData(client.sock, "Wrong number of arguments (or too many root directory accesses).")
def listFiles(command, client):
nbArgs = len(command)
if nbArgs == 1 or (nbArgs == 2 and command.count("-l") == 0):
if nbArgs == 2:
if not os.path.exists(client.currentWorkingDir + command[1]):
if not os.path.exists(client.currentWorkingDir + command[1]) or command[1].count('..') != 0:
sendData(client.sock, "Unknown path.")
return
files = os.listdir(client.currentWorkingDir if nbArgs == 1 else client.currentWorkingDir + command[1])
if len(files) == 0:
@ -158,7 +162,7 @@ def listFiles(command, client):
if command[1] == "-l":
command[1], command[2] = command[2], command[1]
if os.path.exists(client.currentWorkingDir + command[1]):
if os.path.exists(client.currentWorkingDir + command[1]) or command[1].count('..') != 0:
path += command[1] + '/'
else:
@ -176,19 +180,70 @@ def listFiles(command, client):
for file in files:
if os.path.isdir(path + file):
message = message + "d "
message += "d "
elif os.path.isfile(path + file):
message = message + "- "
message += "- "
elif os.path.isabs(path + file):
message = message + "a "
message += "a "
elif os.path.islink(path + file):
message = message + "l "
message += "l "
else:
message += '? '
message += ' ' * (tailleMax - len(str(os.path.getsize(path + file))))
message = message + (str(os.path.getsize(path + file)) + ' ' + str(datetime.datetime.fromtimestamp(os.path.getmtime(path + file))) + ' ' + file + '\n')
message += str(os.path.getsize(path + file)) + ' ' + str(datetime.datetime.fromtimestamp(os.path.getmtime(path + file))) + ' ' + file + '\n'
sendData(client.sock, message)
else:
sendData(client.sock, "Wrong number of (or invalid) arguments.")
def copyFile(command, client):
nbArgs = len(command)
if nbArgs == 3:
if not os.path.exists(client.currentWorkingDir + command[2]):
if not os.path.isdir(client.currentWorkingDir + command[1]):
with client.mutex:
try:
shutil.copy(client.currentWorkingDir + command[1], client.currentWorkingDir + command[2])
sendData(client.sock, "File successfully copied !")
except:
sendData(client.sock, "An error occurred while copying your file...")
else:
sendData(client.sock, "You want to copy a directory ? Use \'-r\' option.")
else:
sendData(client.sock, "The destination specified already exist !")
elif nbArgs == 4:
if command[2] == "-r":
command[2], command[1] = command[1], command[2]
elif command[3] == "-r":
command[3], command[1] = command[1], command[3]
if not os.path.exists(client.currentWorkingDir + command[3]):
with client.mutex:
if os.path.isdir(client.currentWorkingDir + command[2]):
try:
shutil.copytree(client.currentWorkingDir + command[2], client.currentWorkingDir + command[3])
sendData(client.sock, "Directory and its content has been successfully copied.")
except:
sendData(client.sock, "An error occurred while copying your directory...")
else:
try:
shutil.copy(client.currentWorkingDir + command[2], client.currentWorkingDir + command[3])
sendData(client.sock, "Omitted \'-r\' option, but file successfully copied.")
except:
sendData(client.sock, "An error occurred while copying your file...")
else:
sendData(client.sock, "The destination specified already exist !")
else:
sendData(client.sock, "Wrong number of arguments.")

@ -16,13 +16,13 @@ import threading
from UserList import UserList
from ACL import AccessControlList
from socketCommands import sendData, recvData
from fileCommands import listFiles, changeDirectory, makeDirectory, moveFile, removeFile
from fileCommands import listFiles, changeDirectory, makeDirectory, moveFile, removeFile, copyFile
__authors__ = "HorlogeSkynet, Tatyik"
__copyright__ = "Copyright 2017, ACMS"
__license__ = "GPLv3"
__version__ = "0.1.12"
__version__ = "0.1.13"
__status__ = "Development"
__date__ = "02/22/2017"
@ -161,6 +161,9 @@ def computeCommand(command, client):
elif command[0] == 'rm':
removeFile(command, client)
elif command[0] == 'cp':
copyFile(command, client)
elif command[0] == 'clear':
sendData(client.sock, 'CLEAR_SCREEN')