TemporaryDirectory::TemporaryDirectory(const std::string& aDirPath) { theDirPath = aDirPath; boost::filesystem::path myDirectory(theDirPath); if(!boost::filesystem::exists(myDirectory)) { std::string myError("Directory "); myError += theDirPath; myError += " does not exist"; throw std::runtime_error(myError); } if ( ! boost::filesystem::is_directory(myDirectory) ) { std::string myError = theDirPath; myError += " is not a directory"; throw std::runtime_error(myError); } boost::filesystem::directory_iterator myEndIter; boost::filesystem::directory_iterator myDirIter(myDirectory); for(/*void*/; myDirIter != myEndIter; ++myDirIter) { TemporaryFile * myFile = new TemporaryFile(theDirPath + "/" + myDirIter->leaf() ); theFiles.insert(std::make_pair(myFile->getFileName(), myFile)); } }
TEST(String, readFile) { const TemporaryFile afileTemp, emptyFileTemp; auto afile = afileTemp.path().string(); auto emptyFile = emptyFileTemp.path().string(); EXPECT_TRUE(writeFile(string(), emptyFile.c_str())); EXPECT_TRUE(writeFile(StringPiece("bar"), afile.c_str())); { string contents; EXPECT_TRUE(readFile(emptyFile.c_str(), contents)); EXPECT_EQ(contents, ""); EXPECT_TRUE(readFile(afile.c_str(), contents, 0)); EXPECT_EQ("", contents); EXPECT_TRUE(readFile(afile.c_str(), contents, 2)); EXPECT_EQ("ba", contents); EXPECT_TRUE(readFile(afile.c_str(), contents)); EXPECT_EQ("bar", contents); } { vector<unsigned char> contents; EXPECT_TRUE(readFile(emptyFile.c_str(), contents)); EXPECT_EQ(vector<unsigned char>(), contents); EXPECT_TRUE(readFile(afile.c_str(), contents, 0)); EXPECT_EQ(vector<unsigned char>(), contents); EXPECT_TRUE(readFile(afile.c_str(), contents, 2)); EXPECT_EQ(vector<unsigned char>({'b', 'a'}), contents); EXPECT_TRUE(readFile(afile.c_str(), contents)); EXPECT_EQ(vector<unsigned char>({'b', 'a', 'r'}), contents); } }
void test_write_empty_workbook() { xlnt::workbook wb; TemporaryFile file; xlnt::save_workbook(wb, file.GetFilename()); TS_ASSERT(PathHelper::FileExists(file.GetFilename())); }
TEST(RecordIOTest, Simple) { TemporaryFile file; { RecordIOWriter writer(File(file.fd())); writer.write(iobufs({"hello ", "world"})); writer.write(iobufs({"goodbye"})); } { RecordIOReader reader(File(file.fd())); auto it = reader.begin(); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("hello world", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("goodbye", sp((it++)->first)); EXPECT_TRUE(it == reader.end()); } { RecordIOWriter writer(File(file.fd())); writer.write(iobufs({"meow"})); writer.write(iobufs({"woof"})); } { RecordIOReader reader(File(file.fd())); auto it = reader.begin(); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("hello world", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("goodbye", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("meow", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("woof", sp((it++)->first)); EXPECT_TRUE(it == reader.end()); } }
/** * Loads a block from disk into the cache. */ void load_block(size_t b_index) { if (!data_table[b_index]) { // Find the least recently used block in the cache size_t cb_index = 0; size_t p = cache_info[0].priority; for (size_t i=1; i < cache_size; i++) { if (cache_info[i].priority < p) { cb_index = i; p = cache_info[i].priority; } } // Unload prior block at this location unload_cache_block(cb_index); // Set table reference data_table[b_index] = &(cache_info[cb_index]); // Fill block info data_table[b_index]->index = b_index; data_table[b_index]->c_index = cb_index*BLOCK_SIZE; data_table[b_index]->modified = false; data_table[b_index]->used = true; // Load block data_file.seekg(sizeof(T)*BLOCK_SIZE*b_index); data_file.read((char*)(&(cache[cb_index*BLOCK_SIZE])), sizeof(T)*BLOCK_SIZE); } // Set priority to the current priority tally data_table[b_index]->priority = priot(); }
bool process () { bool error = false; // Make sure the template file exists if (! m_templateFile.existsAsFile()) { std::cout << name () << " The template file doesn't exist!\n\n"; error = true; } if (!error) { // Prepare to write output to a temporary file. std::cout << " Building: " << m_targetFile.getFullPathName() << "...\n"; TemporaryFile temp (m_targetFile); ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream (1024 * 128)); if (out == 0) { std::cout << " \n!! ERROR - couldn't write to the target file: " << temp.getFile().getFullPathName() << "\n\n"; return false; } out->setNewLineString ("\n"); if (! parseFile (m_targetFile.getParentDirectory(), m_targetFile, *out, m_templateFile, m_alreadyIncludedFiles, m_includesToIgnore, m_wildcards, 0, false)) { return false; } out = 0; if (calculateFileHashCode (m_targetFile) == calculateFileHashCode (temp.getFile())) { std::cout << " -- No need to write - new file is identical\n"; return true; } if (! temp.overwriteTargetFileWithTemporary()) { std::cout << " \n!! ERROR - couldn't write to the target file: " << m_targetFile.getFullPathName() << "\n\n"; return false; } } return error; }
/** * @brief Initializes the Disk Cache. * * When this is finished, all data is on disk, and the in-RAM cache * is empty. * * @param element_count The number of data elements. * @param cache_size_ The max number of data blocks to hold in RAM at once. */ void init(size_t element_count_, size_t cache_size_) { block_count = (element_count_ / BLOCK_SIZE) + 1; e_count = block_count * BLOCK_SIZE; cache_size = cache_size_; // Initialize vectors cache.resize(cache_size * BLOCK_SIZE); cache_info.resize(cache_size); data_table.resize(block_count); // Set all cache blocks to "not used" for (size_t i=0; i < cache_size; i++) { cache_info[i].priority = 0; cache_info[i].modified = false; cache_info[i].used = false; } // Clear out the data_table pointers memset(&(data_table[0]), 0, sizeof(T*)*block_count); // Initialize the disk cache file with the appropriate size data_file.open(); data_file.seekp((sizeof(T)*e_count)-1); data_file.put('\0'); data_file.flush(); }
//============================================================================== static bool munge (const File& templateFile, const File& targetFile, const String& wildcard, StringArray& alreadyIncludedFiles, const StringArray& includesToIgnore) { if (! templateFile.existsAsFile()) { std::cout << " The template file doesn't exist!\n\n"; return false; } StringArray wildcards; wildcards.addTokens (wildcard, ";,", "'\""); wildcards.trim(); wildcards.removeEmptyStrings(); std::cout << "Building: " << targetFile.getFullPathName() << "...\n"; TemporaryFile temp (targetFile); ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream (1024 * 128)); if (out == 0) { std::cout << "\n!! ERROR - couldn't write to the target file: " << temp.getFile().getFullPathName() << "\n\n"; return false; } out->setNewLineString ("\n"); if (! parseFile (targetFile.getParentDirectory(), targetFile, *out, templateFile, alreadyIncludedFiles, includesToIgnore, wildcards, true, false)) { return false; } out = 0; if (calculateFileHashCode (targetFile) == calculateFileHashCode (temp.getFile())) { std::cout << " -- No need to write - new file is identical\n"; return true; } if (! temp.overwriteTargetFileWithTemporary()) { std::cout << "\n!! ERROR - couldn't write to the target file: " << targetFile.getFullPathName() << "\n\n"; return false; } return true; }
void test_write_empty_workbook() { xlnt::workbook wb; TemporaryFile file; xlnt::excel_serializer serializer(wb); serializer.save_workbook(file.GetFilename()); TS_ASSERT(PathHelper::FileExists(file.GetFilename())); }
void MtpFolder::CreateFile(const std::string& name) { if (name.length() > MAX_MTP_NAME_LENGTH) throw MtpNameTooLong(); NewLIBMTPFile newFile(name, m_folderId, m_storageId); TemporaryFile empty; m_device.SendFile(newFile, empty.FileNo()); m_cache.clearItem(((LIBMTP_file_t*)newFile)->item_id); m_cache.clearItem(m_id); }
void URIStreamOpenerTest::testStreamOpenerPath() { TemporaryFile tempFile; std::string path = tempFile.path(); std::ofstream ostr(path.c_str()); assert (ostr.good()); ostr << "Hello, world!" << std::endl; ostr.close(); URIStreamOpener opener; std::istream* istr = opener.open(path); assert (istr != 0); assert (istr->good()); delete istr; }
bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, int numBytes) { if (file.getSize() == numBytes) { MemoryInputStream newStream (data, numBytes, false); if (calculateStreamHashCode (newStream) == calculateFileHashCode (file)) return true; } TemporaryFile temp (file); return temp.getFile().appendData (data, numBytes) && temp.overwriteTargetFileWithTemporary(); }
void URIStreamOpenerTest::testStreamOpenerRelative() { TemporaryFile tempFile; std::string path = tempFile.path(); std::ofstream ostr(path.c_str()); assert (ostr.good()); ostr << "Hello, world!" << std::endl; ostr.close(); URI uri(Path(path).toString(Path::PATH_UNIX)); std::string uriString = uri.toString(); URIStreamOpener opener; std::istream* istr = opener.open(uri); assert (istr != 0); assert (istr->good()); delete istr; }
unsigned long long FluteLibTest::performsFileSend( const TemporaryFile& aTemporaryFile, int aKBitRates) { FluteSender mySender(aTemporaryFile.getFilePath(), aKBitRates); unsigned long long mySessionSize = mySender.sessionSize(); mySender.startSending(); return mySessionSize; }
void URIStreamOpenerTest::testStreamOpenerPathResolve() { TemporaryFile tempFile; std::string path = tempFile.path(); std::ofstream ostr(path.c_str()); assert (ostr.good()); ostr << "Hello, world!" << std::endl; ostr.close(); Path p(path); Path parent(p.parent()); std::string base = parent.toString(); URIStreamOpener opener; std::istream* istr = opener.open(base, p.getFileName()); assert (istr != 0); assert (istr->good()); delete istr; }
void FileLogger::trimFileSize (int64 maxFileSizeBytes) const { if (maxFileSizeBytes <= 0) { logFile.deleteFile(); } else { const int64 fileSize = logFile.getSize(); if (fileSize > maxFileSizeBytes) { TemporaryFile tempFile (logFile); { FileOutputStream out (tempFile.getFile()); FileInputStream in (logFile); if (! (out.openedOk() && in.openedOk())) return; in.setPosition (fileSize - maxFileSizeBytes); for (;;) { const char c = in.readByte(); if (c == 0) return; if (c == '\n' || c == '\r') { out << c; break; } } out.writeFromInputStream (in, -1); } tempFile.overwriteTargetFileWithTemporary(); } } }
bool Utils::writeStringToFile(const String& s, const File& f) { TemporaryFile temp (f); ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream()); if (out != nullptr) { out->write(s.toUTF8(), s.getNumBytesAsUTF8()); out = nullptr; // (deletes the stream) bool succeeded = temp.overwriteTargetFileWithTemporary(); return succeeded; } else { return false; } }
TEST(sys_mman, mmap_file_write_at_offset) { TemporaryFile tf; size_t pagesize = sysconf(_SC_PAGESIZE); // Create the file with three pages worth of data. ASSERT_EQ(STR_SSIZE(PAGE0_MSG), write(tf.fd, PAGE0_MSG, sizeof(PAGE0_MSG))); ASSERT_NE(-1, lseek(tf.fd, pagesize, SEEK_SET)); ASSERT_EQ(STR_SSIZE(PAGE1_MSG), write(tf.fd, PAGE1_MSG, sizeof(PAGE1_MSG))); ASSERT_NE(-1, lseek(tf.fd, 2 * pagesize, SEEK_SET)); ASSERT_EQ(STR_SSIZE(PAGE2_MSG), write(tf.fd, PAGE2_MSG, sizeof(PAGE2_MSG))); ASSERT_NE(-1, lseek(tf.fd, 3 * pagesize - sizeof(END_MSG), SEEK_SET)); ASSERT_EQ(STR_SSIZE(END_MSG), write(tf.fd, END_MSG, sizeof(END_MSG))); ASSERT_NE(-1, lseek(tf.fd, 0, SEEK_SET)); void* map = mmap(NULL, pagesize, PROT_WRITE, MAP_SHARED, tf.fd, pagesize); ASSERT_NE(MAP_FAILED, map); close(tf.fd); memcpy(map, NEWPAGE1_MSG, sizeof(NEWPAGE1_MSG)); ASSERT_EQ(0, munmap(map, pagesize)); tf.reopen(); map = mmap(NULL, pagesize, PROT_WRITE, MAP_SHARED, tf.fd, 2 * pagesize); ASSERT_NE(MAP_FAILED, map); close(tf.fd); memcpy(map, NEWPAGE2_MSG, sizeof(NEWPAGE2_MSG)); ASSERT_EQ(0, munmap(map, pagesize)); tf.reopen(); char buf[pagesize]; ASSERT_EQ(static_cast<ssize_t>(pagesize), read(tf.fd, buf, pagesize)); ASSERT_STREQ(PAGE0_MSG, buf); ASSERT_NE(-1, lseek(tf.fd, pagesize, SEEK_SET)); ASSERT_EQ(static_cast<ssize_t>(pagesize), read(tf.fd, buf, pagesize)); ASSERT_STREQ(NEWPAGE1_MSG, buf); ASSERT_NE(-1, lseek(tf.fd, 2 * pagesize, SEEK_SET)); ASSERT_EQ(static_cast<ssize_t>(pagesize), read(tf.fd, buf, pagesize)); ASSERT_STREQ(NEWPAGE2_MSG, buf); ASSERT_STREQ(END_MSG, buf+pagesize-sizeof(END_MSG)); }
static void CleanOutGarbageRegistry() { char gbgFile[1024]; GetTempPath(1024, gbgFile); strcat(gbgFile, "GensTempFileRecords"); char key[64]; int i = 0; while(true) { sprintf(key, "File%d", i); GetPrivateProfileString("Files", key, "", Str_Tmp, 1024, gbgFile); if(!*Str_Tmp) break; TemporaryFile temp; strcpy(temp.filename, Str_Tmp); if(!temp.Delete(true)) i++; } }
TEST(LineReader, Simple) { TemporaryFile file; int fd = file.fd(); writeAll(fd, "Meow\n" "Hello world\n" "This is a long line. It is longer than the other lines.\n" "\n" "Incomplete last line"); { CHECK_ERR(lseek(fd, 0, SEEK_SET)); char buf[10]; LineReader lr(fd, buf, sizeof(buf)); expect(lr, "Meow\n"); expect(lr, "Hello worl"); expect(lr, "d\n"); expect(lr, "This is a "); expect(lr, "long line."); expect(lr, " It is lon"); expect(lr, "ger than t"); expect(lr, "he other l"); expect(lr, "ines.\n"); expect(lr, "\n"); expect(lr, "Incomplete"); expect(lr, " last line"); expect(lr, ""); } { CHECK_ERR(lseek(fd, 0, SEEK_SET)); char buf[80]; LineReader lr(fd, buf, sizeof(buf)); expect(lr, "Meow\n"); expect(lr, "Hello world\n"); expect(lr, "This is a long line. It is longer than the other lines.\n"); expect(lr, "\n"); expect(lr, "Incomplete last line"); expect(lr, ""); } }
TEST(TemporaryFile, Simple) { int fd = -1; char c = 'x'; { TemporaryFile f; EXPECT_FALSE(f.path().empty()); EXPECT_TRUE(f.path().is_absolute()); fd = f.fd(); EXPECT_LE(0, fd); ssize_t r = write(fd, &c, 1); EXPECT_EQ(1, r); } // The file must have been closed. This assumes that no other thread // has opened another file in the meanwhile, which is a sane assumption // to make in this test. ssize_t r = write(fd, &c, 1); int savedErrno = errno; EXPECT_EQ(-1, r); EXPECT_EQ(EBADF, savedErrno); }
TEST(sys_mman, mmap_file_write) { TemporaryFile tf; ASSERT_EQ(STR_SSIZE(INITIAL_MSG), write(tf.fd, INITIAL_MSG, sizeof(INITIAL_MSG))); lseek(tf.fd, 0, SEEK_SET); void* map = mmap(NULL, sizeof(STRING_MSG), PROT_WRITE, MAP_SHARED, tf.fd, 0); ASSERT_NE(MAP_FAILED, map); close(tf.fd); memcpy(map, STRING_MSG, sizeof(STRING_MSG)); ASSERT_EQ(0, munmap(map, sizeof(STRING_MSG))); tf.reopen(); char buf[sizeof(STRING_MSG)]; memset(buf, 0, sizeof(STRING_MSG)); ASSERT_EQ(STR_SSIZE(STRING_MSG), read(tf.fd, buf, sizeof(STRING_MSG))); ASSERT_STREQ(STRING_MSG, buf); }
TEST(RecordIOTest, SmallRecords) { constexpr size_t kSize = 10; char tmp[kSize]; memset(tmp, 'x', kSize); TemporaryFile file; { RecordIOWriter writer(File(file.fd())); for (size_t i = 0; i < kSize; ++i) { // record of size 0 should be ignored writer.write(IOBuf::wrapBuffer(tmp, i)); } } { RecordIOReader reader(File(file.fd())); auto it = reader.begin(); for (size_t i = 1; i < kSize; ++i) { ASSERT_FALSE(it == reader.end()); EXPECT_EQ(StringPiece(tmp, i), sp((it++)->first)); } EXPECT_TRUE(it == reader.end()); } }
TEST(RecordIOTest, MultipleFileIds) { TemporaryFile file; { RecordIOWriter writer(File(file.fd()), 1); writer.write(iobufs({"hello"})); } { RecordIOWriter writer(File(file.fd()), 2); writer.write(iobufs({"world"})); } { RecordIOWriter writer(File(file.fd()), 1); writer.write(iobufs({"goodbye"})); } { RecordIOReader reader(File(file.fd()), 0); // return all auto it = reader.begin(); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("hello", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("world", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("goodbye", sp((it++)->first)); EXPECT_TRUE(it == reader.end()); } { RecordIOReader reader(File(file.fd()), 1); auto it = reader.begin(); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("hello", sp((it++)->first)); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("goodbye", sp((it++)->first)); EXPECT_TRUE(it == reader.end()); } { RecordIOReader reader(File(file.fd()), 2); auto it = reader.begin(); ASSERT_FALSE(it == reader.end()); EXPECT_EQ("world", sp((it++)->first)); EXPECT_TRUE(it == reader.end()); } { RecordIOReader reader(File(file.fd()), 3); auto it = reader.begin(); EXPECT_TRUE(it == reader.end()); } }
/** * Unloads a block from the cache, making sure that any modifications * are written back to disk. */ void unload_cache_block(size_t cb_index) { const size_t b_index = cache_info[cb_index].index; // Clear the reference from the table if (cache_info[cb_index].used) data_table[b_index] = nullptr; // Check if the cache block is modified, and if so, write it to disk if (cache_info[cb_index].modified && cache_info[cb_index].used) { // Seek to the correct place in the file data_file.seekp(sizeof(T)*BLOCK_SIZE*b_index); // Write out the data block const T* i = &(cache[cb_index*BLOCK_SIZE]); data_file.write((char*)i, sizeof(T)*BLOCK_SIZE); // Clear modified tag cache_info[cb_index].modified = false; cache_info[cb_index].used = false; } // Set priority to zero cache_info[cb_index].priority = 0; }
void URIStreamOpenerTest::testStreamOpenerURIResolve() { TemporaryFile tempFile; std::string path = tempFile.path(); std::ofstream ostr(path.c_str()); assert (ostr.good()); ostr << "Hello, world!" << std::endl; ostr.close(); Path p(path); p.makeAbsolute(); Path parent(p.parent()); URI uri; uri.setScheme("file"); uri.setPath(parent.toString(Path::PATH_UNIX)); std::string uriString = uri.toString(); URIStreamOpener opener; std::istream* istr = opener.open(uriString, p.getFileName()); assert (istr != 0); assert (istr->good()); delete istr; }
bool runLameChildProcess (const TemporaryFile& tempMP3, const StringArray& processArgs) const { ChildProcess cp; if (cp.start (processArgs)) { const String childOutput (cp.readAllProcessOutput()); DBG (childOutput); ignoreUnused (childOutput); cp.waitForProcessToFinish (10000); return tempMP3.getFile().getSize() > 0; } return false; }
bool ResourceFile::write (const File& cppFile) { TemporaryFile tempH (cppFile.withFileExtension (".h"), TemporaryFile::useHiddenFile); TemporaryFile tempCpp (cppFile, TemporaryFile::useHiddenFile); ScopedPointer <FileOutputStream> cppOut (tempCpp.getFile().createOutputStream (32768)); ScopedPointer <FileOutputStream> hppOut (tempH.getFile().createOutputStream (32768)); if (cppOut != nullptr && hppOut != nullptr) { if (write (cppFile, *cppOut, *hppOut)) { cppOut = nullptr; hppOut = nullptr; return (tempCpp.getFile().hasIdenticalContentTo (tempCpp.getTargetFile()) || tempCpp.overwriteTargetFileWithTemporary()) && (tempH.getFile().hasIdenticalContentTo (tempH.getTargetFile()) || tempH.overwriteTargetFileWithTemporary()); } } return false; }
bool TemporaryFile::operator==(const TemporaryFile& aRHS) const { ifstream myLHS(this->getFilePath().c_str()); ifstream myRHS(aRHS.getFilePath().c_str()); while (!myLHS.eof() || !myRHS.eof()) { if (myLHS.get() != myRHS.get()) { return false; } } if (myLHS.eof() && myRHS.eof()) { return true; } return false; }
bool convertToMP3() const { TemporaryFile tempMP3 (".mp3"); StringArray args2 (args); args2.add (tempWav.getFile().getFullPathName()); args2.add (tempMP3.getFile().getFullPathName()); DBG (args2.joinIntoString (" ")); if (runLameChildProcess (tempMP3, args2)) { FileInputStream fis (tempMP3.getFile()); if (fis.openedOk() && output->writeFromInputStream (fis, -1) > 0) { output->flush(); return true; } } return false; }