PRStatus FetchFile(PRFileDesc *in, PRFileDesc *out) { char buf[FCOPY_BUFFER_SIZE]; PRInt32 nBytes; while ((nBytes = DrainInputBuffer(buf, sizeof(buf))) > 0) { if (PR_Write(out, buf, nBytes) != nBytes) { fprintf(stderr, "httpget: cannot write to file\n"); return PR_FAILURE; } } if (nBytes < 0) { /* Input buffer is empty and end of stream */ return PR_SUCCESS; } while ((nBytes = PR_Read(in, buf, sizeof(buf))) > 0) { if (PR_Write(out, buf, nBytes) != nBytes) { fprintf(stderr, "httpget: cannot write to file\n"); return PR_FAILURE; } } if (nBytes < 0) { fprintf(stderr, "httpget: cannot read from socket\n"); return PR_FAILURE; } return PR_SUCCESS; }
PRStatus FastFetchFile(PRFileDesc *in, PRFileDesc *out, PRUint32 size) { PRInt32 nBytes; PRFileMap *outfMap; void *addr; char *start; PRUint32 rem; PRUint32 bytesToRead; PRStatus rv; PRInt64 sz64; LL_UI2L(sz64, size); outfMap = PR_CreateFileMap(out, sz64, PR_PROT_READWRITE); PR_ASSERT(outfMap); addr = PR_MemMap(outfMap, LL_ZERO, size); if (addr == (void *) -1) { fprintf(stderr, "cannot memory-map file: (%d, %d)\n", PR_GetError(), PR_GetOSError()); PR_CloseFileMap(outfMap); return PR_FAILURE; } PR_ASSERT(addr != (void *) -1); start = (char *) addr; rem = size; while ((nBytes = DrainInputBuffer(start, rem)) > 0) { start += nBytes; rem -= nBytes; } if (nBytes < 0) { /* Input buffer is empty and end of stream */ return PR_SUCCESS; } bytesToRead = (rem < FCOPY_BUFFER_SIZE) ? rem : FCOPY_BUFFER_SIZE; while (rem > 0 && (nBytes = PR_Read(in, start, bytesToRead)) > 0) { start += nBytes; rem -= nBytes; bytesToRead = (rem < FCOPY_BUFFER_SIZE) ? rem : FCOPY_BUFFER_SIZE; } if (nBytes < 0) { fprintf(stderr, "httpget: cannot read from socket\n"); return PR_FAILURE; } rv = PR_MemUnmap(addr, size); PR_ASSERT(rv == PR_SUCCESS); rv = PR_CloseFileMap(outfMap); PR_ASSERT(rv == PR_SUCCESS); return PR_SUCCESS; }