Image backup apollo Part #5

- Rework handling devtable
- Use file for devtable '/var/tuxbox/config/devtable.txt'
- If file is empty or does not exist, will default entries
  '/dev/console' and '/dev/null' creates
This commit is contained in:
Michael Liebmann
2013-09-24 07:51:10 +02:00
parent 23163e7897
commit 9188c78751
4 changed files with 90 additions and 90 deletions

View File

@@ -53,9 +53,6 @@
#include <system/flashtool.h> #include <system/flashtool.h>
#include <system/httptool.h> #include <system/httptool.h>
#ifdef BOXMODEL_APOLLO
#include <system/mtdutils/mkfs.jffs2.h>
#endif
#include <system/helpers.h> #include <system/helpers.h>
#include <lib/libnet/libnet.h> #include <lib/libnet/libnet.h>
@@ -602,9 +599,37 @@ bool CFlashExpert::checkSize(int mtd, std::string &backupFile)
} }
#ifdef BOXMODEL_APOLLO #ifdef BOXMODEL_APOLLO
void CFlashExpert::addDevtableEntry(int fd_dev, const char *entry) bool CFlashExpert::readDevtableFile(std::string &devtableFile, CMkfsJFFS2::v_devtable_t &v_devtable)
{ {
write(fd_dev, entry, strlen(entry)); FILE *fd = fopen(devtableFile.c_str(), "r");
if (!fd) return false;
char lineRead[1024];
memset(lineRead, 0, sizeof(lineRead));
bool status = false;
while (fgets(lineRead, sizeof(lineRead)-1, fd)) {
std::string line = lineRead;
line = trim(line);
// ignore comments
if (line.find_first_of("#") == 0) {
continue;
}
// ignore comments after the entry
size_t pos = line.find_first_of("#");
if (pos != std::string::npos) {
line = line.substr(0, pos);
line = trim(line);
}
// minimal entry: "/dev/x x 0000"
// length = 13
if (line.length() > 12) {
v_devtable.push_back(line);
status = true;
}
memset(lineRead, 0, sizeof(lineRead));
}
fclose(fd);
if (!status) return false;
return true;
} }
void CFlashExpert::readmtdJFFS2(std::string &filename) void CFlashExpert::readmtdJFFS2(std::string &filename)
@@ -615,21 +640,24 @@ void CFlashExpert::readmtdJFFS2(std::string &filename)
progress.setTitle(LOCALE_FLASHUPDATE_TITLEREADFLASH); progress.setTitle(LOCALE_FLASHUPDATE_TITLEREADFLASH);
progress.paint(); progress.paint();
std::string devTable = "/tmp/devtable.txt"; bool devtableFileIO = false;
int fd_dev = open(devTable.c_str(), O_WRONLY|O_CREAT|O_TRUNC); CMkfsJFFS2::v_devtable_t v_devtable;
if (fd_dev != -1) { std::string devtableFile = (std::string)CONFIGDIR + "/devtable.txt";
addDevtableEntry(fd_dev, "/dev/console c 0600 0 0 5 1 0 0 0\n"); if (file_exists(devtableFile.c_str())) {
addDevtableEntry(fd_dev, "/dev/null c 0666 0 0 1 3 0 0 0\n"); if (readDevtableFile(devtableFile, v_devtable))
close(fd_dev); devtableFileIO = true;
} else }
devTable = ""; if ((!devtableFileIO) || (v_devtable.empty())) {
v_devtable.push_back("/dev/console c 0600 0 0 5 1 0 0 0");
v_devtable.push_back("/dev/null c 0666 0 0 1 3 0 0 0");
}
std::string path = "/"; std::string path = "/";
CMTDInfo *MTDInfo = CMTDInfo::getInstance(); CMTDInfo *MTDInfo = CMTDInfo::getInstance();
int esize = MTDInfo->getMTDEraseSize(MTDInfo->findMTDsystem()); int esize = MTDInfo->getMTDEraseSize(MTDInfo->findMTDsystem());
CMkfsJFFS2 mkfs; CMkfsJFFS2 mkfs;
mkfs.makeJffs2Image(path, filename, esize, 0, 0, __LITTLE_ENDIAN, true, true, &progress, devTable); mkfs.makeJffs2Image(path, filename, esize, 0, 0, __LITTLE_ENDIAN, true, true, &progress, &v_devtable);
progress.hide(); progress.hide();
unlink(devTable.c_str());
char message[500]; char message[500];
sprintf(message, g_Locale->getText(LOCALE_FLASHUPDATE_SAVESUCCESS), filename.c_str()); sprintf(message, g_Locale->getText(LOCALE_FLASHUPDATE_SAVESUCCESS), filename.c_str());

View File

@@ -39,6 +39,9 @@
#include <gui/widget/progresswindow.h> #include <gui/widget/progresswindow.h>
#include <driver/framebuffer.h> #include <driver/framebuffer.h>
#ifdef BOXMODEL_APOLLO
#include <system/mtdutils/mkfs.jffs2.h>
#endif
#include <string> #include <string>
@@ -78,7 +81,7 @@ class CFlashExpert : public CProgressWindow
bool checkSize(int mtd, std::string &backupFile); bool checkSize(int mtd, std::string &backupFile);
void readmtd(int readmtd); void readmtd(int readmtd);
#ifdef BOXMODEL_APOLLO #ifdef BOXMODEL_APOLLO
void addDevtableEntry(int fd_dev, const char *entry); bool readDevtableFile(std::string &devtableFile, CMkfsJFFS2::v_devtable_t &v_devtable);
void readmtdJFFS2(std::string &filename); void readmtdJFFS2(std::string &filename);
#endif #endif

View File

@@ -69,6 +69,7 @@
#include <string> #include <string>
#include "rbtree.h" #include "rbtree.h"
#include <system/helpers.h>
#include <system/localize.h> #include <system/localize.h>
extern CLocaleManager *g_Locale; extern CLocaleManager *g_Locale;
@@ -891,7 +892,7 @@ void CMkfsJFFS2::create_target_filesystem(struct filesystem_entry *root)
Regular files must exist in the target root directory. If a char, Regular files must exist in the target root directory. If a char,
block, fifo, or directory does not exist, it will be created. block, fifo, or directory does not exist, it will be created.
*/ */
int CMkfsJFFS2::interpret_table_entry(struct filesystem_entry *root, char *line) bool CMkfsJFFS2::interpret_table_entry(struct filesystem_entry *root, const char *line)
{ {
char *hostpath; char *hostpath;
char type; char type;
@@ -901,9 +902,9 @@ int CMkfsJFFS2::interpret_table_entry(struct filesystem_entry *root, char *line)
struct filesystem_entry *entry; struct filesystem_entry *entry;
memset(name, '\0', 512); memset(name, '\0', 512);
if (sscanf (line, "%511s %c %lo %lu %lu %lu %lu %lu %lu %lu", if (sscanf(line, "%511s %c %lo %lu %lu %lu %lu %lu %lu %lu",
name, &type, &mode, &uid, &gid, &major, &minor, &start, &increment, &count) < 0) name, &type, &mode, &uid, &gid, &major, &minor, &start, &increment, &count) < 0)
return 1; return false;
if (!strcmp(name, "/")) { if (!strcmp(name, "/")) {
sys_errmsg("Device table entries require absolute paths"); sys_errmsg("Device table entries require absolute paths");
@@ -956,7 +957,7 @@ int CMkfsJFFS2::interpret_table_entry(struct filesystem_entry *root, char *line)
if (parent == NULL) { if (parent == NULL) {
errmsg ("skipping device_table entry '%s': no parent directory!", name); errmsg ("skipping device_table entry '%s': no parent directory!", name);
free(hostpath); free(hostpath);
return 1; return false;
} }
switch (type) { switch (type) {
@@ -996,50 +997,25 @@ int CMkfsJFFS2::interpret_table_entry(struct filesystem_entry *root, char *line)
} }
} }
free(hostpath); free(hostpath);
return 0; return true;
} }
int CMkfsJFFS2::parse_device_table(struct filesystem_entry *root, FILE * file) bool CMkfsJFFS2::parse_device_table(struct filesystem_entry *root, v_devtable_t *v_devtable)
{ {
char *line;
int status = 0;
size_t length = 0;
/* Turn off squash, since we must ensure that values /* Turn off squash, since we must ensure that values
* entered via the device table are not squashed */ * entered via the device table are not squashed */
squash_uids = 0; squash_uids = 0;
squash_perms = 0; squash_perms = 0;
/* Looks ok so far. The general plan now is to read in one bool status = false;
* line at a time, check for leading comment delimiters ('#'), std::vector<std::string>::iterator it = v_devtable->begin();
* then try and parse the line as a device table. If we fail while (it < v_devtable->end()) {
* to parse things, try and help the poor fool to fix their std::string line = *it;
* device table with a useful error msg... */ line = trim(line);
line = NULL; if (interpret_table_entry(root, line.c_str()))
while (getline(&line, &length, file) != -1) { status = true;
/* First trim off any whitespace */ ++it;
int len = strlen(line);
/* trim trailing whitespace */
while (len > 0 && isspace(line[len - 1]))
line[--len] = '\0';
/* trim leading whitespace */
memmove(line, &line[strspn(line, " \n\r\t\v")], len);
/* How long are we after trimming? */
len = strlen(line);
/* If this is NOT a comment line, try to interpret it */
if (len && *line != '#') {
if (interpret_table_entry(root, line))
status = 1;
}
free(line);
line = NULL;
} }
fclose(file);
return status; return status;
} }
@@ -1075,18 +1051,13 @@ struct filesystem_entry *CMkfsJFFS2::recursive_add_host_directory(
} }
if (((sb.st_dev == dev_x[dev_dev]) && (strstr(targetpath, "/dev") == targetpath)) || if (((sb.st_dev == dev_x[dev_dev]) && (strstr(targetpath, "/dev") == targetpath)) ||
((sb.st_dev == dev_x[dev_proc]) && (strstr(targetpath, "/proc") == targetpath)) || ((sb.st_dev == dev_x[dev_proc]) && (strstr(targetpath, "/proc") == targetpath)) ||
((sb.st_dev == dev_x[dev_sys]) && (strstr(targetpath, "/sys") == targetpath)) || ((sb.st_dev == dev_x[dev_sys]) && (strstr(targetpath, "/sys") == targetpath)) ||
((sb.st_dev == dev_x[dev_tmp]) && (strstr(targetpath, "/tmp") == targetpath))) { ((sb.st_dev == dev_x[dev_tmp]) && (strstr(targetpath, "/tmp") == targetpath))) {
skipCheck = true; skipCheck = true;
} }
if ((!skipCheck) && if ((!skipCheck) && (sb.st_dev != dev_x[dev_jffs2])) /* jffs2 */
#if 0
(sb.st_dev != dev_x[dev_dev]) && /* /dev */
(sb.st_dev != dev_x[dev_pts]) && /* /dev/pts */
#endif
(sb.st_dev != dev_x[dev_jffs2])) /* jffs2 */
return NULL; return NULL;
} }
@@ -1167,14 +1138,14 @@ struct filesystem_entry *CMkfsJFFS2::recursive_add_host_directory(
bool CMkfsJFFS2::makeJffs2Image(std::string& path, bool CMkfsJFFS2::makeJffs2Image(std::string& path,
std::string& imageName, std::string& imageName,
int eraseBlockSize/*=0x20000*/, int eraseBlockSize,
int padFsSize/*=0*/, int padFsSize,
int addCleanmarkers/*=0*/, int addCleanmarkers,
int targetEndian/*=__LITTLE_ENDIAN*/, int targetEndian,
bool skipSpezialFolders/*=true*/, bool skipSpezialFolders,
bool useSumtool/*=true*/, bool useSumtool,
CProgressWindow *progress/*=NULL*/, CProgressWindow *progress/*=NULL*/,
std::string devTable/*=""*/) v_devtable_t *v_devtable/*=NULL*/)
{ {
Init(); Init();
@@ -1182,7 +1153,6 @@ bool CMkfsJFFS2::makeJffs2Image(std::string& path,
imageName_ = imageName; imageName_ = imageName;
useSumtool_ = useSumtool; useSumtool_ = useSumtool;
erase_block_size = eraseBlockSize; // -e erase_block_size = eraseBlockSize; // -e
FILE *devtable = NULL; // -D
pad_fs_size = padFsSize; // -p pad_fs_size = padFsSize; // -p
add_cleanmarkers = addCleanmarkers; // -n add_cleanmarkers = addCleanmarkers; // -n
target_endian = targetEndian; // -l target_endian = targetEndian; // -l
@@ -1193,7 +1163,6 @@ bool CMkfsJFFS2::makeJffs2Image(std::string& path,
hardlinks.rb_node = NULL; hardlinks.rb_node = NULL;
// printf("[%s] erase_block_size: 0x%X\n", __FUNCTION__, eraseBlockSize); // printf("[%s] erase_block_size: 0x%X\n", __FUNCTION__, eraseBlockSize);
classInit(); classInit();
char *cwd; char *cwd;
@@ -1208,7 +1177,6 @@ bool CMkfsJFFS2::makeJffs2Image(std::string& path,
if (skipSpezialFolders) { if (skipSpezialFolders) {
std::string tmpPath = (path == "/") ? "" : path; std::string tmpPath = (path == "/") ? "" : path;
// std::string path_x[dev_max] = {tmpPath + "/sys", tmpPath + "/proc", tmpPath + "/tmp", tmpPath + "/dev/pts", tmpPath + "/dev", tmpPath + "/"};
std::string path_x[dev_max] = {tmpPath + "/sys", tmpPath + "/proc", tmpPath + "/tmp", tmpPath + "/dev", tmpPath + "/"}; std::string path_x[dev_max] = {tmpPath + "/sys", tmpPath + "/proc", tmpPath + "/tmp", tmpPath + "/dev", tmpPath + "/"};
for (int ix = dev_sys; ix < dev_max; ix++) { for (int ix = dev_sys; ix < dev_max; ix++) {
@@ -1230,10 +1198,8 @@ bool CMkfsJFFS2::makeJffs2Image(std::string& path,
progressBar->showGlobalStatus(50); progressBar->showGlobalStatus(50);
} }
if (devTable != "") if ((v_devtable != NULL) && (!v_devtable->empty()))
devtable = fopen(devTable.c_str(), "r"); parse_device_table(fse_root, v_devtable);
if (devtable)
parse_device_table(fse_root, devtable);
pid_t pid; pid_t pid;
std::string pcmd = "du -sx " + rootdir; std::string pcmd = "du -sx " + rootdir;

View File

@@ -24,18 +24,21 @@
*/ */
#include <string> #include <string>
#include <vector>
#include <gui/widget/progresswindow.h> #include <gui/widget/progresswindow.h>
#include <system/helpers.h> #include <system/helpers.h>
class CMkfsJFFS2 class CMkfsJFFS2
{ {
public:
typedef std::vector<std::string> v_devtable_t;
private: private:
enum { enum {
dev_sys = 0, dev_sys = 0,
dev_proc = 1, dev_proc = 1,
dev_tmp = 2, dev_tmp = 2,
/* dev_pts = 3,*/
dev_dev = 3, dev_dev = 3,
dev_jffs2 = 4, dev_jffs2 = 4,
dev_max = 5 dev_max = 5
@@ -70,8 +73,8 @@ class CMkfsJFFS2
struct filesystem_entry *parent, const char *targetpath, struct filesystem_entry *parent, const char *targetpath,
const char *hostpath, bool skipSpezialFolders, const char *hostpath, bool skipSpezialFolders,
CProgressWindow *progress=NULL); CProgressWindow *progress=NULL);
int parse_device_table(struct filesystem_entry *root, FILE * file); bool parse_device_table(struct filesystem_entry *root, v_devtable_t *v_devtable);
int interpret_table_entry(struct filesystem_entry *root, char *line); bool interpret_table_entry(struct filesystem_entry *root, const char *line);
void create_target_filesystem(struct filesystem_entry *root); void create_target_filesystem(struct filesystem_entry *root);
void recursive_populate_directory(struct filesystem_entry *dir); void recursive_populate_directory(struct filesystem_entry *dir);
void padblock(void); void padblock(void);
@@ -100,14 +103,14 @@ class CMkfsJFFS2
bool makeJffs2Image(std::string& path, bool makeJffs2Image(std::string& path,
std::string& imageName, std::string& imageName,
int eraseBlockSize=0x20000, int eraseBlockSize,
int padFsSize=0, int padFsSize,
int addCleanmarkers=0, int addCleanmarkers,
int targetEndian=__LITTLE_ENDIAN, int targetEndian,
bool skipSpezialFolders=true, bool skipSpezialFolders,
bool useSumtool=true, bool useSumtool,
CProgressWindow *progress=NULL, CProgressWindow *progress=NULL,
std::string devTable=""); v_devtable_t *v_devtable=NULL);
}; };