void GoogleTranslator::saveCache() { QFile file(getCacheFileName()); if (file.open(QIODevice::Append | QIODevice::Text)) { QTextStream out(&file); foreach(QString key, newCacheItems_.keys()) out << key << "\t" << newCacheItems_[key] << "\n"; file.close(); newCacheItems_.clear(); } }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimFileWellPath::updateFilePathsFromProjectPath(const QString& newProjectPath, const QString& oldProjectPath) { RimWellPath::updateFilePathsFromProjectPath(newProjectPath, oldProjectPath); if (isStoredInCache()) { QString newCacheFileName = getCacheFileName(); if (caf::Utils::fileExists(newCacheFileName)) { m_filepath = newCacheFileName; } } else { m_filepath = RimTools::relocateFile(m_filepath(), newProjectPath, oldProjectPath, nullptr, nullptr); } }
void GoogleTranslator::loadCache() { languageCache_.clear(); QFile file(getCacheFileName()); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream out(&file); QStringList cachedItems = out.readAll().split("\n", QString::SkipEmptyParts); file.close(); for(int i=0; i<cachedItems.length(); i++) { QStringList itemPair = cachedItems[i].split("\t"); if(itemPair.count()==2) languageCache_.insert(itemPair[0].toLower(),itemPair[1]); } } }
/* * Write the dependency info to "fd" at the current file position. */ static int writeDependencies(int fd, u4 modWhen, u4 crc) { u1* buf = NULL; int result = -1; ssize_t bufLen; ClassPathEntry* cpe; int numDeps; /* * Count up the number of completed entries in the bootclasspath. */ numDeps = 0; bufLen = 0; for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { const char* cacheFileName = dvmPathToAbsolutePortion(getCacheFileName(cpe)); assert(cacheFileName != NULL); /* guaranteed by Class.c */ LOGV("+++ DexOpt: found dep '%s'\n", cacheFileName); numDeps++; bufLen += strlen(cacheFileName) +1; } bufLen += 4*4 + numDeps * (4+kSHA1DigestLen); buf = malloc(bufLen); set4LE(buf+0, modWhen); set4LE(buf+4, crc); set4LE(buf+8, DALVIK_VM_BUILD); set4LE(buf+12, numDeps); // TODO: do we want to add dvmGetInlineOpsTableLength() here? Won't // help us if somebody replaces an existing entry, but it'd catch // additions/removals. u1* ptr = buf + 4*4; for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { const char* cacheFileName = dvmPathToAbsolutePortion(getCacheFileName(cpe)); assert(cacheFileName != NULL); /* guaranteed by Class.c */ const u1* signature = getSignature(cpe); int len = strlen(cacheFileName) +1; if (ptr + 4 + len + kSHA1DigestLen > buf + bufLen) { LOGE("DexOpt: overran buffer\n"); dvmAbort(); } set4LE(ptr, len); ptr += 4; memcpy(ptr, cacheFileName, len); ptr += len; memcpy(ptr, signature, kSHA1DigestLen); ptr += kSHA1DigestLen; } assert(ptr == buf + bufLen); result = sysWriteFully(fd, buf, bufLen, "DexOpt dep info"); free(buf); return result; }
/* * Read the "opt" header, verify it, then read the dependencies section * and verify that data as well. * * If "sourceAvail" is "true", this will verify that "modWhen" and "crc" * match up with what is stored in the header. If they don't, we reject * the file so that it can be recreated from the updated original. If * "sourceAvail" isn't set, e.g. for a .odex file, we ignore these arguments. * * On successful return, the file will be seeked immediately past the * "opt" header. */ bool dvmCheckOptHeaderAndDependencies(int fd, bool sourceAvail, u4 modWhen, u4 crc, bool expectVerify, bool expectOpt) { DexOptHeader optHdr; u1* depData = NULL; const u1* magic; off_t posn; int result = false; ssize_t actual; /* * Start at the start. The "opt" header, when present, will always be * the first thing in the file. */ if (lseek(fd, 0, SEEK_SET) != 0) { LOGE("DexOpt: failed to seek to start of file: %s\n", strerror(errno)); goto bail; } /* * Read and do trivial verification on the opt header. The header is * always in host byte order. */ actual = read(fd, &optHdr, sizeof(optHdr)); if (actual < 0) { LOGE("DexOpt: failed reading opt header: %s\n", strerror(errno)); goto bail; } else if (actual != sizeof(optHdr)) { LOGE("DexOpt: failed reading opt header (got %d of %zd)\n", (int) actual, sizeof(optHdr)); goto bail; } magic = optHdr.magic; if (memcmp(magic, DEX_MAGIC, 4) == 0) { /* somebody probably pointed us at the wrong file */ LOGD("DexOpt: expected optimized DEX, found unoptimized\n"); goto bail; } else if (memcmp(magic, DEX_OPT_MAGIC, 4) != 0) { /* not a DEX file, or previous attempt was interrupted */ LOGD("DexOpt: incorrect opt magic number (0x%02x %02x %02x %02x)\n", magic[0], magic[1], magic[2], magic[3]); goto bail; } if (memcmp(magic+4, DEX_OPT_MAGIC_VERS, 4) != 0) { LOGW("DexOpt: stale opt version (0x%02x %02x %02x %02x)\n", magic[4], magic[5], magic[6], magic[7]); goto bail; } if (optHdr.depsLength < kMinDepSize || optHdr.depsLength > kMaxDepSize) { LOGW("DexOpt: weird deps length %d, bailing\n", optHdr.depsLength); goto bail; } /* * Do the header flags match up with what we want? * * This is useful because it allows us to automatically regenerate * a file when settings change (e.g. verification is now mandatory), * but can cause difficulties if the bootstrap classes we depend upon * were handled differently than the current options specify. We get * upset because they're not verified or optimized, but we're not able * to regenerate them because the installer won't let us. * * (This is also of limited value when !sourceAvail.) * * So, for now, we essentially ignore "expectVerify" and "expectOpt" * by limiting the match mask. * * The only thing we really can't handle is incorrect byte-ordering. */ const u4 matchMask = DEX_OPT_FLAG_BIG; u4 expectedFlags = 0; #if __BYTE_ORDER != __LITTLE_ENDIAN expectedFlags |= DEX_OPT_FLAG_BIG; #endif if (expectVerify) expectedFlags |= DEX_FLAG_VERIFIED; if (expectOpt) expectedFlags |= DEX_OPT_FLAG_FIELDS | DEX_OPT_FLAG_INVOCATIONS; if ((expectedFlags & matchMask) != (optHdr.flags & matchMask)) { LOGI("DexOpt: header flag mismatch (0x%02x vs 0x%02x, mask=0x%02x)\n", expectedFlags, optHdr.flags, matchMask); goto bail; } posn = lseek(fd, optHdr.depsOffset, SEEK_SET); if (posn < 0) { LOGW("DexOpt: seek to deps failed: %s\n", strerror(errno)); goto bail; } /* * Read all of the dependency stuff into memory. */ depData = (u1*) malloc(optHdr.depsLength); if (depData == NULL) { LOGW("DexOpt: unable to allocate %d bytes for deps\n", optHdr.depsLength); goto bail; } actual = read(fd, depData, optHdr.depsLength); if (actual < 0) { LOGW("DexOpt: failed reading deps: %s\n", strerror(errno)); goto bail; } else if (actual != (ssize_t) optHdr.depsLength) { LOGW("DexOpt: failed reading deps: got %d of %d\n", (int) actual, optHdr.depsLength); goto bail; } /* * Verify simple items. */ const u1* ptr; u4 val; ptr = depData; val = read4LE(&ptr); if (sourceAvail && val != modWhen) { LOGI("DexOpt: source file mod time mismatch (%08x vs %08x)\n", val, modWhen); goto bail; } val = read4LE(&ptr); if (sourceAvail && val != crc) { LOGI("DexOpt: source file CRC mismatch (%08x vs %08x)\n", val, crc); goto bail; } val = read4LE(&ptr); if (val != DALVIK_VM_BUILD) { LOGD("DexOpt: VM build version mismatch (%d vs %d)\n", val, DALVIK_VM_BUILD); goto bail; } /* * Verify dependencies on other cached DEX files. It must match * exactly with what is currently defined in the bootclasspath. */ ClassPathEntry* cpe; u4 numDeps; numDeps = read4LE(&ptr); LOGV("+++ DexOpt: numDeps = %d\n", numDeps); for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { const char* cacheFileName = dvmPathToAbsolutePortion(getCacheFileName(cpe)); assert(cacheFileName != NULL); /* guaranteed by Class.c */ const u1* signature = getSignature(cpe); size_t len = strlen(cacheFileName) +1; u4 storedStrLen; if (numDeps == 0) { /* more entries in bootclasspath than in deps list */ LOGI("DexOpt: not all deps represented\n"); goto bail; } storedStrLen = read4LE(&ptr); if (len != storedStrLen || strcmp(cacheFileName, (const char*) ptr) != 0) { LOGI("DexOpt: mismatch dep name: '%s' vs. '%s'\n", cacheFileName, ptr); goto bail; } ptr += storedStrLen; if (memcmp(signature, ptr, kSHA1DigestLen) != 0) { LOGI("DexOpt: mismatch dep signature for '%s'\n", cacheFileName); goto bail; } ptr += kSHA1DigestLen; LOGV("DexOpt: dep match on '%s'\n", cacheFileName); numDeps--; } if (numDeps != 0) { /* more entries in deps list than in classpath */ LOGI("DexOpt: Some deps went away\n"); goto bail; } // consumed all data and no more? if (ptr != depData + optHdr.depsLength) { LOGW("DexOpt: Spurious dep data? %d vs %d\n", (int) (ptr - depData), optHdr.depsLength); assert(false); } result = true; bail: free(depData); return result; }