示例#1
0
static psync_fileid_t psync_p2p_has_file(const unsigned char *hashstart, const unsigned char *genhash, const unsigned char *rand, uint64_t filesize,
                                         unsigned char *realhash){
  psync_sql_res *res;
  psync_variant_row row;
  psync_fileid_t ret;
  unsigned char hashsource[PSYNC_HASH_BLOCK_SIZE], hashbin[PSYNC_HASH_DIGEST_LEN], hashhex[PSYNC_HASH_DIGEST_HEXLEN];
  char like[PSYNC_P2P_HEXHASH_BYTES+1];
  memcpy(like, hashstart, PSYNC_P2P_HEXHASH_BYTES);
  like[PSYNC_P2P_HEXHASH_BYTES]='%';
  memcpy(hashsource+PSYNC_HASH_DIGEST_HEXLEN, rand, PSYNC_HASH_BLOCK_SIZE-PSYNC_HASH_DIGEST_HEXLEN);
  res=psync_sql_query_rdlock("SELECT id, checksum FROM localfile WHERE checksum LIKE ? AND size=?");
  psync_sql_bind_lstring(res, 1, like, PSYNC_P2P_HEXHASH_BYTES+1);
  psync_sql_bind_uint(res, 2, filesize);
  while ((row=psync_sql_fetch_row(res))){
    assertw(row[1].type==PSYNC_TSTRING && row[1].length==PSYNC_HASH_DIGEST_HEXLEN);
    memcpy(hashsource, row[1].str, PSYNC_HASH_DIGEST_HEXLEN);
    psync_hash(hashsource, PSYNC_HASH_BLOCK_SIZE, hashbin);
    psync_binhex(hashhex, hashbin, PSYNC_HASH_DIGEST_LEN);
    if (!memcmp(hashhex, genhash, PSYNC_HASH_DIGEST_HEXLEN)){
      if (realhash)
        memcpy(realhash, row[1].str, PSYNC_HASH_DIGEST_HEXLEN);
      ret=psync_get_number(row[0]);
      psync_sql_free_result(res);
      return ret;
    }
  }
  psync_sql_free_result(res);
  return 0;
}
示例#2
0
void psync_decrease_local_folder_taskcnt(psync_folderid_t lfolderid){
  psync_sql_res *res;
  res=psync_sql_prep_statement("UPDATE localfolder SET taskcnt=taskcnt+1 WHERE id=?");
  psync_sql_bind_uint(res, 1, lfolderid);
  psync_sql_run_free(res);
  assertw(psync_sql_affected_rows()==1);
}
 virtual pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
     Warning("untested");
     assertx(!(which&std::ios_base::in));
     if (which&std::ios_base::out) {
         assertw(!fseek(_file, assert_narrow_cast<long>(pos), SEEK_SET));
     }
     return ftell(_file);
 }
 virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override {
     Warning("untested");
     assertx(!(which&std::ios_base::in));
     if (which&std::ios_base::out) {
         assertw(!fseek(_file, assert_narrow_cast<long>(off), seekdir_to_origin(dir)));
     }
     return ftell(_file);
 }
 virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override {
     Warning("untested");
     assertx(!(which&std::ios_base::out));
     if (which&std::ios_base::in) {
         if (assertw(!fseek(_file, assert_narrow_cast<long>(off), seekdir_to_origin(dir))) &&
             !(dir==std::ios_base::cur && off==0)) // don't clear buffer if called from tellg()
             setg(_buffer0, _buffer0, _buffer0);   // only clear if successful
     }
     return ftell(_file);
 }
示例#6
0
static void psync_p2p_wake(){
  psync_socket_t sock;
  struct sockaddr_in addr;
  packet_type_t pack;
  sock=psync_create_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (unlikely_log(sock==INVALID_SOCKET))
    return;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family=AF_INET;
  addr.sin_port=htons(PSYNC_P2P_PORT);
  addr.sin_addr.s_addr=htonl(0x7f000001UL);
  pack=P2P_WAKE;
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr))!=SOCKET_ERROR)
    assertw(psync_write_socket(sock, &pack, sizeof(pack))==sizeof(pack));
  psync_close_socket(sock);
}