Miscellaneous helpers


Detailed Description

The misc family of functions provide wrappers not fitting in any other module and not being worth an own category for each of them.

The isdir(), isfile() and islink() functions wrap the stat(2) system call and checks if the path in the string pointed to by path is a directory, regular file or link, respectively.

The mkdirp() function creates any missing parent directories of the path in the string pointed to by path, before creating the directory itself. The mkdirnamep() function additionally calls dirname(3) on the path string before calling mkdirp().

The path_concat() function concatenates the strings pointed to by dirname and basename and checks the latter using str_path_isdot().

The runlink() function removes all files and directories in the path pointed to by the string path.


Functions

int isdir (const char *path)
 check if given path is a directory
int isfile (const char *path)
 check if given path is a regular file
int islink (const char *path)
 check if given path is a symbolic link
int mkdirnamep (const char *path, mode_t mode)
 recursive mkdir(2) with dirname(3)
int mkdirp (const char *path, mode_t mode)
 recursive mkdir(2)
int runlink (const char *path)
 recursive unlink(2) and rmdir(2)


Function Documentation

int isdir ( const char *  path  ) 

check if given path is a directory

Parameters:
[in] path path to check
Returns:
1 on success, 0 otherwise
See also:
stat(2)

Definition at line 22 of file isdir.c.

00023 {
00024         struct stat stats;
00025         return stat(path, &stats) == 0 && S_ISDIR(stats.st_mode);
00026 }

int isfile ( const char *  path  ) 

check if given path is a regular file

Parameters:
[in] path path to check
Returns:
1 on success, 0 otherwise
See also:
stat(2)

Definition at line 22 of file isfile.c.

00023 {
00024         struct stat stats;
00025         return stat(path, &stats) == 0 && S_ISREG(stats.st_mode);
00026 }

int islink ( const char *  path  ) 

check if given path is a symbolic link

Parameters:
[in] path path to check
Returns:
1 on success, 0 otherwise
See also:
stat(2)

Definition at line 22 of file islink.c.

00023 {
00024         struct stat stats;
00025         return stat(path, &stats) == 0 && S_ISLNK(stats.st_mode);
00026 }

int mkdirnamep ( const char *  path,
mode_t  mode 
)

recursive mkdir(2) with dirname(3)

Parameters:
[in] path path to create
[in] mode file permissions
Returns:
0 on success, -1 on error with errno set
See also:
mkdir(2)

dirname(3)

Definition at line 25 of file mkdirnamep.c.

References mkdirp(), str_dup(), and str_isempty.

00026 {
00027         char *buf, *dname;
00028         int rc, errno_orig;
00029         
00030         if (str_isempty(path))
00031                 return errno = EINVAL, -1;
00032         
00033         buf   = str_dup(path);
00034         dname = dirname(buf);
00035         
00036         rc = mkdirp(dname, mode);
00037         
00038         errno_orig = errno;
00039         free(buf);
00040         errno = errno_orig;
00041         
00042         return rc;
00043 }

int mkdirp ( const char *  path,
mode_t  mode 
)

recursive mkdir(2)

Parameters:
[in] path path to create
[in] mode file permissions
Returns:
0 on success, -1 on error with errno set
See also:
mkdir(2)

Definition at line 25 of file mkdirp.c.

References str_dup(), and str_isempty.

Referenced by chroot_mkdirp(), and mkdirnamep().

00026 {
00027         int errno_orig, rc = 0;
00028         char *p, *buf, c;
00029         struct stat sb;
00030         
00031         if (str_isempty(path))
00032                 return errno = EINVAL, -1;
00033         
00034         buf = p = str_dup(path);
00035         
00036         do {
00037                 c = 0;
00038                 
00039                 while (*p) {
00040                         if (*p == '/') {
00041                                 do { ++p; } while (*p == '/');
00042                                 c  = *p;
00043                                 *p = '\0';
00044                                 break;
00045                         }
00046                         
00047                         else
00048                                 ++p;
00049                 }
00050                 
00051                 if (mkdir(buf, 0777) == -1) {
00052                         if (errno != EEXIST || stat(buf, &sb) == -1)
00053                                 goto err;
00054                         
00055                         if (!S_ISDIR(sb.st_mode)) {
00056                                 errno = ENOTDIR;
00057                                 goto err;
00058                         }
00059                         
00060                         if (!c)
00061                                 goto out;
00062                 }
00063                 
00064                 if (!c) {
00065                         if (chmod(buf, mode) == -1)
00066                                 goto err;
00067                         
00068                         goto out;
00069                 }
00070                 
00071                 *p = c;
00072         } while (1);
00073         
00074 err:
00075         rc = -1;
00076 out:
00077         errno_orig = errno;
00078         free(buf);
00079         errno = errno_orig;
00080         return rc;
00081 }

int runlink ( const char *  path  ) 

recursive unlink(2) and rmdir(2)

Parameters:
[in] path path to remove
Returns:
0 on success, -1 on error with errno set
See also:
unlink(2)

rmdir(2)

Definition at line 27 of file runlink.c.

References _lucid_asprintf(), and runlink().

Referenced by runlink().

00028 {
00029         struct stat sb;
00030         
00031         DIR *dp;
00032         struct dirent *d;
00033         
00034         int status = 0;
00035         char *p, *new_path;
00036         
00037         if (lstat(path, &sb) == -1) {
00038                 if (errno == ENOENT)
00039                         return 0;
00040                 else
00041                         return -1;
00042         }
00043         
00044         if (S_ISDIR(sb.st_mode)) {
00045                 if (!(dp = opendir(path)))
00046                         return -1;
00047                 
00048                 while ((d = readdir(dp))) {
00049                         p = d->d_name;
00050                         
00051                         if (p && p[0] == '.' && (!p[1] || (p[1] == '.' && !p[2])))
00052                                 continue;
00053                         
00054                         _lucid_asprintf(&new_path, "%s/%s", path, d->d_name);
00055                         
00056                         if (runlink(new_path) == -1)
00057                                 status = -1;
00058                         
00059                         free(new_path);
00060                 }
00061                 
00062                 if (closedir(dp) == -1)
00063                         return -1;
00064                 
00065                 if (rmdir(path) == -1)
00066                         return -1;
00067                 
00068                 return status;
00069         }
00070         
00071         if (unlink(path) == -1)
00072                 return -1;
00073         
00074         return 0;
00075 }


Generated on Sun Dec 3 17:46:16 2006 for lucid by  doxygen 1.5.1