void test_mkdirs(const std::string basepath) {
  // Test creating a directory hierarchy.
  EXPECT_TRUE(mkdirs(basepath));
  // Test finding an existing directory hierarchy.
  EXPECT_TRUE(mkdirs(basepath));
  const std::string filepath = basepath + "/file";
  // Verify that the hierarchy was created by trying to create a file in it.
  EXPECT_NE(-1, adb_creat(filepath.c_str(), 0600));
  // If a file exists where we want a directory, the operation should fail.
  EXPECT_FALSE(mkdirs(filepath));
}
Beispiel #2
0
/**
 * Function to prepare the attempt directories for the task JVM.
 * It creates the task work and log directories.
 */
static int create_attempt_directories(const char* user, const char *job_id, 
					const char *task_id) {
  // create dirs as 0750
  const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
  if (job_id == NULL || task_id == NULL || user == NULL) {
    fprintf(LOGFILE, 
            "Either task_id is null or the user passed is null.\n");
    return -1;
  }
  int result = 0;

  char **local_dir = get_values(TT_SYS_DIR_KEY);

  if (local_dir == NULL) {
    fprintf(LOGFILE, "%s is not configured.\n", TT_SYS_DIR_KEY);
    return -1;
  }

  char **local_dir_ptr;
  for(local_dir_ptr = local_dir; *local_dir_ptr != NULL; ++local_dir_ptr) {
    char *task_dir = get_attempt_work_directory(*local_dir_ptr, user, job_id, 
                                                task_id);
    if (task_dir == NULL) {
      free_values(local_dir);
      return -1;
    }
    if (mkdirs(task_dir, perms) != 0) {
      // continue on to create other task directories
      free(task_dir);
    } else {
      free(task_dir);
    }
  }
  free_values(local_dir);

  // also make the directory for the task logs
  char *job_task_name = malloc(strlen(job_id) + strlen(task_id) + 2);
  if (job_task_name == NULL) {
    fprintf(LOGFILE, "Malloc of job task name failed\n");
    result = -1;
  } else {
    sprintf(job_task_name, "%s/%s", job_id, task_id);
    char *log_dir = get_job_log_directory(job_task_name);
    free(job_task_name);
    if (log_dir == NULL) {
      result = -1;
    } else if (mkdirs(log_dir, perms) != 0) {
      result = -1;
    }
    free(log_dir);
  }
  return result;
}
static int _tpm_extern_init()
{
  info("_tpm_extern_init()");
  mkdirs(tpm_storage_file);
  mkdirs(tpm_log_file);
  debug("openening random device %s", tpm_random_device);
  rand_fh = open(tpm_random_device, O_RDONLY);
  if (rand_fh < 0) {
    error("open(%s) failed: %s", tpm_random_device, strerror(errno));
    return -1;
  }
  return 0;
}
Beispiel #4
0
// initialize state directories
int wish_init_dirs( struct wish_state* state ) {
   // make sure tmp_dir is defined and exists
   if( !state->conf.tmp_dir ) {
      // try $HOME, and fall back to /tmp/wish-PID if there isn't a $HOME value
      char* home_dir = strdup( getenv("HOME") );
      if( !home_dir ) {
         home_dir = (char*)calloc( strlen("/tmp/wish-XXXXXX"), 1 );
         sprintf(home_dir, "/tmp/wish-%d", getpid() );
      }
      state->conf.tmp_dir = fullpath( home_dir, ".wish/tmp/", NULL );
      errorf("wish_init_dirs: no temporary directory specified; using %s\n", state->conf.tmp_dir);
      free( home_dir );
   }
   
   struct stat sb;
   int rc = stat( state->conf.tmp_dir, &sb );
   if( rc == 0 && !S_ISDIR( sb.st_mode ) ) {
      errorf("wish_init_dirs: %s is not a directory!\n", state->conf.tmp_dir );
      return -ENOTDIR;
   }
   if( rc != 0 ) {
      // need to create?
      rc = mkdirs( state->conf.tmp_dir );
      if( rc != 0 ) {
         errorf("wish_init_dirs: could not create %s!  errno = %d\n", state->conf.tmp_dir, -errno );
         return -errno;
      }
   }
   
   // make sure files_root exists
   if( !state->conf.files_root ) {
      errorf("wish_init_dirs: no file root defined!  Please specify a value for %s in your configuration!\n", FILES_ROOT_KEY );
      return -ENOENT;
   }
   
   rc = stat( state->conf.files_root, &sb );
   if( rc == 0 && !S_ISDIR( sb.st_mode ) ) {
      errorf("wish_init_dirs: %s is not a directory!\n", state->conf.files_root );
      return -ENOTDIR;
   }
   if( rc != 0 ) {
      // need to create?
      rc = mkdirs( state->conf.tmp_dir );
      if( rc != 0 ) {
         errorf("wish_init_dirs: could not create %s!  errno = %d\n", state->conf.files_root, -errno );
         return -errno;
      }
   }
   
   return 0;
}
Beispiel #5
0
int main(int argc, char *argv[]) {
	removefile_state_t state = NULL;
	removefile_callback_t callback = NULL;
	pthread_t thread = NULL;
	int err = 0;

	mkdirs();
	start_timer("removefile(NULL)");
	assert(removefile("/tmp/removefile-test", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
	stop_timer();


	mkdirs();
	assert((state = removefile_state_alloc()) != NULL);
	assert(pthread_create(&thread, NULL, threadproc, state) == 0);
	start_timer("removefile(state) with cancel");
        assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CALLBACK, removefile_error_callback) == 0);
        assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CONTEXT, (void*)4567) == 0);
	assert(removefile("/tmp/removefile-test", state, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == -1 && errno == ECANCELED);
	stop_timer();

        start_timer("removefile(NULL)");
        assert(removefile("/tmp/removefile-test", NULL, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
        stop_timer();

	mkdirs();
	assert(removefile_state_set(state, 1234567, (void*)1234567) == -1 && errno == EINVAL);

	assert(removefile_state_set(state, REMOVEFILE_STATE_CONFIRM_CALLBACK, removefile_confirm_callback) == 0);
	assert(removefile_state_get(state, REMOVEFILE_STATE_CONFIRM_CALLBACK, &callback) == 0);
	assert(callback == removefile_confirm_callback);
	assert(removefile_state_set(state, REMOVEFILE_STATE_CONFIRM_CONTEXT, (void*)1234) == 0);

	assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CALLBACK, removefile_error_callback) == 0);
	assert(removefile_state_get(state, REMOVEFILE_STATE_ERROR_CALLBACK, &callback) == 0);
	assert(callback == removefile_error_callback);
	assert(removefile_state_set(state, REMOVEFILE_STATE_ERROR_CONTEXT, (void*)4567) == 0);

	assert(removefile_state_set(state, REMOVEFILE_STATE_STATUS_CALLBACK, removefile_status_callback) == 0);
	assert(removefile_state_get(state, REMOVEFILE_STATE_STATUS_CALLBACK, &callback) == 0);
	assert(callback == removefile_status_callback);
	assert(removefile_state_set(state, REMOVEFILE_STATE_STATUS_CONTEXT, (void*)5678) == 0);

	start_timer("removefile(state)");
	assert(removefile("/tmp/removefile-test", state, REMOVEFILE_SECURE_1_PASS | REMOVEFILE_RECURSIVE) == 0);
	stop_timer();

	return 0;
}
Beispiel #6
0
ReadaheadShared *shared_get(void) {
        int _cleanup_close_ fd = -1;
        ReadaheadShared *m = NULL;

        mkdirs();

        fd = open("/run/systemd/readahead/shared", O_CREAT|O_RDWR|O_CLOEXEC, 0644);
        if (fd < 0) {
                log_error("Failed to create shared memory segment: %m");
                return NULL;
        }

        if (ftruncate(fd, sizeof(ReadaheadShared)) < 0) {
                log_error("Failed to truncate shared memory segment: %m");
                return NULL;
        }

        m = mmap(NULL, sizeof(ReadaheadShared), PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
        if (m == MAP_FAILED) {
                log_error("Failed to mmap shared memory segment: %m");
                return NULL;
        }

        return m;
}
Beispiel #7
0
/* add an entry to the tree */
int
virtdir_add(virtdir_t *tp, const char *name, size_t size, uint8_t type, const char *tgt, size_t tgtlen)
{
	struct stat	st;
	char		path[MAXPATHLEN];
	int		pathlen;

	if (tp->v == NULL) {
		(void) stat(".", &st);
		virtdir_init(tp, NULL, &st, &st, &st);
	}
	pathlen = normalise(name, size, path, sizeof(path));
	if (virtdir_find(tp, path, pathlen) != NULL) {
		/* attempt to add a duplicate directory entry */
		return 0;
	}
	ALLOC(virt_dirent_t, tp->v, tp->size, tp->c, 10, 10, "virtdir_add",
			return 0);
	tp->v[tp->c].namelen = pathlen;
	if ((tp->v[tp->c].name = strnsave(path, pathlen)) == NULL) {
		return 0;
	}
	tp->v[tp->c].d_name = strrchr(tp->v[tp->c].name, '/') + 1;
	tp->v[tp->c].type = type;
	tp->v[tp->c].ino = (ino_t) random() & 0xfffff;
	if (tgt != NULL) {
		tp->v[tp->c].tgtlen = tgtlen;
		tp->v[tp->c].tgt = strnsave(tgt, tgtlen);
	}
	tp->c += 1;
	qsort(tp->v, tp->c, sizeof(tp->v[0]), compare);
	mkdirs(tp, path, pathlen);
	return 1;
}
Beispiel #8
0
static struct packing *
pkg_create_archive(const char *outdir, struct pkg *pkg, pkg_formats format,
    unsigned required_flags)
{
	char		*pkg_path = NULL;
	struct packing	*pkg_archive = NULL;
	const char	*pkgname, *pkgversion;

	/*
	 * Ensure that we have all the information we need
	 */
	if (pkg->type != PKG_OLD_FILE)
		assert((pkg->flags & required_flags) == required_flags);

	if (mkdirs(outdir) != EPKG_OK)
		return NULL;

	pkg_get(pkg, PKG_NAME, &pkgname, PKG_VERSION, &pkgversion);
	if (asprintf(&pkg_path, "%s/%s-%s", outdir, pkgname, pkgversion) == -1) {
		pkg_emit_errno("asprintf", "");
		return (NULL);
	}

	if (packing_init(&pkg_archive, pkg_path, format) != EPKG_OK)
		pkg_archive = NULL;

	free(pkg_path);

	return pkg_archive;
}
Beispiel #9
0
/* this is more-or-less equivalent to mkdir -p */
static int
mkdirs(char *path, mode_t mode)
{
    char *      cp;
    int         rv;
    struct stat sb;
    
    if (!path || !path[0]) 
	fail("Null pointer or empty string passed to mkdirs()");
    while (*path == '/' && path[1] == '/')
	path++;
    for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
    if (cp && cp != path) {
	*cp = '\0';
	if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
	    mkdirs(path, mode) < 0) {
	    return -1;
	}
	*cp = '/';
    }
    rv = mkdir(path, mode);
    if (rv) {
	if (errno != EEXIST)
	    fail("mkdirs cannot make %s", path);
	fprintf(stderr, "directory creation race: %s\n", path);
	if (!stat(path, &sb) && S_ISDIR(sb.st_mode)) 
	    rv = 0;
    }
    return rv;
}
Beispiel #10
0
static bool mkdirs(char *path)
{
    bool retval = false;
    char *slash;

    slash = strrchr(path, '/');
    if (slash == NULL)
        retval = true;
    else
    {
        slash[0] = '\0';
        while(true)
        {
            /* make the new directory */
            if (mkdir(path,
                      S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
                retval = true;
            /* try to create the parent path if that was the problem */
            else if (errno == ENOENT && mkdirs(path))
                continue;

            break;
        }
        slash[0] = '/';
    }

    return retval;
}
void test_delete_user() {
    printf("\nTesting delete_user\n");
    char* local_dirs = get_value("mapred.local.dir");
    char* job_dir = get_job_directory(TEST_ROOT "/local-1", username, "job_3");
    if (mkdirs(job_dir, 0700) != 0) {
        exit(1);
    }
    char buffer[100000];
    sprintf(buffer, "%s/local-1/taskTracker/%s", TEST_ROOT, username);
    if (access(buffer, R_OK) != 0) {
        printf("FAIL: directory missing before test\n");
        exit(1);
    }
    if (delete_as_user(username, local_dirs, "") != 0) {
        exit(1);
    }
    if (access(buffer, R_OK) == 0) {
        printf("FAIL: directory not deleted\n");
        exit(1);
    }
    if (access(TEST_ROOT "/local-1", R_OK) != 0) {
        printf("FAIL: local-1 directory does not exist\n");
        exit(1);
    }
    free(job_dir);
    free(local_dirs);
}
Beispiel #12
0
static int
mkdirs(char *path, mode_t mode)
{
    char *cp;
    struct stat sb;
    int res;
    int l;

    /* strip trailing "/." */
    l = strlen(path);
    if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
        path[l - 2] = 0;

    while (*path == '/' && path[1] == '/')
	path++;
    for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
    if (cp && cp != path) {
	*cp = '\0';
	if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
	    mkdirs(path, mode) < 0) {
	    return -1;
	}
	*cp = '/';
    }
    
    res = mkdir(path, mode);
    if ((res != 0) && (errno == EEXIST))
      return 0;
    else
      return res;
}
Beispiel #13
0
//  This function recursively cleans a specific directory path
//  If flag = 1, it will not protect PROTECTed files
static int move_contents(string path, int flag) {
   mixed *dir;
   string what;
   int loop;
 
   //	Get directory contents array
   dir = get_dir( path );
   if(!dir || !sizeof(dir))  return -1;
 
   //	Loop through and delete contents of dir array
   for(loop=0; loop<sizeof(dir); loop++) {
   what = path + dir[loop];

   //	If selection is a directory ... recursively clean and remove it
   if(file_size( what ) == -2) {
  what = replace_string( what, "//", "/" );
   mkdirs( PURGED + what + "/" );
   move_contents(what + "/", 1);
   rmdir(what);  
   }
 
   //	If file isn't protected ... remove it
  if( flag ) {
  what = replace_string( what, "//", "/" );
     rename( what, PURGED + what );
	rm(what);
   }
  }
 
return 1; }
static int
mkdirs(char *path, mode_t mode)
{
    char *cp;
    struct stat sb;
    int res;
    
    while (*path == '/' && path[1] == '/')
	path++;
    for (cp = strrchr(path, '/'); cp && cp != path && cp[-1] == '/'; cp--)
	;
    if (cp && cp != path) {
	*cp = '\0';
	if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
	    mkdirs(path, mode) < 0) {
	    return -1;
	}
	*cp = '/';
    }
    res = mkdir(path, mode);
    if ((res != 0) && (errno == EEXIST))
	return 0;
     else
	return res;
}
void test_multi_hashvalue(uint32_t limit)
{
   MEM_POOL* mem_pool = mem_pool_init(MB_SIZE);
   int32_t ret;
   struct hash_index_config config;
   config.row_limit = limit;
   strcpy(config.work_space,"/tmp/hash_compress_test");
   system("rm -rf /tmp/hash_compress_test");
   mkdirs(config.work_space);
   init_profile(1000,mem_pool);
   
   struct hash_index_manager* hash_index = hash_index_init(&config,mem_pool);


   uint32_t i;
   struct low_data_struct* data = (struct low_data_struct*)mem_pool_malloc(mem_pool,sizeof(struct low_data_struct));
   get_low_data(data,mem_pool);
   
   for(i=0; i<config.row_limit;i++)
   {   sprintf((char*)data->data,"%d",i);
	   ret = hash_index_insert(hash_index,data,i);
	   ASSERT_EQ(0,ret);
   }

   verify_muti_hashvalue(hash_index,mem_pool);
   hash_index_release(hash_index);
}
void test_delete_user() {
  printf("\nTesting delete_user\n");
  char* job_dir = get_job_directory(TEST_ROOT "/local-1", username, "job_3");
  if (mkdirs(job_dir, 0700) != 0) {
    exit(1);
  }
  char buffer[100000];
  sprintf(buffer, "%s/local-1/usercache/%s", TEST_ROOT, username);
  if (access(buffer, R_OK) != 0) {
    printf("FAIL: directory missing before test\n");
    exit(1);
  }
  if (delete_as_user(username, buffer, NULL) != 0) {
    exit(1);
  }
  if (access(buffer, R_OK) == 0) {
    printf("FAIL: directory not deleted\n");
    exit(1);
  }
  if (access(TEST_ROOT "/local-1", R_OK) != 0) {
    printf("FAIL: local-1 directory does not exist\n");
    exit(1);
  }
  free(job_dir);
}
Beispiel #17
0
static struct packing *
pkg_create_archive(const char *outdir, struct pkg *pkg, pkg_formats format,
    unsigned required_flags)
{
	char		*pkg_path = NULL;
	struct packing	*pkg_archive = NULL;

	/*
	 * Ensure that we have all the information we need
	 */
	if (pkg->type != PKG_OLD_FILE)
		assert((pkg->flags & required_flags) == required_flags);

	if (mkdirs(outdir) != EPKG_OK)
		return NULL;

	if (pkg_asprintf(&pkg_path, "%S/%n-%v", outdir, pkg, pkg) == -1) {
		pkg_emit_errno("pkg_asprintf", "");
		return (NULL);
	}

	if (packing_init(&pkg_archive, pkg_path, format, false) != EPKG_OK)
		pkg_archive = NULL;

	free(pkg_path);

	return pkg_archive;
}
Beispiel #18
0
/* Return opened file descriptor */
static int
repo_fetch_remote_tmp(struct pkg_repo *repo, const char *filename, const char *extension, time_t *t, int *rc)
{
	char url[MAXPATHLEN];
	char tmp[MAXPATHLEN];
	int fd;
	mode_t mask;
	const char *tmpdir;

	snprintf(url, MAXPATHLEN, "%s/%s.%s", pkg_repo_url(repo), filename, extension);

	tmpdir = getenv("TMPDIR");
	if (tmpdir == NULL)
		tmpdir = "/tmp";
	mkdirs(tmpdir);
	snprintf(tmp, MAXPATHLEN, "%s/%s.%s.XXXXXX", tmpdir, filename, extension);

	mask = umask(022);
	fd = mkstemp(tmp);
	umask(mask);
	if (fd == -1) {
		pkg_emit_error("Could not create temporary file %s, "
		    "aborting update.\n", tmp);
		*rc = EPKG_FATAL;
		return (-1);
	}
	(void)unlink(tmp);

	if ((*rc = pkg_fetch_file_to_fd(repo, url, fd, t)) != EPKG_OK) {
		close(fd);
		fd = -1;
	}

	return (fd);
}
Beispiel #19
0
int EL_Log::create_new_file()
{
	char filename[256];
	if (access(dir, F_OK) < 0) {
		if (mkdirs(dir) < 0)
			return -1;
	}

	if (current_num == maintain) {
		current_num = 0;
	}

	if (log_fp) {
		fclose(log_fp);
	}

	++current_num;
	sprintf(filename ,"%s/%s_%02ld.log", dir, name_base, current_num);
	log_fp = fopen(filename, "w");
	if (log_fp == NULL)
		return -1;

	current_size = 0;

	return 0;
}		// -----  end of function create_new_file  -----
static int mkdirs(const char *path)
{
  char *copy = strdup(path);
  char *p = strchr(copy + 1, '/');
  while (p != NULL) {
    *p = '\0';
#if defined(_WIN32) || defined(_WIN64)
    if ((mkdir(copy) == -1) && (errno != EEXIST)) {
#else
    if ((mkdir(copy, 0755) == -1) && (errno != EEXIST)) {
#endif
      free(copy);
      return errno;
    }
    *p = '/';
    p = strchr(p + 1, '/');
  }
  free(copy);
  return 0;
}

#if defined(_WIN32) || defined(_WIN64)

#include <windows.h>
#include <wincrypt.h>

static HCRYPTPROV rand_ch;

static int _tpm_extern_init()
{
  info("_tpm_extern_init()");
  mkdirs(tpm_storage_file);
  mkdirs(tpm_log_file);
  debug("initializing crypto context for RNG");
  BOOL res = CryptAcquireContext(&rand_ch, NULL, NULL,
                                 PROV_RSA_FULL, CRYPT_SILENT);
  if (!res) {
    /* try it again with CRYPT_NEWKEYSET enabled */
    res = CryptAcquireContext(&rand_ch, NULL, NULL,
                              PROV_RSA_FULL, CRYPT_SILENT | CRYPT_NEWKEYSET);
  }
  if (!res) {
    error("CryptAcquireContext() failed: %d", GetLastError());
    return -1;
  }
  return 0;
}
void test_delete_user() {
  printf("\nTesting delete_user\n");
  char* app_dir = get_app_directory(TEST_ROOT "/local-1", yarn_username, "app_3");
  if (mkdirs(app_dir, 0700) != 0) {
    exit(1);
  }

  char buffer[100000];
  sprintf(buffer, "%s/test.cfg", app_dir);
  if (write_config_file(buffer, 1) != 0) {
    exit(1);
  }

  char * dirs[] = {buffer, 0};
  int ret = delete_as_user(yarn_username, "file1" , dirs);
  if (ret == 0) {
    printf("FAIL: if baseDir is a file, delete_as_user should fail if a subdir is also passed\n");
    exit(1);
  }

  // Pass a file to delete_as_user in the baseDirs parameter. The file should
  // be deleted.
  ret = delete_as_user(yarn_username, "" , dirs);
  if (ret != 0) {
    printf("FAIL: delete_as_user could not delete baseDir when baseDir is a file: return code is %d\n", ret);
    exit(1);
  }

  sprintf(buffer, "%s", app_dir);
  char missing_dir[20];
  strcpy(missing_dir, "/some/missing/dir");
  char * dirs_with_missing[] = {missing_dir, buffer, 0};
  ret = delete_as_user(yarn_username, "" , dirs_with_missing);
  printf("%d" , ret);
  if (access(buffer, R_OK) == 0) {
    printf("FAIL: directory not deleted\n");
    exit(1);
  }

  sprintf(buffer, "%s/local-1/usercache/%s", TEST_ROOT, yarn_username);
  if (access(buffer, R_OK) != 0) {
    printf("FAIL: directory missing before test\n");
    exit(1);
  }
  if (delete_as_user(yarn_username, buffer, NULL) != 0) {
    exit(1);
  }
  if (access(buffer, R_OK) == 0) {
    printf("FAIL: directory not deleted\n");
    exit(1);
  }
  if (access(TEST_ROOT "/local-1", R_OK) != 0) {
    printf("FAIL: local-1 directory does not exist\n");
    exit(1);
  }
  free(app_dir);
}
Beispiel #22
0
/*
 *  Open a .pkt file on the queue, create a fresh one if needed.
 *  If CFG.maxpktsize is set then it will add the .pkt to the
 *  ARCmail archive when possible.
 */
FILE *OpenPkt(fidoaddr Orig, fidoaddr Dest, char *Extension)
{
    char	*Queue;
    static FILE	*qp;

    Queue = calloc(PATH_MAX, sizeof(char));

    snprintf(Queue, PATH_MAX, "%s/%d.%d.%d.%d/mailpkt.%s", CFG.out_queue, Dest.zone, Dest.net, Dest.node, Dest.point, Extension);
    mkdirs(Queue, 0750);
    
    if (file_exist(Queue, R_OK))
	qp = CreatePkt(Queue, Orig, Dest, Extension);
    else {
	if ((qp = fopen(Queue, "a")) == NULL) {
	    WriteError("$Can't reopen Queue %s", Queue);
	    free(Queue);
	    return NULL;
	}

	if (CFG.maxpktsize && (ftell(qp) >= (CFG.maxpktsize * 1024)) && (strcmp(Extension, "qqq") == 0)) {
	    /*
	     * It's a pkt that's meant to be send archived and it's
	     * bigger then maxpktsize. Try to add this pkt to the
	     * outbound archive for this node.
	     */
	    fsync(fileno(qp));
	    fclose(qp);
	    if (PrepARC(Queue, Dest) == TRUE) {
		/*
		 * If the pack succeeded create a fresh packet.
		 */
		qp = CreatePkt(Queue, Orig, Dest, Extension);
	    } else {
		/*
		 * If the pack failed the existing queue is
		 * reopened and we continue adding to that
		 * existing packet.
		 */
		Syslog('s', "PrepARC failed");
		qp = fopen(Queue, "a");
	    }

	    /*
	     * Go back to the original inbound directory.
	     */
	    if (do_unprot) 
		chdir(CFG.inbound);
	    else
		chdir(CFG.pinbound);
	}
    }

    free(Queue);
    do_flush = TRUE;
    return qp;
}
Beispiel #23
0
static int backup(int argc, char** argv) {
    char buf[4096];
    char default_name[32];
    const char* filename = strcpy(default_name, "./backup.ab");
    int fd, outFd;
    int i, j;

    /* find, extract, and use any -f argument */
    for (i = 1; i < argc; i++) {
        if (!strcmp("-f", argv[i])) {
            if (i == argc-1) {
                fprintf(stderr, "adb: -f passed with no filename\n");
                return usage();
            }
            filename = argv[i+1];
            for (j = i+2; j <= argc; ) {
                argv[i++] = argv[j++];
            }
            argc -= 2;
            argv[argc] = NULL;
        }
    }

    /* bare "adb backup" or "adb backup -f filename" are not valid invocations */
    if (argc < 2) return usage();

    adb_unlink(filename);
    mkdirs((char *)filename);
    outFd = adb_creat(filename, 0640);
    if (outFd < 0) {
        fprintf(stderr, "adb: unable to open file %s\n", filename);
        return -1;
    }

    snprintf(buf, sizeof(buf), "backup");
    for (argc--, argv++; argc; argc--, argv++) {
        strncat(buf, ":", sizeof(buf) - strlen(buf) - 1);
        strncat(buf, argv[0], sizeof(buf) - strlen(buf) - 1);
    }

    D("backup. filename=%s buf=%s\n", filename, buf);
    fd = adb_connect(buf);
    if (fd < 0) {
        fprintf(stderr, "adb: unable to connect for backup\n");
        adb_close(outFd);
        return -1;
    }

    printf("Now unlock your device and confirm the backup operation.\n");
    copy_to_file(fd, outFd);

    adb_close(fd);
    adb_close(outFd);
    return 0;
}
// Given a relative or absolute filepath, create the directory hierarchy
// as needed. Returns true if the hierarchy is/was setup.
bool mkdirs(const std::string& path) {
  // TODO: all the callers do unlink && mkdirs && adb_creat ---
  // that's probably the operation we should expose.

  // Implementation Notes:
  //
  // Pros:
  // - Uses dirname, so does not need to deal with OS_PATH_SEPARATOR.
  // - On Windows, uses mingw dirname which accepts '/' and '\\', drive letters
  //   (C:\foo), UNC paths (\\server\share\dir\dir\file), and Unicode (when
  //   combined with our adb_mkdir() which takes UTF-8).
  // - Is optimistic wrt thinking that a deep directory hierarchy will exist.
  //   So it does as few stat()s as possible before doing mkdir()s.
  // Cons:
  // - Recursive, so it uses stack space relative to number of directory
  //   components.

  // If path points to a symlink to a directory, that's fine.
  struct stat sb;
  if (stat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode)) {
    return true;
  }

  const std::string parent(adb_dirname(path));

  // If dirname returned the same path as what we passed in, don't go recursive.
  // This can happen on Windows when walking up the directory hierarchy and not
  // finding anything that already exists (unlike POSIX that will eventually
  // find . or /).
  if (parent == path) {
    errno = ENOENT;
    return false;
  }

  // Recursively make parent directories of 'path'.
  if (!mkdirs(parent)) {
    return false;
  }

  // Now that the parent directory hierarchy of 'path' has been ensured,
  // create path itself.
  if (adb_mkdir(path, 0775) == -1) {
    const int saved_errno = errno;
    // If someone else created the directory, that is ok.
    if (directory_exists(path)) {
      return true;
    }
    // There might be a pre-existing file at 'path', or there might have been some other error.
    errno = saved_errno;
    return false;
  }

  return true;
}
Beispiel #25
0
void mkdirs(int oklen, char* path) {

    if (strlen(path) <= oklen)  return;
    char dir[PATH_MAX];

    strcpy(dir, path);
    char* slash = strrchr(dir, '/');
    if (slash == 0)  return;
    *slash = 0;
    mkdirs(oklen, dir);
    MKDIR(dir);
}
struct dyhash_index_manager* init(struct dyhash_index_config* config,char * dir_path){
	char cmd[1024];
	memset(cmd, 0, sizeof(cmd));
	sprintf(cmd, "rm -rf %s", dir_path);
	system(cmd);
	
	mkdirs(dir_path);

	MEM_POOL* mem_pool = mem_pool_init(MB_SIZE);
    init_profile(1000,mem_pool);
	return dyhash_index_init(config,mem_pool);
}
Beispiel #27
0
/*
 * Flush outbound queue to real outbound.
 */
void flush_queue(void)
{
    char	    *temp;
    struct dirent   *de;
    DIR		    *dp;
    
    Syslog('+', "Flushing outbound queue");
    if (enoughspace(CFG.freespace) == 0) {
	Syslog('+', "Low diskspace, not flushing outbound queue");
	return;
    }

    IsDoing("Flush queue");
    if (!do_quiet) {
	mbse_colour(LIGHTBLUE, BLACK);
	printf("Flushing outbound queue\n");
	mbse_colour(CYAN, BLACK);
    }
    
    temp = calloc(PATH_MAX, sizeof(char));
    snprintf(temp, PATH_MAX, "%s/foobar", CFG.out_queue);
    mkdirs(temp, 0750);

    if ((dp = opendir(CFG.out_queue)) == 0) {
	WriteError("$Can't open %s", CFG.out_queue);
	free(temp);
	return;
    }
    
    /*
     * The outbound queue contains subdirectories which actuallly hold the
     * queue outbound files. Process each found subdirectory.
     */
    while ((de = readdir(dp))) {
	if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
	    snprintf(temp, PATH_MAX, "%s/%s", CFG.out_queue, de->d_name);
	    Syslog('p', "Queue directory %s", temp);
	    flush_dir(de->d_name);
	    if (chdir(CFG.out_queue))
		WriteError("$Can't chdir to %s", CFG.out_queue);
	    if (rmdir(temp))
		WriteError("$Can't rmdir %s", temp);
	}
    }
    closedir(dp);
    free(temp);

    if (!do_quiet) {
	printf("\r                                              \r");
	fflush(stdout);
    }
}
Beispiel #28
0
/*
 * if any directories leading up to path don't exist, create them.
 * modifies but restores path.
 */
static int
mkpdirs(char *path)
{
	int rv = 0;
	char *sl = strrchr(path, '/');
print("%s\n", path);
	if (sl != nil) {
		*sl = '\0';
		rv = mkdirs(path);
		*sl = '/';
	}
	return rv;
}
Beispiel #29
0
static void 
mkdirs(char* path) {

  if (strlen(path) <= 0)  return;
  char dir[MAX_PATH];

  strcpy(dir, path);
  char* slash = strrchr(dir, '/');
  if (slash == 0)  return;
  *slash = 0;
  mkdirs(dir);
  mkdir(dir);
}
int main(void) {
  atexit(cleanup);

  system("rm -fr DIR");

  mkdirs("DIR/a/b");

  struct stat buf;
  xstat("DIR/a/b", &buf);
  assert(S_ISDIR(buf.st_mode));

  exit(EXIT_SUCCESS);
}