Skip to content
Snippets Groups Projects
Commit 851e166b authored by Ronny Nilsson's avatar Ronny Nilsson
Browse files

Reduce duplicated code by moving UBI path name generation to separate function.

parent a421c489
No related branches found
No related tags found
No related merge requests found
......@@ -116,12 +116,86 @@ static unsigned int ubi_read_blk(unsigned char *start, unsigned int blk, unsigne
//-------------------------------------------------------------
// Create a path string to a UBI character device.
// Returned string must be freed by user!
static char* ubi_path_by_id(int devId, int volId)
{
const int DST_LEN = UBI_MAX_VOLUME_NAME + 16;
struct ubi_vol_info volInfo;
struct stat dstStat;
char *dstPath;
int res;
res = 0;
dstPath = calloc(1, DST_LEN);
if(!dstPath) return NULL;
// Generate path string with a traling null.
snprintf(dstPath, DST_LEN, "/dev/ubi%d", devId);
if(volId >= 0 && volId < UBI_MAX_VOLUMES) {
snprintf(dstPath + strlen(dstPath), DST_LEN - strlen(dstPath),
"_%d", volId);
}
dstPath[DST_LEN - 1] = 0;
if(volId == -1) return dstPath;
/* If the volume is supposed to exsit; verify it does.
* Otherwise; verify it does NOT exist. */
errno = 0;
res = ubi_get_vol_info1(libubi, devId, volId, &volInfo);
if(res == -1) {
if(errno == ENODEV || errno == ENOENT) {
if(stat(dstPath, &dstStat) && (errno == ENODEV || errno == ENOENT)) {
return dstPath;
}
else {
fprintf(stderr, "Incorrectly found UBI volume %d!\n", volId);
res = -1;
goto out;
}
}
else {
fprintf(stderr, "Error reading UBI volume %d: %s\n",
volId, strerror(errno));
res = -1;
goto out;
}
}
/* The volume is supposed to exist. Now verify our
* string points to the correct character device. */
res = stat(dstPath, &dstStat);
if(res == -1) {
fprintf(stderr, "Error investigating file %s: %s", dstPath, strerror(errno));
goto out;
}
else if(!S_ISCHR(dstStat.st_mode) || volInfo.major != major(dstStat.st_rdev) ||
volInfo.minor != minor(dstStat.st_rdev)) {
fprintf(stderr, "BUG: wrong choice of destination! %s %d %u %d %u\n",
dstPath, volInfo.major, major(dstStat.st_rdev), volInfo.minor,
minor(dstStat.st_rdev));
res = -1;
goto out;
}
out:
if(res) {
free(dstPath);
dstPath = NULL;
}
return dstPath;
}
//-------------------------------------------------------------
// Erase an UBI volume (if it exists).
int ubi_erase_volume(int volId)
{
char nodePath[UBI_MAX_VOLUME_NAME];
struct ubi_vol_info volInfo;
char *nodePath;
memset(&volInfo, 0, sizeof(volInfo));
......@@ -134,12 +208,14 @@ int ubi_erase_volume(int volId)
return -1;
}
snprintf(nodePath, UBI_MAX_VOLUME_NAME, "/dev/ubi%d", devInfo.dev_num);
nodePath = ubi_path_by_id(devInfo.dev_num, -1);
if(ubi_rmvol(libubi, nodePath, volId)) {
fprintf(stderr, "Error deleting volume %d: %s\n", volId, strerror(errno));
free(nodePath);
return -1;
}
free(nodePath);
return 0;
}
......@@ -149,8 +225,8 @@ int ubi_erase_volume(int volId)
// Create a new static UBI volume of <size> bytes.
int ubi_create_volume(int volId, long long size)
{
char nodePath[UBI_MAX_VOLUME_NAME];
struct ubi_mkvol_request mkReq;
char *nodePath;
memset(&mkReq, 0, sizeof(mkReq));
mkReq.vol_id = volId;
......@@ -158,18 +234,20 @@ int ubi_create_volume(int volId, long long size)
mkReq.bytes = size;
mkReq.vol_type = UBI_STATIC_VOLUME;
mkReq.name = ubiStr3;
snprintf(nodePath, UBI_MAX_VOLUME_NAME, "/dev/ubi%d", devInfo.dev_num);
nodePath = ubi_path_by_id(devInfo.dev_num, -1);
errno = 0;
if(ubi_mkvol(libubi, nodePath, &mkReq)) {
fprintf(stderr, "Error creating UBI volume %d %s: %s\n",
volId, nodePath, strerror(errno));
free(nodePath);
return -1;
}
else if (verbose) {
printf("Created UBI %d of %lld bytes.\n", volId, size);
}
free(nodePath);
return 0;
}
......@@ -182,21 +260,22 @@ int ubi_create_volume(int volId, long long size)
static int ubi_write_volume(int volId, const char *srcPath)
{
int srcFd, dstFd, res, readChunk, writtenChunk;
char dstPath[UBI_MAX_VOLUME_NAME], *buf;
struct stat srcStat, dstStat;
struct ubi_vol_info volInfo;
long long writtenTot;
char *dstPath, *buf;
res = 0;
errno = 0;
srcFd = -1;
dstFd = -1;
dstPath = NULL;
writtenTot = 0ll;
buf = malloc(devInfo.leb_size);
// Sanity check the volume before overwriting it.
// Sanity check the volume arg before overwriting anything.
if(ubi_get_vol_info1(libubi, devInfo.dev_num, volId, &volInfo)) {
fprintf(stderr, "Error reading UBI volume %d: %s\n",
fprintf(stderr, "Error investigating UBI volume %d: %s\n",
volId, strerror(errno));
res = -1;
goto out;
......@@ -207,9 +286,7 @@ static int ubi_write_volume(int volId, const char *srcPath)
goto out;
}
/* Open the source data file and the destination volume
* character device. Further sanity check them and prepare
* them for work. */
// Open the source data file and sanity check it.
srcFd = open(srcPath, O_RDONLY);
if(srcFd == -1) {
fprintf(stderr, "Error opening file %s: %s", srcPath, strerror(errno));
......@@ -227,8 +304,14 @@ static int ubi_write_volume(int volId, const char *srcPath)
goto out;
}
snprintf(dstPath, UBI_MAX_VOLUME_NAME, "/dev/ubi%d_%d",
devInfo.dev_num, volId);
// Generate a UBI volume path string.
dstPath = ubi_path_by_id(devInfo.dev_num, volId);
if(!dstPath) {
res = -1;
goto out;
}
// Open the UBI volume and sanity check it.
dstFd = open(dstPath, O_WRONLY);
if(dstFd == -1) {
fprintf(stderr, "Error opening file %s: %s", dstPath, strerror(errno));
......@@ -248,13 +331,13 @@ static int ubi_write_volume(int volId, const char *srcPath)
res = -1;
goto out;
}
if(volInfo.rsvd_bytes % volInfo.leb_size || volInfo.data_bytes) {
else if(volInfo.rsvd_bytes % volInfo.leb_size || volInfo.data_bytes) {
fprintf(stderr, "Error volume %s to write isn't empty!", dstPath);
res = -1;
goto out;
}
// All checks passed. Begin write!
if(ubi_update_start(libubi, dstFd, volInfo.rsvd_bytes)) {
fprintf(stderr, "Error preparing volume %s: %s", dstPath, strerror(errno));
res = -1;
......@@ -302,6 +385,7 @@ out:
if(srcFd != -1) close(srcFd);
if(dstFd != -1) close(dstFd);
free(buf);
free(dstPath);
return res;
}
......@@ -459,12 +543,8 @@ static int meta_find_all(void)
case METAVOLID3:
metaInfos[i].found = 1;
metaInfos[i].volInfo = volInfo;
metaInfos[i].nodePath = malloc(UBI_MAX_VOLUME_NAME);
snprintf(metaInfos[i].nodePath, UBI_MAX_VOLUME_NAME,
"/dev/ubi%d", devInfo.dev_num);
metaInfos[i].volPath = malloc(UBI_MAX_VOLUME_NAME);
snprintf(metaInfos[i].volPath, UBI_MAX_VOLUME_NAME,
"/dev/ubi%d_%d", devInfo.dev_num, volInfo.vol_id);
metaInfos[i].nodePath = ubi_path_by_id(devInfo.dev_num, -1);
metaInfos[i].volPath = ubi_path_by_id(devInfo.dev_num, volInfo.vol_id);
if(meta_find_seqno(metaInfos + i)) {
if (verbose) {
printf("Found %s in %s but it's corrupt.\n",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment