123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #include "TestUtil.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <cerrno>
- #include <cstring>
- #include <sstream>
- #include <fstream>
- #include "a2io.h"
- #include "File.h"
- #include "FatalException.h"
- #include "Cookie.h"
- #include "DefaultDiskWriter.h"
- #include "fmt.h"
- #include "util.h"
- #ifdef ENABLE_MESSAGE_DIGEST
- # include "message_digest_helper.h"
- #endif // ENABLE_MESSAGE_DIGEST
- namespace aria2 {
- void createFile(const std::string& path, size_t length)
- {
- File(File(path).getDirname()).mkdirs();
- int fd = creat(path.c_str(), OPEN_MODE);
- if(fd == -1) {
- throw FATAL_EXCEPTION(fmt("Could not create file=%s. cause:%s",
- path.c_str(),
- strerror(errno)));
- }
- if(-1 == ftruncate(fd, length)) {
- throw FATAL_EXCEPTION(fmt("ftruncate failed. cause:%s", strerror(errno)));
- }
- close(fd);
- }
- std::string readFile(const std::string& path)
- {
- std::stringstream ss;
- std::ifstream in(path.c_str(), std::ios::binary);
- char buf[4096];
- while(1) {
- in.read(buf, sizeof(buf));
- ss.write(buf, in.gcount());
- if(in.gcount() != sizeof(buf)) {
- break;
- }
- }
- return ss.str();
- }
- Cookie createCookie
- (const std::string& name,
- const std::string& value,
- const std::string& domain,
- bool hostOnly,
- const std::string& path,
- bool secure)
- {
- return Cookie
- (name, value, 0, false, domain, hostOnly, path, secure, false, 0);
- }
- Cookie createCookie
- (const std::string& name,
- const std::string& value,
- time_t expiryTime,
- const std::string& domain,
- bool hostOnly,
- const std::string& path,
- bool secure)
- {
- return Cookie
- (name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
- }
- std::string fromHex(const std::string& s)
- {
- return util::fromHex(s.begin(), s.end());
- }
- #ifdef ENABLE_MESSAGE_DIGEST
- std::string fileHexDigest
- (const SharedHandle<MessageDigest>& ctx, const std::string& filename)
- {
- SharedHandle<DiskWriter> writer(new DefaultDiskWriter(filename));
- writer->openExistingFile();
- return util::toHex(message_digest::digest(ctx, writer, 0, writer->size()));
- }
- #endif // ENABLE_MESSAGE_DIGEST
- WrDiskCacheEntry::DataCell* createDataCell(int64_t goff,
- const char* data,
- size_t offset)
- {
- WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
- cell->goff = goff;
- size_t len = strlen(data);
- cell->data = new unsigned char[len];
- memcpy(cell->data, data, len);
- cell->offset = offset;
- cell->len = cell->capacity = len - offset;
- return cell;
- }
- } // namespace aria2
|