This commit is contained in:
Robbbert 2017-07-30 15:30:04 +10:00
commit 6d365c12cb
4 changed files with 175 additions and 372 deletions

View File

@ -468,6 +468,28 @@ std::string normalize_unicode(const char *s, size_t length, unicode_normalizatio
} }
//-------------------------------------------------
// uchar_toupper - uses utf8proc to convert to
// upper case
//-------------------------------------------------
char32_t uchar_toupper(char32_t ch)
{
return utf8proc_toupper(ch);
}
//-------------------------------------------------
// uchar_tolower - uses utf8proc to convert to
// lower case
//-------------------------------------------------
char32_t uchar_tolower(char32_t ch)
{
return utf8proc_tolower(ch);
}
//------------------------------------------------- //-------------------------------------------------
// utf8_previous_char - return a pointer to the // utf8_previous_char - return a pointer to the
// previous character in a string // previous character in a string

View File

@ -111,6 +111,10 @@ std::string normalize_unicode(const std::string &s, unicode_normalization_form n
std::string normalize_unicode(const char *s, unicode_normalization_form normalization_form); std::string normalize_unicode(const char *s, unicode_normalization_form normalization_form);
std::string normalize_unicode(const char *s, size_t length, unicode_normalization_form normalization_form); std::string normalize_unicode(const char *s, size_t length, unicode_normalization_form normalization_form);
// upper and lower case
char32_t uchar_toupper(char32_t ch);
char32_t uchar_tolower(char32_t ch);
// misc UTF-8 helpers // misc UTF-8 helpers
const char *utf8_previous_char(const char *utf8string); const char *utf8_previous_char(const char *utf8string);
bool utf8_is_valid_string(const char *utf8string); bool utf8_is_valid_string(const char *utf8string);

View File

@ -144,21 +144,6 @@ static void internal_error(const imgtool_module *module, const char *message)
} }
//-------------------------------------------------
// normalize_filename - convert a filename to the
// native format used by the module
//-------------------------------------------------
char *imgtool::partition::normalize_filename(const char *src)
{
// get charconverter from module
imgtool::charconverter *charconverter = (imgtool::charconverter *) get_info_ptr(IMGTOOLINFO_PTR_CHARCONVERTER);
// and convert
return core_strdup(charconverter ? charconverter->from_utf8(src).c_str() : src);
}
//------------------------------------------------- //-------------------------------------------------
// imgtool_init - initializes the imgtool core // imgtool_init - initializes the imgtool core
//------------------------------------------------- //-------------------------------------------------
@ -1125,81 +1110,78 @@ std::string imgtool::image::info()
// into a NUL delimited list // into a NUL delimited list
//------------------------------------------------- //-------------------------------------------------
imgtoolerr_t imgtool::partition::cannonicalize_path(uint32_t flags, const char **path, char **alloc_path) imgtoolerr_t imgtool::partition::cannonicalize_path(uint32_t flags, const char *path, std::string &result)
{ {
imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS; imgtoolerr_t err = (imgtoolerr_t)IMGTOOLERR_SUCCESS;
char *new_path = nullptr;
char path_separator, alt_path_separator;
const char *s;
int in_path_separator, i, j;
path_separator = m_path_separator; // is this path NULL? if so, is that ignored?
alt_path_separator = m_alternate_path_separator; if (!path && (flags & PATH_LEAVENULLALONE))
return IMGTOOLERR_SUCCESS;
/* is this path NULL? if so, is that ignored? */ // is this the special filename for bootblocks?
if (!*path && (flags & PATH_LEAVENULLALONE)) if (path == FILENAME_BOOTBLOCK)
goto done;
/* is this the special filename for bootblocks? */
if (*path == FILENAME_BOOTBLOCK)
{ {
if (!(flags & PATH_CANBEBOOTBLOCK)) if (!(flags & PATH_CANBEBOOTBLOCK))
err = (imgtoolerr_t)IMGTOOLERR_UNEXPECTED; err = (imgtoolerr_t)IMGTOOLERR_UNEXPECTED;
else if (!m_supports_bootblock) else if (!m_supports_bootblock)
err = (imgtoolerr_t)IMGTOOLERR_FILENOTFOUND; err = (imgtoolerr_t)IMGTOOLERR_FILENOTFOUND;
goto done; return err;
} }
if (path_separator == '\0') // normalize the path into the native character set
std::string converted_path;
imgtool::charconverter *charconverter = (imgtool::charconverter *) get_info_ptr(IMGTOOLINFO_PTR_CHARCONVERTER);
if (charconverter)
{
converted_path = charconverter->from_utf8(path);
path = converted_path.c_str();
}
if (m_path_separator == '\0')
{ {
if (flags & PATH_MUSTBEDIR) if (flags & PATH_MUSTBEDIR)
{ {
/* do we specify a path when paths are not supported? */ // do we specify a path when paths are not supported? */
if (*path && **path) if (path && *path)
{ return imgtoolerr_t(IMGTOOLERR_CANNOTUSEPATH | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_CANNOTUSEPATH | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done; result = ""; /* normalize empty path */
} }
*path = nullptr; /* normalize empty path */ else
{
// simple passthrough
result = path;
} }
} }
else else
{ {
s = *path ? *path : ""; const char *s = path ? path : "";
// allocate space for a new cannonical path // allocate space for a new cannonical path
new_path = (char*)malloc(strlen(s) + 4); std::ostringstream stream;
if (!new_path)
{
err = (imgtoolerr_t)IMGTOOLERR_OUTOFMEMORY;
goto done;
}
/* copy the path */ // copy the path
in_path_separator = true; bool in_path_separator = true;
i = j = 0; size_t i = 0;
do do
{ {
if ((s[i] != '\0') && (s[i] != path_separator) && (s[i] != alt_path_separator)) if ((s[i] != '\0') && (s[i] != m_path_separator) && (s[i] != m_alternate_path_separator))
{ {
new_path[j++] = s[i]; stream << s[i];
in_path_separator = false; in_path_separator = false;
} }
else if (!in_path_separator) else if (!in_path_separator)
{ {
new_path[j++] = '\0'; stream << '\0';
in_path_separator = true; in_path_separator = true;
} }
} }
while(s[i++] != '\0'); while(s[i++] != '\0');
new_path[j++] = '\0'; stream << '\0' << '\0';
new_path[j++] = '\0'; result = stream.str();
*path = new_path;
} }
done: return IMGTOOLERR_SUCCESS;
*alloc_path = new_path;
return err;
} }
@ -1317,41 +1299,23 @@ done:
imgtoolerr_t imgtool::partition::list_file_attributes(const char *path, uint32_t *attrs, size_t len) imgtoolerr_t imgtool::partition::list_file_attributes(const char *path, uint32_t *attrs, size_t len)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_fname = nullptr;
memset(attrs, 0, sizeof(*attrs) * len); memset(attrs, 0, sizeof(*attrs) * len);
if (!m_list_attrs) if (!m_list_attrs)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_fname = normalize_filename(path);
if (new_fname == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
path = new_fname;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_LEAVENULLALONE, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_LEAVENULLALONE, path, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_list_attrs(*this, path, attrs, len); err = m_list_attrs(*this, cannonical_path.c_str(), attrs, len);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path != nullptr)
free(alloc_path);
if (new_fname != nullptr)
free(new_fname);
return err;
} }
@ -1363,39 +1327,20 @@ done:
imgtoolerr_t imgtool::partition::get_file_attributes(const char *path, const uint32_t *attrs, imgtool_attribute *values) imgtoolerr_t imgtool::partition::get_file_attributes(const char *path, const uint32_t *attrs, imgtool_attribute *values)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_fname = nullptr;
if (!m_get_attrs) if (!m_get_attrs)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_fname = normalize_filename(path);
if (new_fname == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
path = new_fname;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_LEAVENULLALONE, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_LEAVENULLALONE, path, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_get_attrs(*this, path, attrs, values); err = m_get_attrs(*this, cannonical_path.c_str(), attrs, values);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path != nullptr)
free(alloc_path);
if (new_fname != nullptr)
free(new_fname);
return err;
} }
@ -1407,27 +1352,20 @@ done:
imgtoolerr_t imgtool::partition::put_file_attributes(const char *path, const uint32_t *attrs, const imgtool_attribute *values) imgtoolerr_t imgtool::partition::put_file_attributes(const char *path, const uint32_t *attrs, const imgtool_attribute *values)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
if (!m_set_attrs) if (!m_set_attrs)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_LEAVENULLALONE, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_LEAVENULLALONE, path, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_set_attrs(*this, path, attrs, values); err = m_set_attrs(*this, cannonical_path.c_str(), attrs, values);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
return err;
} }
@ -1467,40 +1405,21 @@ imgtoolerr_t imgtool::partition::put_file_attribute(const char *path, uint32_t a
imgtoolerr_t imgtool::partition::get_icon_info(const char *path, imgtool_iconinfo *iconinfo) imgtoolerr_t imgtool::partition::get_icon_info(const char *path, imgtool_iconinfo *iconinfo)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_fname = nullptr;
if (!m_get_iconinfo) if (!m_get_iconinfo)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_fname = normalize_filename(path);
if (new_fname == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
path = new_fname;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(0, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(0, path, cannonical_path);
if (err) if (err)
goto done; return err;
memset(iconinfo, 0, sizeof(*iconinfo)); memset(iconinfo, 0, sizeof(*iconinfo));
err = m_get_iconinfo(*this, path, iconinfo); err = m_get_iconinfo(*this, cannonical_path.c_str(), iconinfo);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
if (new_fname)
free(new_fname);
return err;
} }
@ -1515,7 +1434,6 @@ imgtoolerr_t imgtool::partition::suggest_file_filters(const char *path,
{ {
imgtoolerr_t err; imgtoolerr_t err;
int i, j; int i, j;
char *alloc_path = nullptr;
imgtoolerr_t (*check_stream)(imgtool::stream &stream, imgtool_suggestion_viability_t *viability); imgtoolerr_t (*check_stream)(imgtool::stream &stream, imgtool_suggestion_viability_t *viability);
size_t position; size_t position;
@ -1523,20 +1441,18 @@ imgtoolerr_t imgtool::partition::suggest_file_filters(const char *path,
memset(suggestions, 0, sizeof(*suggestions) * suggestions_length); memset(suggestions, 0, sizeof(*suggestions) * suggestions_length);
if (!m_suggest_transfer) if (!m_suggest_transfer)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_LEAVENULLALONE, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_LEAVENULLALONE, path, cannonical_path);
if (err) if (err)
goto done; return err;
// invoke the module's suggest call // invoke the module's suggest call
err = m_suggest_transfer(*this, path, suggestions, suggestions_length); err = m_suggest_transfer(*this, cannonical_path.c_str(), suggestions, suggestions_length);
if (err) if (err)
goto done; return err;
// loop on resulting suggestions, and do the following: // loop on resulting suggestions, and do the following:
// 1. Call check_stream if present, and remove disqualified streams // 1. Call check_stream if present, and remove disqualified streams
@ -1553,7 +1469,7 @@ imgtoolerr_t imgtool::partition::suggest_file_filters(const char *path,
err = check_stream(*stream, &suggestions[i].viability); err = check_stream(*stream, &suggestions[i].viability);
stream->seek(position, SEEK_SET); stream->seek(position, SEEK_SET);
if (err) if (err)
goto done; return err;
} }
} }
@ -1579,10 +1495,7 @@ imgtoolerr_t imgtool::partition::suggest_file_filters(const char *path,
} }
suggestions[j].viability = (imgtool_suggestion_viability_t)0; suggestions[j].viability = (imgtool_suggestion_viability_t)0;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
return err;
} }
@ -1709,56 +1622,41 @@ imgtoolerr_t imgtool::partition::get_free_space(uint64_t &sz)
imgtoolerr_t imgtool::partition::read_file(const char *filename, const char *fork, imgtool::stream &destf, filter_getinfoproc filter) imgtoolerr_t imgtool::partition::read_file(const char *filename, const char *fork, imgtool::stream &destf, filter_getinfoproc filter)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
union filterinfo u; union filterinfo u;
if (!m_read_file) if (!m_read_file)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
if (filter) if (filter)
{ {
// use a filter // use a filter
filter(FILTINFO_PTR_READFILE, &u); filter(FILTINFO_PTR_READFILE, &u);
if (!u.read_file) if (!u.read_file)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
err = u.read_file(*this, filename, fork, destf); err = u.read_file(*this, filename, fork, destf);
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
} }
else else
{ {
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_CANBEBOOTBLOCK, &filename, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_CANBEBOOTBLOCK, filename, cannonical_path);
if (err) if (err)
goto done; return err;
err = cannonicalize_fork(&fork); err = cannonicalize_fork(&fork);
if (err) if (err)
goto done; return err;
// invoke the actual module // invoke the actual module
err = m_read_file(*this, filename, fork, destf); err = m_read_file(*this, cannonical_path.c_str(), fork, destf);
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
} }
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
return err;
} }
@ -1770,62 +1668,57 @@ done:
imgtoolerr_t imgtool::partition::write_file(const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts, filter_getinfoproc filter) imgtoolerr_t imgtool::partition::write_file(const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts, filter_getinfoproc filter)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *buf = nullptr;
char *s;
std::unique_ptr<util::option_resolution> alloc_resolution; std::unique_ptr<util::option_resolution> alloc_resolution;
char *alloc_path = nullptr;
uint64_t free_space; uint64_t free_space;
uint64_t file_size; uint64_t file_size;
union filterinfo u; union filterinfo u;
if (!m_write_file) if (!m_write_file)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
if (filter) if (filter)
{ {
// use a filter // use a filter
filter(FILTINFO_PTR_WRITEFILE, &u); filter(FILTINFO_PTR_WRITEFILE, &u);
if (!u.write_file) if (!u.write_file)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
err = u.write_file(*this, filename, fork, sourcef, opts); err = u.write_file(*this, filename, fork, sourcef, opts);
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
} }
else else
{ {
// does this partition prefer upper case file names? // does this partition prefer upper case file names?
std::string ucase_str;
if (m_prefer_ucase) if (m_prefer_ucase)
{ {
buf = (char*)malloc(strlen(filename) + 1); std::ostringstream s;
if (!buf) size_t i = 0;
while (filename[i])
{ {
err = imgtoolerr_t(IMGTOOLERR_OUTOFMEMORY); char32_t ch;
goto done; int count = uchar_from_utf8(&ch, &filename[i], UTF8_CHAR_MAX);
if (count > 0)
{
char32_t upper_ch = uchar_toupper(ch);
s << utf8_from_uchar(upper_ch);
}
i += count > 0 ? count : 1;
} }
strcpy(buf, filename); ucase_str = s.str();
for (s = buf; *s; s++) filename = ucase_str.c_str();
*s = toupper(*s);
filename = buf;
} }
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_CANBEBOOTBLOCK, &filename, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_CANBEBOOTBLOCK, filename, cannonical_path);
if (err) if (err)
goto done; return err;
err = cannonicalize_fork(&fork); err = cannonicalize_fork(&fork);
if (err) if (err)
goto done; return err;
// allocate dummy options if necessary // allocate dummy options if necessary
if (!opts && m_writefile_optguide) if (!opts && m_writefile_optguide)
@ -1833,8 +1726,7 @@ imgtoolerr_t imgtool::partition::write_file(const char *filename, const char *fo
try { alloc_resolution.reset(new util::option_resolution(*m_writefile_optguide)); } try { alloc_resolution.reset(new util::option_resolution(*m_writefile_optguide)); }
catch (...) catch (...)
{ {
err = IMGTOOLERR_OUTOFMEMORY; return IMGTOOLERR_OUTOFMEMORY;
goto done;
} }
if (!m_writefile_optspec.empty()) if (!m_writefile_optspec.empty())
alloc_resolution->set_specification(m_writefile_optspec); alloc_resolution->set_specification(m_writefile_optspec);
@ -1846,35 +1738,21 @@ imgtoolerr_t imgtool::partition::write_file(const char *filename, const char *fo
{ {
err = m_free_space(*this, &free_space); err = m_free_space(*this, &free_space);
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
file_size = sourcef.size(); file_size = sourcef.size();
if (file_size > free_space) if (file_size > free_space)
{ return markerrorsource(IMGTOOLERR_NOSPACE);
err = markerrorsource(IMGTOOLERR_NOSPACE);
goto done;
}
} }
// actually invoke the write file handler // actually invoke the write file handler
err = m_write_file(*this, filename, fork, sourcef, opts); err = m_write_file(*this, cannonical_path.c_str(), fork, sourcef, opts);
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
} }
done: return IMGTOOLERR_SUCCESS;
if (buf)
free(buf);
if (alloc_path)
free(alloc_path);
return err;
} }
@ -1923,13 +1801,6 @@ imgtoolerr_t imgtool::partition::get_file(const char *filename, const char *fork
goto done; goto done;
} }
new_fname = normalize_filename(filename);
if (new_fname == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
err = read_file(new_fname, fork, *f, filter); err = read_file(new_fname, fork, *f, filter);
if (err) if (err)
goto done; goto done;
@ -1996,42 +1867,20 @@ imgtoolerr_t imgtool::partition::put_file(const char *newfname, const char *fork
imgtoolerr_t imgtool::partition::delete_file(const char *fname) imgtoolerr_t imgtool::partition::delete_file(const char *fname)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_fname = nullptr;
if (!m_delete_file) if (!m_delete_file)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_fname = normalize_filename(fname);
if (new_fname == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
fname = new_fname;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(0, &fname, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(0, fname, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_delete_file(*this, fname); err = m_delete_file(*this, cannonical_path.c_str());
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
if (new_fname)
free(new_fname);
return err;
} }
@ -2043,39 +1892,20 @@ done:
imgtoolerr_t imgtool::partition::list_file_forks(const char *path, imgtool_forkent *ents, size_t len) imgtoolerr_t imgtool::partition::list_file_forks(const char *path, imgtool_forkent *ents, size_t len)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_fname = nullptr;
if (!m_list_forks) if (!m_list_forks)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_fname = normalize_filename(path);
if (new_fname == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
path = new_fname;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(0, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(0, path, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_list_forks(*this, path, ents, len); err = m_list_forks(*this, cannonical_path.c_str(), ents, len);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
if (new_fname)
free(new_fname);
return err;
} }
@ -2087,40 +1917,22 @@ done:
imgtoolerr_t imgtool::partition::create_directory(const char *path) imgtoolerr_t imgtool::partition::create_directory(const char *path)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_path = nullptr;
// implemented? // implemented?
if (!m_create_dir) if (!m_create_dir)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_path = normalize_filename(path);
if (new_path == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
path = new_path;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_MUSTBEDIR, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_MUSTBEDIR, path, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_create_dir(*this, path); err = m_create_dir(*this, path);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
if (new_path)
free(new_path);
return err;
} }
@ -2132,40 +1944,22 @@ done:
imgtoolerr_t imgtool::partition::delete_directory(const char *path) imgtoolerr_t imgtool::partition::delete_directory(const char *path)
{ {
imgtoolerr_t err; imgtoolerr_t err;
char *alloc_path = nullptr;
char *new_path = nullptr;
// implemented? // implemented?
if (!m_delete_dir) if (!m_delete_dir)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_path = normalize_filename(path);
if (new_path == nullptr)
{
err = imgtoolerr_t(IMGTOOLERR_BADFILENAME | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
path = new_path;
// cannonicalize path // cannonicalize path
err = cannonicalize_path(PATH_MUSTBEDIR, &path, &alloc_path); std::string cannonical_path;
err = cannonicalize_path(PATH_MUSTBEDIR, path, cannonical_path);
if (err) if (err)
goto done; return err;
err = m_delete_dir(*this, path); err = m_delete_dir(*this, path);
if (err) if (err)
goto done; return err;
done: return IMGTOOLERR_SUCCESS;
if (alloc_path)
free(alloc_path);
if (new_path)
free(new_path);
return err;
} }
@ -2439,50 +2233,34 @@ imgtoolerr_t imgtool::directory::open(imgtool::partition &partition, const std::
const char *path = path_string.c_str(); const char *path = path_string.c_str();
imgtoolerr_t err = imgtoolerr_t(IMGTOOLERR_SUCCESS); imgtoolerr_t err = imgtoolerr_t(IMGTOOLERR_SUCCESS);
imgtool::directory::ptr enumeration; imgtool::directory::ptr enumeration;
char *alloc_path = nullptr;
char *new_path = nullptr;
outenum.reset(); outenum.reset();
if (!partition.m_next_enum) if (!partition.m_next_enum)
{ return imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
err = imgtoolerr_t(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
goto done;
}
new_path = partition.normalize_filename(path); std::string cannonical_path;
path = new_path; err = partition.cannonicalize_path(PATH_MUSTBEDIR, path, cannonical_path);
err = partition.cannonicalize_path(PATH_MUSTBEDIR, &path, &alloc_path);
if (err) if (err)
goto done; return err;
try { enumeration = std::make_unique<directory>(partition); } try { enumeration = std::make_unique<directory>(partition); }
catch (std::bad_alloc const &) catch (std::bad_alloc const &)
{ {
err = imgtoolerr_t(IMGTOOLERR_OUTOFMEMORY); return imgtoolerr_t(IMGTOOLERR_OUTOFMEMORY);
goto done;
} }
if (partition.m_begin_enum) if (partition.m_begin_enum)
{ {
err = partition.m_begin_enum(*enumeration, path); err = partition.m_begin_enum(*enumeration, cannonical_path.c_str());
if (err) if (err)
{ return markerrorsource(err);
err = markerrorsource(err);
goto done;
}
} }
enumeration->m_okay_to_close = true; enumeration->m_okay_to_close = true;
outenum = std::move(enumeration); outenum = std::move(enumeration);
done: return IMGTOOLERR_SUCCESS;
if (alloc_path != nullptr)
free(alloc_path);
if (new_path != nullptr)
free(new_path);
return err;
} }
@ -2514,13 +2292,13 @@ imgtoolerr_t imgtool::directory::get_next(imgtool_dirent &ent)
if (err) if (err)
return markerrorsource(err); return markerrorsource(err);
imgtool::charconverter *charconverter = (imgtool::charconverter *) m_partition.get_info_ptr(IMGTOOLINFO_PTR_CHARCONVERTER); imgtool::charconverter * const converter = reinterpret_cast<imgtool::charconverter * const>(m_partition.get_info_ptr(IMGTOOLINFO_PTR_CHARCONVERTER));
if (charconverter) if (converter)
{ {
std::string new_fname; std::string new_fname;
try try
{ {
new_fname = charconverter->to_utf8(ent.filename); new_fname = converter->to_utf8(ent.filename);
} }
catch (charconverter_exception) catch (charconverter_exception)
{ {

View File

@ -227,9 +227,8 @@ namespace imgtool
std::unique_ptr<uint8_t[]> m_extra_bytes; std::unique_ptr<uint8_t[]> m_extra_bytes;
// methods // methods
imgtoolerr_t cannonicalize_path(uint32_t flags, const char **path, char **alloc_path); imgtoolerr_t cannonicalize_path(uint32_t flags, const char *path, std::string &result);
imgtoolerr_t cannonicalize_fork(const char **fork); imgtoolerr_t cannonicalize_fork(const char **fork);
char *normalize_filename(const char *src);
imgtoolerr_t map_block_to_image_block(uint64_t partition_block, uint64_t &image_block) const; imgtoolerr_t map_block_to_image_block(uint64_t partition_block, uint64_t &image_block) const;
}; };