SWBuf &RawFiles::getRawEntryBuf() { FileDesc *datafile; long start = 0; unsigned short size = 0; VerseKey *key = &getVerseKey(); findOffset(key->Testament(), key->TestamentIndex(), &start, &size); entryBuf = ""; if (size) { SWBuf tmpbuf = path; tmpbuf += '/'; readText(key->Testament(), start, size, entryBuf); tmpbuf += entryBuf; entryBuf = ""; datafile = FileMgr::getSystemFileMgr()->open(tmpbuf.c_str(), FileMgr::RDONLY); if (datafile->getFd() > 0) { size = datafile->seek(0, SEEK_END); char *tmpBuf = new char [ size + 1 ]; memset(tmpBuf, 0, size + 1); datafile->seek(0, SEEK_SET); datafile->read(tmpBuf, size); entryBuf = tmpBuf; delete [] tmpBuf; // preptext(entrybuf); } FileMgr::getSystemFileMgr()->close(datafile); } return entryBuf; }
int InstallMgr::refreshRemoteSource(InstallSource *is) { // assert user disclaimer has been confirmed if (!isUserDisclaimerConfirmed()) return -1; SWBuf root = (SWBuf)privatePath + (SWBuf)"/" + is->uid.c_str(); removeTrailingSlash(root); SWBuf target = root + "/mods.d"; int errorCode = -1; //0 means successful FileMgr::removeDir(target.c_str()); if (!FileMgr::existsDir(target)) FileMgr::createPathAndFile(target+"/globals.conf"); #ifndef EXCLUDEZLIB SWBuf archive = root + "/mods.d.tar.gz"; errorCode = ftpCopy(is, "mods.d.tar.gz", archive.c_str(), false); if (!errorCode) { //sucessfully downloaded the tar,gz of module configs FileDesc *fd = FileMgr::getSystemFileMgr()->open(archive.c_str(), FileMgr::RDONLY); untargz(fd->getFd(), root.c_str()); FileMgr::getSystemFileMgr()->close(fd); } else #endif errorCode = ftpCopy(is, "mods.d", target.c_str(), true, ".conf"); //copy the whole directory is->flush(); return errorCode; }
void RawFiles::setEntry(const char *inbuf, long len) { FileDesc *datafile; long start; unsigned short size; VerseKey *key = &getVerseKey(); len = (len<0)?strlen(inbuf):len; findOffset(key->Testament(), key->TestamentIndex(), &start, &size); if (size) { SWBuf tmpbuf; entryBuf = path; entryBuf += '/'; readText(key->Testament(), start, size, tmpbuf); entryBuf += tmpbuf; } else { SWBuf tmpbuf; entryBuf = path; entryBuf += '/'; tmpbuf = getNextFilename(); doSetText(key->Testament(), key->TestamentIndex(), tmpbuf); entryBuf += tmpbuf; } datafile = FileMgr::getSystemFileMgr()->open(entryBuf, FileMgr::CREAT|FileMgr::WRONLY|FileMgr::TRUNC); if (datafile->getFd() > 0) { datafile->write(inbuf, len); } FileMgr::getSystemFileMgr()->close(datafile); }
// // Quickly guess the Mach-O type of a file. // Returns type zero if the file isn't Mach-O or Universal. // Always looks at the start of the file, and does not change the file pointer. // uint32_t Universal::typeOf(FileDesc fd) { mach_header header; int max_tries = 3; if (fd.read(&header, sizeof(header), 0) != sizeof(header)) return 0; while (max_tries > 0) { switch (header.magic) { case MH_MAGIC: case MH_MAGIC_64: return header.filetype; break; case MH_CIGAM: case MH_CIGAM_64: return flip(header.filetype); break; case FAT_MAGIC: case FAT_CIGAM: { const fat_arch *arch1 = LowLevelMemoryUtilities::increment<fat_arch>(&header, sizeof(fat_header)); if (fd.read(&header, sizeof(header), ntohl(arch1->offset)) != sizeof(header)) return 0; max_tries--; continue; } default: return 0; } } return 0; }
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "usage: %s <lex path>\n\n", *argv); exit(-1); } RawStr mod(argv[1]); char buf[127]; sprintf(buf, "%s.idx", argv[1]); FileDesc *idxfd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::RDONLY, true); long maxoff = idxfd->seek(0, SEEK_END) - 6; FileMgr::getSystemFileMgr()->close(idxfd); SWBuf last = ""; bool first = true; char *trybuf = 0; for (long index = 0; index < maxoff; index+=6) { mod.getIDXBuf(index, &trybuf); if (!first) { if (strcmp(trybuf, last.c_str()) < 0) { printf("entry %ld(offset: %ld) (%s) is less than previous entry (%s)\n\n", index/6, index, trybuf, last.c_str()); exit(-3); } } else first = false; last = trybuf; } if (trybuf) delete [] trybuf; return 0; }
int main(int argc, char **argv) { greekAccentsFilter.setOptionValue("Off"); // off = accents off parseParams(argc, argv); // Let's see if we can open our input file FileDesc *fd = FileMgr::getSystemFileMgr()->open(inFile, FileMgr::RDONLY); if (fd->getFd() < 0) { fprintf(stderr, "error: %s: couldn't open input file: %s \n", argv[0], inFile.c_str()); exit(-2); } RawGenBook *book; // Do some initialization stuff if (!augMod) { RawGenBook::createModule(outPath); } book = new RawGenBook(outPath); SWBuf lineBuffer; SWBuf keyBuffer; SWBuf entBuffer; bool more = true; do { more = FileMgr::getLine(fd, lineBuffer)!=0; if (lineBuffer.startsWith("$$$")) { if ((keyBuffer.size()) && (entBuffer.size())) { writeEntry(book, keyBuffer, entBuffer); } keyBuffer = lineBuffer; keyBuffer << 3; keyBuffer.trim(); entBuffer.size(0); } else { if (keyBuffer.size()) { entBuffer += lineBuffer; entBuffer += "\n"; } } } while (more); if ((keyBuffer.size()) && (entBuffer.size())) { writeEntry(book, keyBuffer, entBuffer); } delete book; FileMgr::getSystemFileMgr()->close(fd); return 0; }
// // Fat (aka universal) file wrappers. // The offset is relative to the start of the containing file. // Universal::Universal(FileDesc fd, size_t offset /* = 0 */) : FileDesc(fd), mBase(offset) { union { fat_header header; // if this is a fat file mach_header mheader; // if this is a thin file }; const size_t size = max(sizeof(header), sizeof(mheader)); if (fd.read(&header, size, offset) != size) UnixError::throwMe(ENOEXEC); switch (header.magic) { case FAT_MAGIC: case FAT_CIGAM: { mArchCount = ntohl(header.nfat_arch); size_t archSize = sizeof(fat_arch) * mArchCount; mArchList = (fat_arch *)malloc(archSize); if (!mArchList) UnixError::throwMe(); if (fd.read(mArchList, archSize, mBase + sizeof(header)) != archSize) { ::free(mArchList); UnixError::throwMe(ENOEXEC); } for (fat_arch *arch = mArchList; arch < mArchList + mArchCount; arch++) { n2hi(arch->cputype); n2hi(arch->cpusubtype); n2hi(arch->offset); n2hi(arch->size); n2hi(arch->align); } secdebug("macho", "%p is a fat file with %d architectures", this, mArchCount); break; } case MH_MAGIC: case MH_MAGIC_64: mArchList = NULL; mArchCount = 0; mThinArch = Architecture(mheader.cputype, mheader.cpusubtype); secdebug("macho", "%p is a thin file (%s)", this, mThinArch.name()); break; case MH_CIGAM: case MH_CIGAM_64: mArchList = NULL; mArchCount = 0; mThinArch = Architecture(flip(mheader.cputype), flip(mheader.cpusubtype)); secdebug("macho", "%p is a thin file (%s)", this, mThinArch.name()); break; default: UnixError::throwMe(ENOEXEC); } }
char RawGenBook::createModule(NormalizedPath const & path) { FileDesc *fd; signed char retval; auto const buf(path.str() + ".bdt"); FileMgr::removeFile(buf.c_str()); fd = FileMgr::getSystemFileMgr()->open(buf.c_str(), FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE); fd->getFd(); FileMgr::getSystemFileMgr()->close(fd); retval = TreeKeyIdx::create(path.c_str()); return retval; }
void SWMgr::InstallScan(const char *dirname) { DIR *dir; struct dirent *ent; FileDesc *conffd = 0; SWBuf newmodfile; SWBuf targetName; if (FileMgr::existsDir(dirname)) { if ((dir = opendir(dirname))) { rewinddir(dir); while ((ent = readdir(dir))) { if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) { newmodfile = dirname; if ((dirname[strlen(dirname)-1] != '\\') && (dirname[strlen(dirname)-1] != '/')) newmodfile += "/"; newmodfile += ent->d_name; // mods.d if (configType) { if (conffd) FileMgr::getSystemFileMgr()->close(conffd); targetName = configPath; if ((configPath[strlen(configPath)-1] != '\\') && (configPath[strlen(configPath)-1] != '/')) targetName += "/"; targetName += ent->d_name; conffd = FileMgr::getSystemFileMgr()->open(targetName.c_str(), FileMgr::WRONLY|FileMgr::CREAT, FileMgr::IREAD|FileMgr::IWRITE); } // mods.conf else { if (!conffd) { conffd = FileMgr::getSystemFileMgr()->open(config->filename.c_str(), FileMgr::WRONLY|FileMgr::APPEND); if (conffd > 0) conffd->seek(0L, SEEK_END); else { FileMgr::getSystemFileMgr()->close(conffd); conffd = 0; } } } AddModToConfig(conffd, newmodfile.c_str()); FileMgr::removeFile(newmodfile.c_str()); } } if (conffd) FileMgr::getSystemFileMgr()->close(conffd); closedir(dir); } } }
// // Create a MachO object from an open file and a starting offset. // We load (only) the header and load commands into memory at that time. // Note that the offset must be relative to the start of the containing file // (not relative to some intermediate container). // MachO::MachO(FileDesc fd, size_t offset, size_t length) : FileDesc(fd), mOffset(offset), mLength(length ? length : (fd.fileSize() - offset)) { size_t size = fd.read(&mHeaderBuffer, sizeof(mHeaderBuffer), mOffset); if (size != sizeof(mHeaderBuffer)) UnixError::throwMe(ENOEXEC); this->initHeader(&mHeaderBuffer); size_t cmdSize = this->commandSize(); mCommandBuffer = (load_command *)malloc(cmdSize); if (!mCommandBuffer) UnixError::throwMe(); if (fd.read(mCommandBuffer, cmdSize, this->headerSize() + mOffset) != cmdSize) UnixError::throwMe(ENOEXEC); this->initCommands(mCommandBuffer); }
char RawFiles::createModule (const char *path) { char *incfile = new char [ strlen (path) + 16 ]; __u32 zero = 0; zero = archtosword32(zero); FileDesc *datafile; sprintf(incfile, "%s/incfile", path); datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::CREAT|FileMgr::WRONLY|FileMgr::TRUNC); delete [] incfile; datafile->write(&zero, 4); FileMgr::getSystemFileMgr()->close(datafile); return RawVerse::createModule (path); }
char SWMgr::AddModToConfig(FileDesc *conffd, const char *fname) { FileDesc *modfd; char ch; SWLog::getSystemLog()->logTimedInformation("Found new module [%s]. Installing...", fname); modfd = FileMgr::getSystemFileMgr()->open(fname, FileMgr::RDONLY); ch = '\n'; conffd->write(&ch, 1); while (modfd->read(&ch, 1) == 1) conffd->write(&ch, 1); ch = '\n'; conffd->write(&ch, 1); FileMgr::getSystemFileMgr()->close(modfd); return 0; }
//=================================================================================================== // // Function: Scribe2MEIFileDesc // Purpose: utility function to create file description for MEI header structure (only mandatory element of a header). // Used by: // //=================================================================================================== FileDesc* CScribeToNeoScribeXML::Scribe2MEIFileDesc() { /* - FileDescription - Title Statement - Edition Statement - PhysDesc - Publication/Distribution - Series Statement - Associated metadata */ //mei_head - <fileDesc> FileDesc* fileDesc = new FileDesc; //fileDesc - <pubStmt> PubStmt* pubStmt = new PubStmt; fileDesc->addChild(pubStmt); RespStmt* pubRespStmt = new RespStmt; pubRespStmt->setValue("http://www.lib.latrobe.edu.au/MMDB/"); pubStmt->addChild(pubRespStmt); Publisher* publisher = new Publisher; pubStmt->addChild(publisher); CorpName* corpName = new CorpName; corpName->setValue("Scribe Software"); publisher->addChild(corpName); //address here; Date* date = new Date; date->setValue("1984-2014"); pubStmt->addChild(date); Availability* avail = new Availability; UseRestrict* CRS = new UseRestrict; CRS->setValue("©1984–2014, Scribe Software"); avail->addChild(CRS); pubStmt->addChild(avail); //fileDesc - <seriesStmt> SeriesStmt* seriesStmt = new SeriesStmt(); fileDesc->addChild(seriesStmt); return fileDesc; }
char FTPLibFTPTransport::getURL(const char *destPath, const char *sourceURL, SWBuf *destBuf) { char retVal = 0; // assert we can login retVal = assureLoggedIn(); if (retVal) return retVal; SWBuf sourcePath = sourceURL; SWBuf outFile = (!destBuf) ? destPath : "swftplib.tmp"; sourcePath << (6 + host.length()); // shift << "ftp://hostname"; SWLog::getSystemLog()->logDebug("getting file %s to %s\n", sourcePath.c_str(), outFile.c_str()); if (passive) FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, ftpConnection); else FtpOptions(FTPLIB_CONNMODE, FTPLIB_PORT, ftpConnection); // !!!WDG also want to set callback options if (sourcePath.endsWith("/") || sourcePath.endsWith("\\")) { SWLog::getSystemLog()->logDebug("getting test directory %s\n", sourcePath.c_str()); FtpDir(NULL, sourcePath, ftpConnection); SWLog::getSystemLog()->logDebug("getting real directory %s\n", sourcePath.c_str()); retVal = FtpDir(outFile.c_str(), sourcePath, ftpConnection) - 1; } else { SWLog::getSystemLog()->logDebug("getting file %s\n", sourcePath.c_str()); retVal = FtpGet(outFile.c_str(), sourcePath, FTPLIB_IMAGE, ftpConnection) - 1; } // Is there a way to FTPGet directly to a buffer? // If not, we probably want to add x-platform way to open a tmp file with FileMgr // This wreaks and will easily fail if a user's CWD is not writable. if (destBuf) { FileDesc *fd = FileMgr::getSystemFileMgr()->open("swftplib.tmp", FileMgr::RDONLY); long size = fd->seek(0, SEEK_END); fd->seek(0, SEEK_SET); destBuf->size(size); fd->read(destBuf->getRawData(), size); FileMgr::getSystemFileMgr()->close(fd); FileMgr::removeFile("swftplib.tmp"); } return retVal; }
char RawGenBook::createModule(const char *ipath) { char *path = 0; char *buf = new char [ strlen (ipath) + 20 ]; FileDesc *fd; signed char retval; stdstr(&path, ipath); if ((path[strlen(path)-1] == '/') || (path[strlen(path)-1] == '\\')) path[strlen(path)-1] = 0; sprintf(buf, "%s.bdt", path); FileMgr::removeFile(buf); fd = FileMgr::getSystemFileMgr()->open(buf, FileMgr::CREAT|FileMgr::WRONLY, FileMgr::IREAD|FileMgr::IWRITE); fd->getFd(); FileMgr::getSystemFileMgr()->close(fd); retval = TreeKeyIdx::create(path); delete [] path; return retval; }
UDFDisc::VirtualDataBlock UDFDisc::FindFileInDirectory(LogicalVolume *pVolume, const FileDesc &directory, const wchar_t *pwszFileName, bool caseSensitive /*= false*/) { FragmentedFile *pDirectoryData = CreateFileFromICB(directory.GetICB()); if (!pDirectoryData) return UDFDisc::VirtualDataBlock(); VirtualDataBlock dirData = pDirectoryData->EntireFile(); BazisLib::TypedBuffer<FileIdentifierDescriptor> pDesc; SmallCacheBuffer cacheBuf; for (ULONGLONG i = 0; i < directory.GetICB().Size();) { FileIdentifierDescriptor desc; if (ReadStructureCached(dirData, cacheBuf, i, &desc, sizeof(desc)) != sizeof(desc)) break; size_t totalSize = sizeof(FileIdentifierDescriptor) + desc.LengthofFileIdentifier + desc.LengthOfImplementationUse; totalSize = (totalSize + 3) & ~3; //Align to DWORD boundary pDesc.EnsureSizeAndSetUsed(totalSize); if (ReadStructureCached(dirData, cacheBuf, i, pDesc, totalSize) != totalSize) break; if (!ValidateUDFDescriptor(kInvalidSector, &pDesc->DescriptorTag, totalSize)) break; std::wstring fileName = UDFHelpers::UncompressUnicode(pDesc->LengthofFileIdentifier, ((unsigned char *)pDesc.GetDataAfterStructure()) + pDesc->LengthOfImplementationUse); bool match; if (caseSensitive) match = !wcscmp(fileName.c_str(), pwszFileName); else match = !_wcsicmp(fileName.c_str(), pwszFileName); if (match) return pVolume->LongADToDataBlock(pDesc->ICB); i += totalSize; } delete pDirectoryData; return UDFDisc::VirtualDataBlock(); }
char *RawFiles::getNextFilename() { static char incfile[255]; __u32 number; FileDesc *datafile; sprintf(incfile, "%s/incfile", path); datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::RDONLY); if (datafile->read(&number, 4) != 4) number = 0; number = swordtoarch32(number); number++; FileMgr::getSystemFileMgr()->close(datafile); datafile = FileMgr::getSystemFileMgr()->open(incfile, FileMgr::CREAT|FileMgr::WRONLY|FileMgr::TRUNC); sprintf(incfile, "%.7d", number-1); number = archtosword32(number); datafile->write(&number, 4); FileMgr::getSystemFileMgr()->close(datafile); return incfile; }