Skip to content

Commit

Permalink
tempdir: Use g_mkdtemp to make directories for booru image lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ahodesuka committed Oct 14, 2018
1 parent 3f74dba commit d65aee0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/booru/imagelist.cc
Expand Up @@ -40,11 +40,9 @@ void ImageList::clear()

std::string ImageList::get_path()
{
static int id = 1;

if (m_Path.empty())
{
m_Path = TempDir::get_instance().make_dir(std::to_string(id++));
m_Path = TempDir::get_instance().make_dir();
g_mkdir_with_parents(Glib::build_filename(m_Path, "thumbnails").c_str(), 0755);
}

Expand Down
28 changes: 19 additions & 9 deletions src/tempdir.h
Expand Up @@ -20,19 +20,29 @@ namespace AhoViewer
return i;
}

std::string make_dir(const std::string &dirPath)
std::string make_dir(std::string dirPath = "")
{
std::string path(Glib::build_filename(m_Path, dirPath));
std::string path;

// Loop until we have a unique directory name
for (size_t i = 1; Glib::file_test(path, Glib::FILE_TEST_EXISTS); ++i)
path = Glib::build_filename(m_Path, dirPath + "-" + std::to_string(i));

if (g_mkdir_with_parents(path.c_str(), 0755) == -1)
if (dirPath == "")
{
std::cerr << "g_mkdir_with_parents: Failed to create '" << path << "'" << std::endl;
return "";
std::string tmpl(Glib::build_filename(m_Path, "XXXXXX"));
path = g_mkdtemp_full(const_cast<char*>(tmpl.c_str()), 0755);
}
else
{
path = Glib::build_filename(m_Path, dirPath);
// Loop until we have a unique directory name
for (size_t i = 1; Glib::file_test(path, Glib::FILE_TEST_EXISTS); ++i)
path = Glib::build_filename(m_Path, dirPath + "-" + std::to_string(i));

if (g_mkdir_with_parents(path.c_str(), 0755) == -1)
{
std::cerr << "g_mkdir_with_parents: Failed to create '" << path << "'" << std::endl;
return "";
}
}

return path;
}
void remove_dir(const std::string &dirPath)
Expand Down

0 comments on commit d65aee0

Please sign in to comment.