Exemplo n.º 1
0
bool CExtUpdate::deleteFileList(const std::string & fileList)
{
	Wildcard = "";
	struct dirent **namelist;
	std::string fList = fileList;
	static struct stat FileInfo;

	size_t pos = fileList.find_last_of("/");
	fList = fileList.substr(0, pos);
	Wildcard = fileList.substr(pos+1);

	int n = scandir(fList.c_str(), &namelist, fileSelect, 0);
	if (n > 0) {
		while (n--) {
			std::string dName = namelist[n]->d_name;
			if (lstat((fList+"/"+dName).c_str(), &FileInfo) != -1) {
				if (S_ISDIR(FileInfo.st_mode)) {
					// Directory
					WRITE_UPDATE_LOG("delete directory: %s\n", (fList+"/"+dName).c_str());
					FileHelpers->removeDir((fList+"/"+dName).c_str());
				}
				else if (S_ISREG(FileInfo.st_mode)) {
					// File
					WRITE_UPDATE_LOG("delete file: %s\n", (fList+"/"+dName).c_str());
					unlink((fList+"/"+dName).c_str());
				}
			}
			free(namelist[n]);
		}
		free(namelist);
	}
	return true;
}
Exemplo n.º 2
0
bool CExtUpdate::applySettings(std::string & filename, int mode)
{
	if(!FileHelpers)
		FileHelpers = new CFileHelpers();

	if (mode == MODE_EXPERT)
		imgFilename = (std::string)g_settings.update_dir + "/" + FILESYSTEM_ENCODING_TO_UTF8_STRING(filename);
	else
		imgFilename = FILESYSTEM_ENCODING_TO_UTF8_STRING(filename);

	DBG_TIMER_START()

	std::string oldFilename = imgFilename;
	std::string hostName    = netGetHostname();
	std::string orgPath     = getPathName(imgFilename);
	std::string orgName     = getBaseName(imgFilename);
	orgName                 = getFileName(orgName);
	std::string orgExt      = "." + getFileExt(imgFilename);
	std::string timeStr     = getNowTimeStr("_%Y%m%d_%H%M");
	std::string settingsStr = "+settings";

	if (orgPath != "/tmp") {
		if (g_settings.softupdate_name_mode_apply == CExtUpdate::SOFTUPDATE_NAME_HOSTNAME_TIME)
			imgFilename = orgPath + "/" + hostName + timeStr + settingsStr + orgExt;
		else if (g_settings.softupdate_name_mode_apply == CExtUpdate::SOFTUPDATE_NAME_ORGNAME_TIME)
			imgFilename = orgPath + "/" + orgName + timeStr  + settingsStr + orgExt;
		else
			imgFilename = orgPath + "/" + orgName  + settingsStr + orgExt;
		FileHelpers->copyFile(oldFilename.c_str(), imgFilename.c_str(), 0644);
	}
	else
		imgFilename = oldFilename;
	filename = imgFilename;

	bool ret = applySettings();
	DBG_TIMER_STOP("Image editing")
	if (!ret) {
		if ((mtdRamError != "") && (!flashErrorFlag))
			DisplayErrorMessage(mtdRamError.c_str());

		// error, delete image file
		unlink(imgFilename.c_str());
	}
	else {
		if (mode == MODE_EXPERT) {
			if ((mtdNumber < 3) || (mtdNumber > 4)) {
				const char *err = "invalid mtdNumber\n";
				printf(err);
				DisplayErrorMessage(err);
				WRITE_UPDATE_LOG("ERROR: %s", err);
				return false;
			}
		}
		WRITE_UPDATE_LOG("\n");
		WRITE_UPDATE_LOG("##### Settings taken. #####\n");
		if (mode == MODE_EXPERT)
			CFlashExpert::getInstance()->writemtd(filename, mtdNumber);
	}
	return ret;
}
Exemplo n.º 3
0
bool CExtUpdate::ErrorReset(bool modus, const std::string & msg1, const std::string & msg2)
{
	char buf[1024];

	if (modus & RESET_UNLOAD) {
		umount(mountPkt.c_str());
//		my_system(2,"rmmod", mtdramDriver.c_str());
	}
	if (modus & CLOSE_FD1)
		close(fd1);
	if (modus & CLOSE_FD2)
		close(fd2);
	if (modus & CLOSE_F1)
		fclose(f1);
	if (modus & DELETE_MTDBUF) {
		if (MTDBuf != NULL)
			delete[] MTDBuf;
		MTDBuf = NULL;
	}

	if (msg2.empty())
		snprintf(buf, sizeof(buf), "%s\n", msg1.c_str());
	else 
		snprintf(buf, sizeof(buf), "%s %s\n", msg1.c_str(), msg2.c_str());

	if ((!msg1.empty()) || (!msg2.empty())) {
		mtdRamError = buf;
		WRITE_UPDATE_LOG("ERROR: %s", buf);
		printf(mtdRamError.c_str());
	}
	if(hintBox)
		hintBox->hide();
	sync();
	return false;
}
Exemplo n.º 4
0
bool CExtUpdate::copyFileList(const std::string & fileList, const std::string & dstPath)
{
	Wildcard = "";
	struct dirent **namelist;
	std::string fList = fileList, dst;
	static struct stat FileInfo;

	size_t pos = fileList.find_last_of("/");
	fList = fileList.substr(0, pos);
	Wildcard = fileList.substr(pos+1);

	int n = scandir(fList.c_str(), &namelist, fileSelect, 0);
	if (n > 0) {
		dst = dstPath + fList;
		FileHelpers->createDir(dst.c_str(), 0755);
		while (n--) {
			std::string dName = namelist[n]->d_name;
			if (lstat((fList+"/"+dName).c_str(), &FileInfo) != -1) {
				if (S_ISLNK(FileInfo.st_mode)) {
					char buf[PATH_MAX];
					int len = readlink((fList+"/"+dName).c_str(), buf, sizeof(buf)-1);
					if (len != -1) {
						buf[len] = '\0';
						WRITE_UPDATE_LOG("symlink: %s => %s\n", (dst+"/"+dName).c_str(), buf);
						symlink(buf, (dst+"/"+dName).c_str());
					}
				}
				else
					if (S_ISREG(FileInfo.st_mode)) {
						WRITE_UPDATE_LOG("copy %s => %s\n", (fList+"/"+dName).c_str(), (dst+"/"+dName).c_str());
						std::string save = (isBlacklistEntry(fList+"/"+dName)) ? ".save" : "";
						if (!FileHelpers->copyFile((fList+"/"+dName).c_str(), (dst + "/" + dName + save).c_str(), FileInfo.st_mode & 0x0FFF))
							return ErrorReset(0, "copyFile error");
					}
			}
			free(namelist[n]);
		}
		free(namelist);
	}

	return true;
}
Exemplo n.º 5
0
bool CExtUpdate::isBlacklistEntry(const std::string & file)
{
	for(vector<std::string>::iterator it = blackList.begin(); it != blackList.end(); ++it) {
		if (*it == file) {
			DBG_MSG("BlacklistEntry %s\n", (*it).c_str());
			WRITE_UPDATE_LOG("BlacklistEntry: %s\n", (*it).c_str());
			return true;
		}
	}
	return false;
}
Exemplo n.º 6
0
bool CExtUpdate::checkSpecialFolders(std::string line, bool copy)
{
	if ((line == "/") || (line == "/*") || (line == "/*.*") || (line.find("/dev") == 0) || (line.find("/proc") == 0) || 
			(line.find("/sys") == 0) || (line.find("/mnt") == 0) || (line.find("/tmp") == 0)) {
		char buf[PATH_MAX];
		neutrino_locale_t msg = (copy) ? LOCALE_FLASHUPDATE_UPDATE_WITH_SETTINGS_SKIPPED : LOCALE_FLASHUPDATE_UPDATE_WITH_SETTINGS_DEL_SKIPPED;
		snprintf(buf, sizeof(buf), g_Locale->getText(msg), line.c_str());
		WRITE_UPDATE_LOG("%s%s", buf, "\n");
		ShowMsg(LOCALE_MESSAGEBOX_INFO, buf, CMessageBox::mbrOk, CMessageBox::mbOk, NEUTRINO_ICON_INFO);
		return true;
	}
	return false;
}
Exemplo n.º 7
0
bool CExtUpdate::readBackupList(const std::string & dstPath)
{
	char buf[PATH_MAX];
	static struct stat FileInfo;
	vector<std::string>::iterator it;
	
	f1 = fopen(backupList.c_str(), "r");
	if (f1 == NULL) {
		f1 = fopen(backupList.c_str(), "w");
		if (f1 != NULL) {
			char tmp1[1024];
			snprintf(tmp1, sizeof(tmp1), "Log=%d\nLogFile=%s\n\n%s\n\n", fLogEnabled, fLogfile.c_str(), defaultBackup.c_str());
			fwrite(tmp1, 1, strlen(tmp1), f1);
			fclose(f1);
		}
		else
			return ErrorReset(0, "cannot create missing backuplist file: " + backupList);
	}

	f1 = fopen(backupList.c_str(), "r");
	if (f1 == NULL)
		return ErrorReset(0, "cannot read backuplist file: " + backupList);
	fpos_t fz;
	fseek(f1, 0, SEEK_END);
	fgetpos(f1, &fz);
	fseek(f1, 0, SEEK_SET);
	if (fz.__pos == 0)
		return ErrorReset(CLOSE_F1, "backuplist filesize is 0");
	size_t pos;
	std::string line;

	// read blacklist and config vars
	copyList.clear();
	blackList.clear();
	deleteList.clear();
	while(fgets(buf, sizeof(buf), f1) != NULL) {
		std::string tmpLine;
		line = buf;
		line = trim(line);
		// ignore comments
		if (line.find_first_of("#") == 0) {
			// config vars
			if (line.find_first_of(":") == 1) {
				if (line.length() > 1)
					readConfig(line);
			}
			continue;
		}
		pos = line.find_first_of("#");
		if (pos != std::string::npos) {
			line = line.substr(0, pos);
			line = trim(line);
		}
		// find blackList entry
		if (line.find_first_of("-") == 0) {
			tmpLine = line.substr(1);
			if ((tmpLine.length() > 1) && (lstat(tmpLine.c_str(), &FileInfo) != -1)) {
				if (S_ISREG(FileInfo.st_mode))
					blackList.push_back(tmpLine);
			}
		}
		// find deleteList entry
		else if (line.find_first_of("~") == 0) {
			tmpLine = line.substr(1);
			if (checkSpecialFolders(tmpLine, false))
				continue;
			tmpLine = dstPath + tmpLine;
			if (line.length() > 2)
				deleteList.push_back(tmpLine);
		}
		// find copyList entry
		else {
			tmpLine = (line.find_first_of("+") == 0) ? line.substr(1) : line; // '+' add entry = default
			if (checkSpecialFolders(tmpLine, true))
				continue;
			if (tmpLine.length() > 1)
				copyList.push_back(tmpLine);
		}
	}
	fclose(f1);

	// read DeleteList
	for(it = deleteList.begin(); it != deleteList.end(); ++it) {
		line = *it;
		if ((line.find("*") != std::string::npos) || (line.find("?") != std::string::npos)) {
			// Wildcards
			WRITE_UPDATE_LOG("delete file list: %s\n", line.c_str());
			deleteFileList(line.c_str());
		}
		else if (lstat(line.c_str(), &FileInfo) != -1) {
			if (S_ISREG(FileInfo.st_mode)) {
				// File
				WRITE_UPDATE_LOG("delete file: %s\n", line.c_str());
				unlink(line.c_str());
			}
			else if (S_ISDIR(FileInfo.st_mode)){
				// Directory
				WRITE_UPDATE_LOG("delete directory: %s\n", line.c_str());
				FileHelpers->removeDir(line.c_str());
			}
		}
	}
	sync();

	if (get_fs_usage(mountPkt.c_str(), total, used, &bsize))
		free2 = (total * bsize) / 1024 - (used * bsize) / 1024;

	// read copyList
	for(it = copyList.begin(); it != copyList.end(); ++it) {
		line = *it;
		line = trim(line);
		// remove '/' from line end
		size_t len = line.length();
		pos = line.find_last_of("/");
		if (len == pos+1)
			line = line.substr(0, pos);
		std::string dst = dstPath + line;
		if ((line.find("*") != std::string::npos) || (line.find("?") != std::string::npos)) {
			// Wildcards
			DBG_MSG("Wildcards: %s\n", dst.c_str());
			WRITE_UPDATE_LOG("\n");
			WRITE_UPDATE_LOG("--------------------\n");
			WRITE_UPDATE_LOG("Wildcards: %s\n", dst.c_str());
			copyFileList(line, dstPath);
		}
		else {
			if (lstat(line.c_str(), &FileInfo) != -1) {
				if (S_ISREG(FileInfo.st_mode)) {
					// one file only
					pos = dst.find_last_of("/");
					std::string dir = dst.substr(0, pos);
					FileHelpers->createDir(dir.c_str(), 0755);
					DBG_MSG("file: %s => %s\n", line.c_str(), dst.c_str());
					WRITE_UPDATE_LOG("\n");
					WRITE_UPDATE_LOG("file: %s => %s\n", line.c_str(), dst.c_str());
					WRITE_UPDATE_LOG("--------------------\n");
					std::string save = (isBlacklistEntry(line)) ? ".save" : "";
					if (!FileHelpers->copyFile(line.c_str(), (dst + save).c_str(), FileInfo.st_mode & 0x0FFF))
						return ErrorReset(0, "copyFile error");
				}
				else if (S_ISDIR(FileInfo.st_mode)) {
					// directory
					DBG_MSG("directory: %s => %s\n", line.c_str(), dst.c_str());
					WRITE_UPDATE_LOG("\n");
					WRITE_UPDATE_LOG("directory: %s => %s\n", line.c_str(), dst.c_str());
					WRITE_UPDATE_LOG("--------------------\n");
					FileHelpers->copyDir(line.c_str(), dst.c_str(), true);
				}
			}
		
		}
	}
	sync();

	if (get_fs_usage(mountPkt.c_str(), total, used, &bsize)) {
		uint64_t flashWarning = 1000; // 1MB
		uint64_t flashError   = 600;  // 600KB
		char buf1[1024];
		total = (total * bsize) / 1024;
		free3 = total - (used * bsize) / 1024;
		printf("##### [%s] %llu KB free org, %llu KB free after delete, %llu KB free now\n", __FUNCTION__, free1, free2, free3);
		memset(buf1, '\0', sizeof(buf1));
		if (free3 <= flashError) {
			snprintf(buf1, sizeof(buf1)-1, g_Locale->getText(LOCALE_FLASHUPDATE_UPDATE_WITH_SETTINGS_ERROR), free3, total);
			ShowMsg(LOCALE_MESSAGEBOX_ERROR, buf1, CMessageBox::mbrOk, CMessageBox::mbOk, NEUTRINO_ICON_ERROR);
			flashErrorFlag = true;
			return false;
		}
		else if (free3 <= flashWarning) {
			snprintf(buf1, sizeof(buf1)-1, g_Locale->getText(LOCALE_FLASHUPDATE_UPDATE_WITH_SETTINGS_WARNING), free3, total);
		    	if (ShowMsg(LOCALE_MESSAGEBOX_INFO, buf1, CMessageBox::mbrNo, CMessageBox::mbYes | CMessageBox::mbNo, NEUTRINO_ICON_INFO) != CMessageBox::mbrYes) {
				flashErrorFlag = true;
				return false;
		    	}
		}
	}
	return true;
}