1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009,2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the directory helper functions
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "../common.h"
29 #include <sys/types.h>
35 /* ret=true, err=0 - directory was created successfully */
36 /* ret=true, err=EEXIST - directory already exists */
37 /* ret=true, err=ENOENT - A directory component in dirname does not exist or is a dangling symbolic link */
38 /* ret=true, err=error - directory creation failed in a bad way */
39 /* ret=false, err=error - dirname is not valid directory path */
40 static bool safe_mkdir(const char * dirname
, int mode
, int * err
)
44 ASSERT(*dirname
); /* empty string? */
46 if (mkdir(dirname
, mode
) == 0)
58 /* dirname path exists, not necessarily as a directory.
59 This includes the case where pathname is a symbolic link,
62 if (stat(dirname
, &st
) != 0)
65 log_error("Failed to stat \"%s\": %d (%s)", dirname
, errno
, strerror(errno
));
68 else if (!S_ISDIR(st
.st_mode
))
71 log_error("\"%s\" exists but is not directory.", dirname
);
79 static bool rmkdir(char * buffer
, int mode
)
101 /* skip extra '/' chars */
113 if (!safe_mkdir(buffer
, mode
, &err
))
121 log_info("Directory \"%s\" created", buffer
);
126 log_error("Failed to create \"%s\" directory: %d (%s)", buffer
, errno
, strerror(errno
));
141 bool ensure_dir_exist(const char * dirname
, int mode
)
148 if (!safe_mkdir(dirname
, mode
, &err
))
153 if (err
== 0 || err
== EEXIST
)
160 log_error("Failed to create \"%s\" directory: %d (%s)", dirname
, errno
, strerror(errno
));
164 /* A directory component in dirname does not exist or is a dangling symbolic link */
166 len
= strlen(dirname
);
168 buffer
= malloc(len
+ 1);
171 log_error("malloc(%zu) failed.", len
);
175 memcpy(buffer
, dirname
, len
);
178 ret
= rmkdir(buffer
, mode
);
185 bool ensure_dir_exist_varg(int mode
, ...)
196 while ((str
= va_arg(ap
, const char *)) != NULL
)
204 buffer
= malloc(len
);
207 log_error("malloc(%zu) failed.", len
);
213 while ((str
= va_arg(ap
, const char *)) != NULL
)
216 memcpy(buffer
, str
, len
);
223 ret
= rmkdir(buffer
, mode
);