tuyau

Client/server for transfering files (like cp)
git clone https://noulin.net/git/tuyau.git
Log | Files | Refs | README

makeHeader.c (2483B)


      1 #include "libsheepyObject.h"
      2 #include "shpPackages/short/short.h"
      3 
      4 /*
      5 packet structures:
      6 0 - send file
      7   u8 command = 0
      8   u8 token[8]
      9   u16 filenameLength
     10   <filename>
     11   u16 filemode
     12   u16 pathLength
     13   <path>
     14   u64 filesize
     15   <filedata>
     16 1 - mkdir
     17   u8 command = 0
     18   u8 token[8]
     19   u16 filemode
     20   u16 pathLength
     21   <path>
     22 2 - download/receive files
     23   u8 command = 0
     24   u8 token[8]
     25   u16 pathLength
     26   <path>
     27 */
     28 
     29 void makeHeader(u8 *buf, u32 *bufip, u8 command, smallDictt *svrInfo, char *sendFile, char *destPath, u64 **filesize) {
     30 
     31   // command
     32   // token
     33   // file name max 8k
     34   // file mode
     35   // dest path
     36 
     37   lv(sendFile);
     38   lv(destPath);
     39 
     40   // command
     41   buf[0] = command;
     42   // token
     43   if (svrInfo)
     44     memcpy(buf + 1, $(svrInfo, "token"), 8);
     45   else
     46     // used for downloading files from server to client
     47     pError0(zeroBuf(buf + 1, 8));
     48 
     49   u32 bufi = 0;
     50   if (command == 0) {
     51     // send file
     52     // file name max 8k
     53     u16 *filenameLength = (u16*)(buf + 9);
     54     char *filename      = basename(sendFile);
     55     *filenameLength     = lenG(filename);
     56     memcpy(buf + 11, filename, lenG(filename));
     57     bufi                = 11 + lenG(filename);
     58     // file mode
     59     u16 *filemode       = (u16*)(buf + bufi);
     60     struct stat st;
     61     if(stat(sendFile, &st) != 0){
     62       logE("Can't access %s", sendFile);
     63       XFailure;
     64     }
     65     *filemode           = st.st_mode & 0xFFF;
     66     bufi += 2;
     67     // dest path
     68     u16 *pathLength     = (u16*)(buf + bufi);
     69     *pathLength         = lenG(destPath);
     70     bufi += 2;
     71     if (destPath) {
     72       memcpy(buf + bufi, destPath, *pathLength);
     73       bufi += *pathLength;
     74     }
     75     // file size
     76     *filesize           = (u64*)(buf + bufi);
     77     **filesize          = fileSizeG(sendFile);
     78     bufi += 8;
     79   }
     80   elif (command == 1) {
     81     // mkdir
     82     u16 *filemode = (u16*)(buf + 9);
     83     struct stat st;
     84     if(stat(sendFile, &st) != 0){
     85       logE("Can't access %s", sendFile);
     86       XFailure;
     87     }
     88     *filemode           = st.st_mode & 0xFFF;
     89     // dest path
     90     u16 *pathLength     = (u16*)(buf + 11);
     91     *pathLength         = lenG(destPath);
     92     memcpy(buf + 13, destPath, *pathLength);
     93     bufi                = 13 + lenG(destPath);
     94   }
     95   elif (command == 2) {
     96     // download/receive files
     97     // path in server
     98     u16 *pathLength     = (u16*)(buf + 9);
     99     *pathLength         = lenG(destPath);
    100     memcpy(buf + 11, destPath, *pathLength);
    101     bufi                = 11 + lenG(destPath);
    102   }
    103 
    104   *bufip = bufi;
    105 }
    106 // vim: set expandtab ts=2 sw=2: