Username: Password: oppure
Beatrix - FileManager.cpp

FileManager.cpp

Caricato da: Piero Tofy
Scarica il programma completo

  1. #include <fstream.h>
  2. #include "ServerProtocol.h"
  3. #include "FileManager.h"
  4. #include <windows.h>
  5.  
  6. /* FileManager.cpp | by PieroTofy 2005 http://www.pierotofy.it |
  7.  
  8. Si veda il file FileManager.h per l'utilizzo delle funzioni
  9. presenti nel modulo */
  10.  
  11.  
  12.  
  13.  
  14. void SendSubdirectoriesAndFilesEnum(SOCKET s, char *pTargetDir){
  15.         char *pFilename = new char[255]; /* Oggetto stringa che conterrà il filename */
  16.         char *pDir = new char[4096]; /* Sarà un percorso più lungo di 4096 chars?!? */
  17.         WIN32_FIND_DATA findData;
  18.        
  19.         /* Azzera la memoria di pDir */
  20.         memset(pDir,'\0',4096);
  21.  
  22.         /* E crea la stringa aggiungendo a pTargetDir "\*.*" */
  23.         lstrcat(pDir,pTargetDir);
  24.         lstrcat(pDir,"\\*.*");
  25.        
  26.         HANDLE hFind = FindFirstFile(pDir, &findData);
  27.  
  28.         if (hFind == INVALID_HANDLE_VALUE){
  29.                 SendTo(s,"Invalid hangle value when enumerating files and directories\n\0");
  30.                 return;
  31.         }
  32.  
  33.  
  34.         SendTo(s,"STARTINGENUM\n\0");
  35.         do{
  36.                 pFilename = findData.cFileName;
  37.  
  38.                 /* E' una directory? */
  39.                 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) SendObject(s,pFilename,T_DIRECTORY);
  40.                 /* No? Allora è un file */
  41.                 else  SendObject(s,pFilename,T_FILE);
  42.         }while(FindNextFile(hFind,&findData));
  43.  
  44.         SendTo(s,"EOE\n\0");
  45.  
  46. }
  47.  
  48. void SendObject(SOCKET s, char *pFilename, short tType){
  49.         char *pBuf = new char[0xFF];
  50.         wsprintf(pBuf,"%d%c%s\n\0",tType,SEPARATOR,pFilename);
  51.         SendTo(s,pBuf);
  52. }
  53.  
  54. BOOL MakeDirectory(char *pDir){
  55.         SECURITY_ATTRIBUTES *sa = new SECURITY_ATTRIBUTES;
  56.         sa->nLength = sizeof(SECURITY_ATTRIBUTES);
  57.         sa->bInheritHandle = FALSE;
  58.         sa->lpSecurityDescriptor = NULL;
  59.  
  60.         return CreateDirectory(pDir, sa);
  61. }
  62.  
  63. /* Funzione per vedere se esiste un file... */
  64. bool FileExists (char *sPath){
  65.         ifstream in(sPath,ios::nocreate);
  66.         if (!in){
  67.                 in.close();
  68.                 return false;
  69.         }else{
  70.                 in.close();
  71.                 return true;
  72.         }
  73. }
  74.  
  75. /* Funzione per inviare l'enumerazione dei drives logici */
  76. void SendLogicalDrivesEnum(SOCKET s){
  77.         char *pBuf = new char[0xFF];
  78.         memset(pBuf,'\0',0xFF);
  79.         int iLength = GetLogicalDriveStrings(0xFF,pBuf);
  80.  
  81.         /* Dobbiamo convertire il separatore predefinito (\0) con il nostro */
  82.         for (register int c = 0; c<iLength; c++)
  83.                 if (pBuf[c] == '\0') pBuf[c] = SEPARATOR;
  84.         pBuf[c] = '\n';
  85.         SendTo(s,pBuf);
  86. }
  87.  
  88. /* Funzione per inviare un file alla socket */
  89. void SendBinaryFile(SOCKET s, char *pFilename){
  90.         /* Controlla se esiste il file prima di procedere */
  91.         char *pBuf = new char[4096];
  92.         wsprintf(pBuf,"%s\n\0",OKCOMMAND);
  93.         int iReadData = 0;
  94.  
  95.         if (FileExists(pFilename)) SendTo(s,pBuf);
  96.         else {
  97.                 SendTo(s,"Unknown filename\n\0");
  98.                 return;
  99.         }
  100.  
  101.         /* Apre il file in lettura */
  102.         ifstream in(pFilename,ios::nocreate | ios::binary);
  103.  
  104.         /* Legge le dimensioni */
  105.         in.seekg (0, ios::end);
  106.         unsigned int length = in.tellg();
  107.         in.seekg (0, ios::beg);
  108.  
  109.         /* Comunichiamo al client le dimensioni */
  110.         wsprintf(pBuf,"%d\n\0",length);
  111.         SendTo(s,pBuf);
  112.  
  113.  
  114.         /* Invia il file a pezzetti */
  115.         while(!in.eof()){
  116.                 if ((1+length - iReadData) < BLOCK_LENGTH){
  117.                         in.read(pBuf,(1+length-iReadData));
  118.                         send(s,pBuf,(length-iReadData),0);
  119.                 }else{
  120.                         in.read(pBuf,BLOCK_LENGTH);
  121.                         send(s,pBuf,BLOCK_LENGTH,0);
  122.                 }
  123.                 iReadData += in.gcount();
  124.         }
  125. }
  126.  
  127. void ReceiveBinaryFile(SOCKET s, char *pFilename){
  128.         char *pBuffer = new char[BLOCK_LENGTH];
  129.  
  130.         //Apre lo stream di output
  131.         ofstream out(pFilename,ios::binary | ios::trunc);
  132.  
  133.         //Possiamo scrivere? Se si manda l'OK e procedi
  134.         wsprintf(pBuffer,"%s\n\0",OKCOMMAND);
  135.  
  136.         if (out){
  137.                 SendTo(s,pBuffer);
  138.  
  139.                 //Riceviamo le dimensioni del file
  140.                 RecvFrom(s,pBuffer);
  141.                 unsigned long int iTotBytes = atoi(pBuffer);
  142.                 unsigned long int iBytesCount = 0;
  143.                 unsigned long int iCount = sizeof(pBuffer);
  144.  
  145.                 while (iBytesCount != iTotBytes){
  146.                         if ((iTotBytes - iBytesCount) < iCount){
  147.                           iCount = iTotBytes - iBytesCount;
  148.                         }
  149.  
  150.                         //Legge i bytes dalla socket
  151.                         recv(s,pBuffer,iCount,0);
  152.  
  153.                         //Incrementa il conto dei bytes..
  154.                         iBytesCount += iCount;
  155.  
  156.                         //Li scrive sul file
  157.                         out.write(pBuffer,iCount);
  158.                 }
  159.  
  160.                 out.flush();
  161.                 out.close();
  162.         }else SendTo(s,"Cannot open the outstream!\n\0");
  163. }
  164.  
  165. /* TODO: RemoveDirectory con ricorsione sui files */