Fix conversion of group and user name to strings
This commit is contained in:
parent
d851f1ee8d
commit
9517f6d74c
@ -404,6 +404,15 @@ std::vector<std::string> extract_files_excluded(const std::string& filepath) {
|
|||||||
return files_excluded;
|
return files_excluded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const std::string clean_utf8(const char* name) {
|
||||||
|
if (!name) return "unknown";
|
||||||
|
try {
|
||||||
|
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
|
||||||
|
converter.from_bytes(name);
|
||||||
|
return std::string(name);
|
||||||
|
} catch (const std::range_error&) return "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
void create_tarball(const std::string &tarball_path,
|
void create_tarball(const std::string &tarball_path,
|
||||||
const std::string &directory,
|
const std::string &directory,
|
||||||
const std::vector<std::string> &exclusions,
|
const std::vector<std::string> &exclusions,
|
||||||
@ -448,16 +457,10 @@ void create_tarball(const std::string &tarball_path,
|
|||||||
std::string top_dir = base_dir_str + "/";
|
std::string top_dir = base_dir_str + "/";
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
if (stat(top_dir.c_str(), &file_stat) == 0) {
|
if (stat(top_dir.c_str(), &file_stat) == 0) {
|
||||||
if (struct passwd *pw = getpwuid(file_stat.st_uid)) {
|
std::string uname = clean_utf8(getpwuid(file_stat.st_uid) ? getpwuid(file_stat.st_uid)->pw_name : "lugito");
|
||||||
if (char *name = (char *)malloc(strlen(pw->pw_name) + 1)) {
|
std::string gname = clean_utf8(getgrgid(file_stat.st_gid) ? getgrgid(file_stat.st_gid)->gr_name : "lugito");
|
||||||
archive_entry_set_uname(entry, name);
|
archive_entry_set_uname(entry, uname);
|
||||||
}
|
archive_entry_set_gname(entry, gname);
|
||||||
}
|
|
||||||
if (struct group *gr = getgrgid(file_stat.st_gid)) {
|
|
||||||
if (char *name = (char *)malloc(strlen(gr->gr_name) + 1)) {
|
|
||||||
archive_entry_set_gname(entry, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
archive_entry_set_uid(entry, file_stat.st_uid);
|
archive_entry_set_uid(entry, file_stat.st_uid);
|
||||||
archive_entry_set_gid(entry, file_stat.st_gid);
|
archive_entry_set_gid(entry, file_stat.st_gid);
|
||||||
archive_entry_set_perm(entry, file_stat.st_mode);
|
archive_entry_set_perm(entry, file_stat.st_mode);
|
||||||
@ -529,16 +532,10 @@ void create_tarball(const std::string &tarball_path,
|
|||||||
|
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
if (stat(path.c_str(), &file_stat) == 0) {
|
if (stat(path.c_str(), &file_stat) == 0) {
|
||||||
if (struct passwd *pw = getpwuid(file_stat.st_uid)) {
|
std::string uname = clean_utf8(getpwuid(file_stat.st_uid) ? getpwuid(file_stat.st_uid)->pw_name : "lugito");
|
||||||
if (char *name = (char *)malloc(strlen(pw->pw_name) + 1)) {
|
std::string gname = clean_utf8(getgrgid(file_stat.st_gid) ? getgrgid(file_stat.st_gid)->gr_name : "lugito");
|
||||||
archive_entry_set_uname(entry, name);
|
archive_entry_set_uname(entry, uname);
|
||||||
}
|
archive_entry_set_gname(entry, gname);
|
||||||
}
|
|
||||||
if (struct group *gr = getgrgid(file_stat.st_gid)) {
|
|
||||||
if (char *name = (char *)malloc(strlen(gr->gr_name) + 1)) {
|
|
||||||
archive_entry_set_gname(entry, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
archive_entry_set_uid(entry, file_stat.st_uid);
|
archive_entry_set_uid(entry, file_stat.st_uid);
|
||||||
archive_entry_set_gid(entry, file_stat.st_gid);
|
archive_entry_set_gid(entry, file_stat.st_gid);
|
||||||
archive_entry_set_perm(entry, file_stat.st_mode);
|
archive_entry_set_perm(entry, file_stat.st_mode);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user