ManaPlus
Functions
mkdir.cpp File Reference

(642df39)

#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 108 of file mkdir.cpp.

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

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().

CAST_SIZE
#define CAST_SIZE
Definition: cast.h:33
VirtFs::mkdir
bool mkdir(const std::string &dirname)
Definition: fs.cpp:774