Example #1
0
static bool makedir(JCR *jcr, char *path, mode_t mode, int *created)
{
   struct stat statp;

   if (mkdir(path, mode) != 0) {
      berrno be;
      *created = false;
      if (lstat(path, &statp) != 0) {
         Jmsg2(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"),
              path, be.bstrerror());
         return false;
      } else if (!S_ISDIR(statp.st_mode)) {
         Jmsg1(jcr, M_ERROR, 0, _("%s exists but is not a directory.\n"), path);
         return false;
      }
      return true;                 /* directory exists */
   }
   if (S_ISLNK(statp.st_mode)) {
      /*
       * Note, we created a directory, not a link, so if we find a
       *  link, there is a security problem here.
       */
      Jmsg1(jcr, M_FATAL, 0, _("Security problem!! We created directory %s, but it is a link.\n"),
         path);
      return false;
   }

   if (jcr->keep_path_list) {
      /* When replace=NEVER, we keep track of all directories newly created */
      path_list_add(jcr, strlen(path), path);
   }

   *created = true;
   return true;
}
Example #2
0
/*
 * Create a parent directory using the cephfs API.
 *
 * We don't use cephfs_mkdirs as we want to keep track which
 * directories got created by us.
 */
static inline bool cephfs_makedirs(plugin_ctx *p_ctx, const char *directory)
{
   char *bp;
   struct stat st;
   bool retval = false;
   POOL_MEM new_directory(PM_FNAME);

   pm_strcpy(new_directory, directory);

   /*
    * See if the parent exists.
    */
   bp = strrchr(new_directory.c_str(), '/');
   if (bp) {
      /*
       * See if we reached the root.
       */
      if (bp == new_directory.c_str()) {
         /*
          * Create the directory.
          */
         if (ceph_mkdir(p_ctx->cmount, directory, 0750) == 0) {
            if (!p_ctx->path_list) {
               p_ctx->path_list = path_list_init();
            }
            path_list_add(p_ctx->path_list, strlen(directory), directory);
            retval = true;
         }
      } else {
         *bp = '\0';

         if (ceph_lstat(p_ctx->cmount, new_directory.c_str(), &st) != 0) {
            switch (errno) {
            case ENOENT:
               /*
                * Make sure our parent exists.
                */
               retval = cephfs_makedirs(p_ctx, new_directory.c_str());
               if (!retval) {
                  return false;
               }

               /*
                * Create the directory.
                */
               if (ceph_mkdir(p_ctx->cmount, directory, 0750) == 0) {
                  if (!p_ctx->path_list) {
                     p_ctx->path_list = path_list_init();
                  }
                  path_list_add(p_ctx->path_list, strlen(directory), directory);
                  retval = true;
               }
               break;
            default:
               break;
            }
         } else {
            retval = true;
         }
      }
   }

   return retval;
}