TEST(JemallocNodumpAllocatorTest, IOBuf) { folly::JemallocNodumpAllocator jna; #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED if (folly::usingJEMalloc()) { EXPECT_NE(0, jna.getArenaIndex()); } #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED const size_t size{1024}; void* ptr = jna.allocate(size); EXPECT_NE(nullptr, ptr); folly::IOBuf ioBuf(folly::IOBuf::TAKE_OWNERSHIP, ptr, size); EXPECT_EQ(size, ioBuf.capacity()); EXPECT_EQ(ptr, ioBuf.data()); uint8_t* data = ioBuf.writableData(); EXPECT_EQ(ptr, data); for (auto i = 0u; i < ioBuf.capacity(); ++i) { data[i] = 'A'; } uint8_t* p = static_cast<uint8_t*> (ptr); for (auto i = 0u; i < size; ++i) { EXPECT_EQ('A', p[i]); } }
sBool sMiniFTPClient::GetFile(const sChar *filename,sFile *writeTo) { sSize currentPos = 0; if(State == CS_NOTARGET) return sFALSE; for(;;) { if(State != CS_CONNECTED && !TryReconnect(RetryCount)) break; sU8 sizeBuf[8]; if(SendCommand(sMFC_GET,filename,currentPos) && ReadAll(sizeBuf,8)) { sSize totalSize; sUnalignedLittleEndianLoad64(sizeBuf,(sU64&) totalSize); if(!Progress(filename,currentPos,totalSize,ProgressUser)) return MaybeNextTime(); sFixedArray<sU8> ioBuf(FileIOBufferSize); while(currentPos < totalSize) { sDInt size = (sDInt) sMin<sS64>(totalSize-currentPos,ioBuf.GetSize()); sDInt read; if(!Socket.Read(&ioBuf[0],size,read) || !read) { MaybeNextTime(); break; } if(!writeTo->Write(&ioBuf[0],read)) return sFALSE; currentPos += read; if(!Progress(filename,currentPos,totalSize,ProgressUser)) return MaybeNextTime(); } if(currentPos == totalSize) return sTRUE; } else if(Error == sMFE_OK) MaybeNextTime(); else break; } return sFALSE; }
sBool sMiniFTPClient::PutFile(const sChar *filename,sFile *readFrom) { sSize currentPos = 0; sSize totalSize = readFrom->GetSize(); if(State == CS_NOTARGET) return sFALSE; for(;;) { if(State != CS_CONNECTED && !TryReconnect(RetryCount)) break; if(!ContinueUploads) currentPos = 0; if(SendCommand(sMFC_PUT,filename,currentPos)) { sU8 sizeBuf[8]; sUnalignedLittleEndianStore64(sizeBuf,totalSize); if(!WriteAll(sizeBuf,8)) continue; if(!Progress(filename,currentPos,totalSize,ProgressUser)) return MaybeNextTime(); sFixedArray<sU8> ioBuf(FileIOBufferSize); while(currentPos < totalSize) { sInt size = (sInt) sMin<sS64>(totalSize-currentPos,ioBuf.GetSize()); if(!readFrom->Read(&ioBuf[0],size)) return MaybeNextTime(); if(!WriteAll(&ioBuf[0],size)) break; currentPos += size; if(!Progress(filename,currentPos,totalSize,ProgressUser)) return sFALSE; } if(currentPos == totalSize) return sTRUE; } else if(Error == sMFE_OK) MaybeNextTime(); else break; } return sFALSE; }
sBool sMiniFTPClient::ListFiles(const sChar *basepath,sArray<sChar> &listing) { listing.Clear(); if(State == CS_NOTARGET) return sFALSE; for(;;) { if(State != CS_CONNECTED && !TryReconnect(RetryCount)) break; sU8 sizeBuf[8]; if(SendCommand(sMFC_LIST,basepath,0) && ReadAll(sizeBuf,8)) { sSize totalSize; sUnalignedLittleEndianLoad64(sizeBuf,(sU64&) totalSize); if((totalSize & 1) // we expect 16bit chars || totalSize > 16*1024*1024) // 16MB limit for directory listings (for now) return sFALSE; if(!Progress(basepath,0,totalSize,ProgressUser)) return MaybeNextTime(); sFixedArray<sU8> ioBuf((sInt) totalSize); sSize currentPos = 0; while(currentPos < totalSize) { sDInt size = (sDInt) (totalSize-currentPos); sDInt read; if(!Socket.Read(&ioBuf[(sInt) currentPos],size,read) || !read) { MaybeNextTime(); break; } currentPos += read; if(!Progress(basepath,currentPos,totalSize,ProgressUser)) return MaybeNextTime(); } if(currentPos == totalSize) { listing.HintSize(sU32(totalSize/2)); listing.AddMany(sU32(totalSize/2)); for(sInt i=0;i<totalSize/2;i++) { sU16 v; sUnalignedLittleEndianLoad16(&ioBuf[i*2],v); listing[i] = v; } return sTRUE; } } else if(Error == sMFE_OK) MaybeNextTime(); else break; } return sFALSE; }