#include <fstream.h>
#include "ServerProtocol.h"
#include "FileManager.h"
#include <windows.h>
/* FileManager.cpp | by PieroTofy 2005 http://www.pierotofy.it |
Si veda il file FileManager.h per l'utilizzo delle funzioni
presenti nel modulo */
void SendSubdirectoriesAndFilesEnum(SOCKET s, char *pTargetDir){
char *pFilename = new char[255]; /* Oggetto stringa che conterrà il filename */
char *pDir = new char[4096]; /* Sarà un percorso più lungo di 4096 chars?!? */
WIN32_FIND_DATA findData;
/* Azzera la memoria di pDir */
memset(pDir,'\0',4096);
/* E crea la stringa aggiungendo a pTargetDir "\*.*" */
lstrcat(pDir,pTargetDir);
lstrcat(pDir,"\\*.*");
HANDLE hFind = FindFirstFile(pDir, &findData);
if (hFind == INVALID_HANDLE_VALUE){
SendTo(s,"Invalid hangle value when enumerating files and directories\n\0");
return;
}
SendTo(s,"STARTINGENUM\n\0");
do{
pFilename = findData.cFileName;
/* E' una directory? */
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) SendObject(s,pFilename,T_DIRECTORY);
/* No? Allora è un file */
else SendObject(s,pFilename,T_FILE);
}while(FindNextFile(hFind,&findData));
SendTo(s,"EOE\n\0");
}
void SendObject(SOCKET s, char *pFilename, short tType){
char *pBuf = new char[0xFF];
wsprintf(pBuf,"%d%c%s\n\0",tType,SEPARATOR,pFilename);
SendTo(s,pBuf);
}
BOOL MakeDirectory(char *pDir){
SECURITY_ATTRIBUTES *sa = new SECURITY_ATTRIBUTES;
sa->nLength = sizeof(SECURITY_ATTRIBUTES);
sa->bInheritHandle = FALSE;
sa->lpSecurityDescriptor = NULL;
return CreateDirectory(pDir, sa);
}
/* Funzione per vedere se esiste un file... */
bool FileExists (char *sPath){
ifstream in(sPath,ios::nocreate);
if (!in){
in.close();
return false;
}else{
in.close();
return true;
}
}
/* Funzione per inviare l'enumerazione dei drives logici */
void SendLogicalDrivesEnum(SOCKET s){
char *pBuf = new char[0xFF];
memset(pBuf,'\0',0xFF);
int iLength = GetLogicalDriveStrings(0xFF,pBuf);
/* Dobbiamo convertire il separatore predefinito (\0) con il nostro */
for (register int c = 0; c<iLength; c++)
if (pBuf[c] == '\0') pBuf[c] = SEPARATOR;
pBuf[c] = '\n';
SendTo(s,pBuf);
}
/* Funzione per inviare un file alla socket */
void SendBinaryFile(SOCKET s, char *pFilename){
/* Controlla se esiste il file prima di procedere */
char *pBuf = new char[4096];
wsprintf(pBuf,"%s\n\0",OKCOMMAND);
int iReadData = 0;
if (FileExists(pFilename)) SendTo(s,pBuf);
else {
SendTo(s,"Unknown filename\n\0");
return;
}
/* Apre il file in lettura */
ifstream in(pFilename,ios::nocreate | ios::binary);
/* Legge le dimensioni */
in.seekg (0, ios::end);
unsigned int length = in.tellg();
in.seekg (0, ios::beg);
/* Comunichiamo al client le dimensioni */
wsprintf(pBuf,"%d\n\0",length);
SendTo(s,pBuf);
/* Invia il file a pezzetti */
while(!in.eof()){
if ((1+length - iReadData) < BLOCK_LENGTH){
in.read(pBuf,(1+length-iReadData));
send(s,pBuf,(length-iReadData),0);
}else{
in.read(pBuf,BLOCK_LENGTH);
send(s,pBuf,BLOCK_LENGTH,0);
}
iReadData += in.gcount();
}
}
void ReceiveBinaryFile(SOCKET s, char *pFilename){
char *pBuffer = new char[BLOCK_LENGTH];
//Apre lo stream di output
ofstream out(pFilename,ios::binary | ios::trunc);
//Possiamo scrivere? Se si manda l'OK e procedi
wsprintf(pBuffer,"%s\n\0",OKCOMMAND);
if (out){
SendTo(s,pBuffer);
//Riceviamo le dimensioni del file
RecvFrom(s,pBuffer);
unsigned long int iTotBytes = atoi(pBuffer);
unsigned long int iBytesCount = 0;
unsigned long int iCount = sizeof(pBuffer);
while (iBytesCount != iTotBytes){
if ((iTotBytes - iBytesCount) < iCount){
iCount = iTotBytes - iBytesCount;
}
//Legge i bytes dalla socket
recv(s,pBuffer,iCount,0);
//Incrementa il conto dei bytes..
iBytesCount += iCount;
//Li scrive sul file
out.write(pBuffer,iCount);
}
out.flush();
out.close();
}else SendTo(s,"Cannot open the outstream!\n\0");
}
/* TODO: RemoveDirectory con ricorsione sui files */