ManaPlus
Functions
mkdir.cpp File Reference

(986a3bf)

#include "fs/mkdir.h"
#include "utils/cast.h"
#include <sys/stat.h>
#include "debug.h"

Go to the source code of this file.

Functions

int mkdir_r (const char *const pathname)
 Create a directory, making leading components first if necessary. More...
 

Function Documentation

◆ mkdir_r()

int mkdir_r ( const char *const  pathname)

Create a directory, making leading components first if necessary.

Definition at line 109 of file mkdir.cpp.

110 {
111  if (pathname == nullptr)
112  return -1;
113 
114  const size_t len = CAST_SIZE(strlen(pathname));
115  char *tmp = new char[len + 2];
116  char *p = nullptr;
117 
118  strcpy(tmp, pathname);
119 
120  // terminate the pathname with '/'
121  if (tmp[len - 1] != '/')
122  {
123  tmp[len] = '/';
124  tmp[len + 1] = '\0';
125  }
126 
127  for (p = tmp; *p != 0; p++)
128  {
129  if (*p == '/')
130  {
131  *p = '\0';
132  // ignore a slash at the beginning of a path
133  if (tmp[0] == 0)
134  {
135  *p = '/';
136  continue;
137  }
138 
139  // check if the name already exists, but not as directory
140  struct stat statbuf;
141  if (stat(tmp, &statbuf) == 0)
142  {
143  if (S_ISDIR(statbuf.st_mode))
144  {
145  *p = '/';
146  continue;
147  }
148  else
149  {
150  delete []tmp;
151  return -1;
152  }
153  }
154 
155  if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
156  {
157  delete []tmp;
158  return -1;
159  }
160 
161  *p = '/';
162  }
163  }
164  delete []tmp;
165  return 0;
166 }
#define CAST_SIZE
Definition: cast.h:34
bool mkdir(const std::string &dirname)
Definition: fs.cpp:775

References CAST_SIZE, and VirtFs::mkdir().

Referenced by Dirs::initConfigDir(), Dirs::initLocalDataDir(), Dirs::initScreenshotDir(), ConfigManager::initServerConfig(), Dirs::initTempDir(), Dirs::initUsersDir(), UpdaterWindow::loadNews(), mainGui(), VirtFs::FsDir::mkdir(), Map::saveExtraLayer(), Game::saveScreenshot(), Files::saveTextFile(), ChatLogger::setLogDir(), and ChatLogger::setServerName().