예제 #1
0
/* Make the given directory including its parent directories as necessary.
 * Return TRUE on success, FALSE on error. */
static notmuch_bool_t
make_directory_and_parents (char *path, int mode)
{
    struct stat st;
    char *start;
    char *end;
    notmuch_bool_t ret;

    /* First check the common case: directory already exists. */
    if (stat (path, &st) == 0)
	return S_ISDIR (st.st_mode) ? TRUE : FALSE;

    for (start = path; *start != '\0'; start = end + 1) {
	/* start points to the first unprocessed character.
	 * Find the next slash from start onwards. */
	end = strchr (start, '/');

	/* If there are no more slashes then all the parent directories
	 * have been made.  Now attempt to make the whole path. */
	if (end == NULL)
	    return make_directory (path, mode);

	/* Make the path up to the next slash, unless the current
	 * directory component is actually empty. */
	if (end > start) {
	    *end = '\0';
	    ret = make_directory (path, mode);
	    *end = '/';
	    if (! ret)
		return FALSE;
	}
    }

    return TRUE;
}
예제 #2
0
/* Create the given maildir folder, i.e. dir and its subdirectories
 * 'cur', 'new', 'tmp'. */
static notmuch_bool_t
maildir_create_folder (void *ctx, const char *dir)
{
    const int mode = 0700;
    char *subdir;
    char *tail;

    /* Create 'cur' directory, including parent directories. */
    subdir = talloc_asprintf (ctx, "%s/cur", dir);
    if (! subdir) {
	fprintf (stderr, "Out of memory.\n");
	return FALSE;
    }
    if (! make_directory_and_parents (subdir, mode))
	return FALSE;

    tail = subdir + strlen (subdir) - 3;

    /* Create 'new' directory. */
    strcpy (tail, "new");
    if (! make_directory (subdir, mode))
	return FALSE;

    /* Create 'tmp' directory. */
    strcpy (tail, "tmp");
    if (! make_directory (subdir, mode))
	return FALSE;

    talloc_free (subdir);
    return TRUE;
}
예제 #3
0
_nc_set_writedir(char *dir)
/* set the write directory for compiled entries */
{
    const char *destination;
    char actual[PATH_MAX];

    if (dir == 0
	&& use_terminfo_vars())
	dir = getenv("TERMINFO");

    if (dir != 0)
	(void) _nc_tic_dir(dir);

    destination = _nc_tic_dir(0);
    if (make_directory(destination) < 0) {
	char *home = _nc_home_terminfo();

	if (home != 0) {
	    destination = home;
	    if (make_directory(destination) < 0)
		_nc_err_abort("%s: permission denied (errno %d)",
			      destination, errno);
	}
    }

    /*
     * Note: because of this code, this logic should be exercised
     * *once only* per run.
     */
    if (chdir(_nc_tic_dir(destination)) < 0
	|| getcwd(actual, sizeof(actual)) == 0)
	_nc_err_abort("%s: not a directory", destination);
    _nc_keep_tic_dir(strdup(actual));
}
예제 #4
0
파일: Cassys.cpp 프로젝트: adri87/Q-A
int initialize_working_directory(const char *text,int must_create_directory){
	char path[FILENAME_MAX];
	get_path(text,path);

	char canonical_name[FILENAME_MAX];
	remove_path_and_extension(text, canonical_name);

	char extension[FILENAME_MAX];
	get_extension(text,extension);

	char working_directory[FILENAME_MAX];
	sprintf(working_directory, "%s%s%s%c",path, canonical_name, CASSYS_DIRECTORY_EXTENSION, PATH_SEPARATOR_CHAR);

	if (must_create_directory != 0) {
        make_directory(working_directory);
    }

	char text_in_wd[FILENAME_MAX];
	sprintf(text_in_wd, "%s%s_0%s",working_directory,canonical_name,extension );
	copy_file(text_in_wd,text);

	char snt_dir_text_in_wd[FILENAME_MAX];
	get_snt_path(text_in_wd, snt_dir_text_in_wd);
    if (must_create_directory != 0) {
        make_directory(snt_dir_text_in_wd);
    }

	char original_snt_dir[FILENAME_MAX];
	get_snt_path(text,original_snt_dir);
	copy_directory_snt_content(snt_dir_text_in_wd, original_snt_dir);

	return 0;
}
예제 #5
0
int
create_directory (const char *pathname, int mode)
{
	if (mode <= 0)
		mode = DEFAULT_DIRECTORY_MODE;

	if  (!pathname || *pathname == '\0') {
		errno = EINVAL;
		return -1;
	}

	mode_t oldumask = umask (022);
	char *path = strdup (pathname);
	int rv, ret = 0;
	for (char *d = path; *d; ++d) {
		if (*d != '/')
			continue;
		*d = 0;
		if (*path) {
			rv = make_directory (path, mode);
			if  (rv == -1 && errno != EEXIST)  {
				ret = -1;
				break;
			}
		}
		*d = '/';
	}
	free (path);
	if (ret == 0)
		ret = make_directory (pathname, mode);
	umask (oldumask);

	return ret;
}
예제 #6
0
파일: util.c 프로젝트: hcs64/vgm_ripping
FILE * open_file_in_directory(const char *base_name, const char *dir_name, const char orig_sep, const char *file_name, const char *perms)
{
    FILE *f = NULL;
    int dir_len = 0;;
    char * full_name = NULL;
    int full_name_len = 0;

    if (dir_name)
    {
        dir_len = strlen(dir_name);
    }

    full_name = malloc(
        strlen(base_name)+1+
        dir_len+1+
        strlen(file_name)+1);

    if (!full_name)
    {
        return f;
    }

    // start with the base (from name of archive)
    strcpy(full_name, base_name);
    full_name_len = strlen(base_name);
    make_directory(base_name);

    if (dir_len)
    {
        full_name[full_name_len++] = DIRSEP;
        for (int i = 0;  i < dir_len; i++)
        {
            if (dir_name[i] == orig_sep)
            {
                // intermediate directories
                full_name[full_name_len] = '\0';
                make_directory(full_name);
                full_name[full_name_len++] = DIRSEP;
            }
            else
            {
                full_name[full_name_len++] = dir_name[i];
            }
        }

        // last directory in the chain
        full_name[full_name_len] = '\0';
        make_directory(full_name);
    }

    full_name[full_name_len++] = DIRSEP;
    strcpy(full_name+full_name_len, file_name);

    f = fopen(full_name, perms);

    free(full_name);

    return f;
}
예제 #7
0
파일: main.c 프로젝트: m1kc/audacious
static void make_dirs(void)
{
#ifdef S_IRGRP
    const mode_t mode755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
#else
    const mode_t mode755 = S_IRWXU;
#endif

    make_directory(aud_paths[AUD_PATH_USER_DIR], mode755);
    make_directory(aud_paths[AUD_PATH_PLAYLISTS_DIR], mode755);
}
예제 #8
0
파일: narcdec.c 프로젝트: urjaman/narcdec
void output_compressed_file(const char* name, uint32_t lenComp, uint32_t lenUnc)
{
	if ((arcfile_offs+lenComp) > (uint32_t)arcfile_size) longjmp(fail_exit,1);
	char tmp[256];
	strcpy(tmp,name);
	make_directory(dirname(tmp));
	int cdfd = open(name, O_CREAT|O_WRONLY, 0666);
	if (cdfd<0) die("open");
	if (lenUnc!=0) { // Zero-len files dont need any of this
		uint8_t *obuf = malloc(lenUnc);
		if (!obuf) die("malloc");
		z_streamp strm = calloc(1,sizeof(z_stream));
		if (!strm) die("calloc");
		strm->next_in = arcfile_data+arcfile_offs;
		strm->avail_in = lenComp;
		strm->next_out = obuf;
		strm->avail_out = lenUnc;
		if (inflateInit(strm)!=Z_OK) mdie("inflateInit");
		if (inflate(strm,Z_FINISH)!=Z_STREAM_END) mdie("inflate");
		inflateEnd(strm);
		free(strm);
		if (write(cdfd,obuf,lenUnc)!=(ssize_t)lenUnc) die("write");
		free(obuf);
	}
	close(cdfd);
	arcfile_offs += lenComp;
}
예제 #9
0
extern int mkdir_main (int argc, char **argv)
{
    mode_t mode = -1;
    int flags = 0;
    int status = 0;
    int i, opt;

    while ((opt = getopt (argc, argv, "m:p")) != -1) {
        switch (opt) {
        case 'm':
            mode = 0777;
            if (!parse_mode (optarg, &mode))
                error_msg_and_die ("invalid mode `%s'", optarg);
            break;
        case 'p':
            flags |= FILEUTILS_RECUR;
            break;
        default:
            show_usage ();
        }
    }

    if (optind == argc)
        show_usage ();

    for (i = optind; i < argc; i++)
        if (make_directory (argv[i], mode, flags) < 0)
            status = 1;

    return status;
}
예제 #10
0
파일: mkdir.c 프로젝트: cpdesign/barebox
static int do_mkdir(struct command *cmdtp, int argc, char *argv[])
{
	int opt, parent = 0, ret;

	while((opt = getopt(argc, argv, "p")) > 0) {
		switch(opt) {
		case 'p':
			parent = 1;
			break;
		default:
			return 1;

		}
	}

	if (optind == argc)
		return COMMAND_ERROR_USAGE;

	while (optind < argc) {
		if (parent) {
			ret = make_directory(argv[optind]);
			if (ret == -EEXIST)
				ret = 0;
		} else {
			ret = mkdir(argv[optind], 0);
		}
		if (ret) {
			printf("could not create %s: %s\n", argv[optind], errno_str());
			return 1;
		}
		optind++;
	}

	return 0;
}
예제 #11
0
파일: main.cpp 프로젝트: avakar/gh
static void checkout_tree(gitdb & db, string_view dir, gitdb::tree_t const & t)
{
	for (auto && te: t)
	{
		std::string name = dir.to_string() + "/" + te.name;

		if ((te.mode & 0xe000) == 0xe000)
		{
			// XXX gitlink
		}
		else if (te.mode & 0x4000)
		{
			make_directory(name);
			checkout_tree(db, name, db.get_tree(te.oid));
		}
		else
		{
			file ff(name, /*readonly=*/false);

			std::vector<uint8_t> v = db.get_blob(te.oid);

			file::ofile fo = ff.seekp(0);
			write_all(fo, v.data(), v.size());
		}
	}
}
예제 #12
0
파일: file.c 프로젝트: brandongooch/epm
int					/* O - 0 = success, -1 = error */
make_link(const char *dst,		/* I - Destination file */
          const char *src)		/* I - Link */
{
  char	buffer[8192],			/* Copy buffer */
	*slash;				/* Pointer to trailing slash */


 /*
  * Check that the destination directory exists...
  */

  strcpy(buffer, dst);
  if ((slash = strrchr(buffer, '/')) != NULL)
    *slash = '\0';

  if (access(buffer, F_OK))
    make_directory(buffer, 0755, 0, 0);

 /* 
  * Make the symlink...
  */

  return (symlink(src, dst));
}
예제 #13
0
int main(int argc, char* argv[]) {
	corpus* corpus;

	long t1;
	(void) time(&t1);
	seedMT(t1);
		// seedMT(4357U);

	if (argc > 1)
	{
		if (strcmp(argv[1], "est")==0)
		{
			INITIAL_ALPHA = atof(argv[2]);
			NTOPICS = atoi(argv[3]);
			read_settings(argv[4]);
			corpus = read_data(argv[5]);
			make_directory(argv[7]);
			run_em(argv[6], argv[7], corpus);
		}
		if (strcmp(argv[1], "inf")==0)
		{
			read_settings(argv[2]);
			corpus = read_data(argv[4]);
			infer(argv[3], argv[5], corpus);
		}
	}
	else
	{
		printf("usage : lda est [initial alpha] [k] [settings] [data] [random/seeded/*] [directory]\n");
		printf("        lda inf [settings] [model] [data] [name]\n");
	}
	return(0);
}
예제 #14
0
 void test_read() {
   char *dir = make_directory();
   String* path = String::create(state, dir);
   d->open(state, path);
   String* name = as<String>(d->read(state));
   TS_ASSERT_EQUALS(name->c_str()[0], '.');
   remove_directory(dir);
 }
예제 #15
0
void
config_set_defaults(void)
	{
	CameraParameter	*param;
	Config			*cfg;
	char			*home_dir;
	boolean			valid;

	/* Camera parameter table and pikrellcam config table have initial value pointers
	|  to static storage.  Replace these pointers to an allocated copy of the
	|  initial value so that when later config changes are made the changes
	|  can be dup_string() replaced into the pointers.
	*/
	for (param = &camera_parameters[0];
				param < &camera_parameters[CAMERA_PARAMETERS_SIZE]; ++param)
		param->arg = strdup(param->arg);

	for (cfg = &config[0]; cfg < &config[CONFIG_SIZE]; ++cfg)
		{
		cfg->arg = strdup(cfg->arg);
		valid = (*cfg->config_func)(cfg->arg, &cfg->result);
		if (!valid)
			printf("config_set_default%s: %s %s\n", valid ? "" : " FAIL",
							cfg->option, cfg->arg);
		}

	pikrellcam.version = strdup(PIKRELLCAM_VERSION);
	pikrellcam.timelapse_format = strdup("tl_$n_$N.jpg");
	pikrellcam.preview_filename = strdup("");
	gethostname(pikrellcam.hostname, HOST_NAME_MAX);	

	/* If pikrellcam started by rc.local or web page, need to get correct
	|  home directory.  Makefile does a setuid/setgid on executable.
	*/
	home_dir = getpwuid(geteuid())->pw_dir;
	asprintf(&pikrellcam.config_dir, "%s/%s", home_dir, PIKRELLCAM_CONFIG_DIR);

	if (make_directory(pikrellcam.config_dir))
		{
		asprintf(&pikrellcam.config_file, "%s/%s",
					pikrellcam.config_dir, PIKRELLCAM_CONFIG);
		asprintf(&pikrellcam.motion_regions_config_file, "%s/%s",
					pikrellcam.config_dir, PIKRELLCAM_MOTION_REGIONS_CONFIG);
		asprintf(&pikrellcam.at_commands_config_file, "%s/%s",
					pikrellcam.config_dir, PIKRELLCAM_AT_COMMANDS_CONFIG);
		asprintf(&pikrellcam.timelapse_status_file, "%s/%s",
					pikrellcam.config_dir, PIKRELLCAM_TIMELAPSE_STATUS);
		}

	/* Make sure some motion regions exist.  These will be replaced if there
	|  is a motion regions config file
	*/
	motion_command("add_region 0.042 0.159 0.224 0.756");
	motion_command("add_region 0.266 0.159 0.233 0.756");
	motion_command("add_region 0.500 0.150 0.233 0.750");
	motion_command("add_region 0.734 0.156 0.224 0.753");
	motion_frame.show_regions = FALSE;
	}
예제 #16
0
파일: xload.c 프로젝트: jiangxilong/barebox
static void *am33xx_net_boot(void)
{
	void *buf = NULL;
	int err;
	int len;
	struct dhcp_req_param dhcp_param;
	const char *bootfile, *ip;
	char *file;

	am33xx_register_ethaddr(0, 0);

	memset(&dhcp_param, 0, sizeof(struct dhcp_req_param));
	dhcp_param.vendor_id = "am335x barebox-mlo";
	err = dhcp(20, &dhcp_param);
	if (err) {
		printf("dhcp failed\n");
		return NULL;
	}

	/*
	 * Older tftp server don't send the file size.
	 * Then tftpfs needs temporary place to store the file.
	 */
	err = mount("none", "ramfs", "/", NULL);
	if (err < 0) {
		printf("failed to mount ramfs\n");
		return NULL;
	}

	err = make_directory(TFTP_MOUNT);
	if (err)
		return NULL;

	ip = ip_to_string(net_get_serverip());
	err = mount(ip, "tftp", TFTP_MOUNT, NULL);
	if (err < 0) {
		printf("Unable to mount.\n");
		return NULL;
	}

	bootfile = getenv("bootfile");
	if (!bootfile) {
		printf("bootfile not found.\n");
		return NULL;
	}

	file = asprintf("%s/%s", TFTP_MOUNT, bootfile);

	buf = read_file(file, &len);
	if (!buf)
		printf("could not read %s.\n", bootfile);

	free(file);

	umount(TFTP_MOUNT);

	return buf;
}
예제 #17
0
/* Remove directory entry for the given CANAME */
void
remove_directory (const char *caname)
{
  struct directory *dir = make_directory (caname, xstrdup (caname));
  struct directory *ret = hash_delete (directory_table, dir);
  if (ret)
    free_directory (ret);
  free_directory (dir);
}
예제 #18
0
static void assert_cache_directory_existence()
{
	// We do not consider the cache directory's mode yet. Must be considerd regarding it later.
	if(!directory_exists(CACHE_DIRECTORY_PATH))
	{
		if(!make_directory(CACHE_DIRECTORY_PATH, CACHE_DIRECTORY_PERMISSION_MODE))
			int_error("Creating a cache directory failed");
	}
}
예제 #19
0
		//-----------------------------------------------------------------//
		void clear()
		{
			unit_map_.clear();
			handle_set_.clear();
			current_path_ = "/";
			string_stack a;
			a.swap(stack_path_);
			make_directory("");
		}
예제 #20
0
 void test_read_returns_nil_when_no_more_entries() {
   char *dir = make_directory();
   String* path = String::create(state, dir);
   d->open(state, path);
   d->read(state);
   d->read(state);
   TS_ASSERT(d->read(state)->nil_p());
   remove_directory(dir);
 }
예제 #21
0
int make_directory (const char *path, long mode, int flags)
{
	if (!(flags & FILEUTILS_RECUR)) {
		if (mkdir (path, 0777) < 0) {
			perror_msg ("Cannot create directory `%s'", path);
			return -1;
		}

		if (mode != -1 && chmod (path, mode) < 0) {
			perror_msg ("Cannot set permissions of directory `%s'", path);
			return -1;
		}
	} else {
		struct stat st;

		if (stat (path, &st) < 0 && errno == ENOENT) {
			int status;
			char *pathcopy, *parent, *parentcopy;
			mode_t mask;

			mask = umask (0);
			umask (mask);

			/* dirname is unsafe, it may both modify the
			   memory of the path argument and may return
			   a pointer to static memory, which can then
			   be modified by consequtive calls to dirname */
			
			pathcopy = xstrdup (path);
			parent = dirname (pathcopy);
			parentcopy = xstrdup (parent);
			status = make_directory (parentcopy, (0777 & ~mask)
									 | 0300, FILEUTILS_RECUR);
			free (pathcopy);
			free (parentcopy);


			if (status < 0 || make_directory (path, mode, 0) < 0)
				return -1;
		}
	}

	return 0;
}
예제 #22
0
파일: incremen.c 프로젝트: xrg/tar
static struct directory *
attach_directory (const char *name)
{
  struct directory *dir = make_directory (name);
  if (dirtail)
    dirtail->next = dir;
  else
    dirhead = dir;
  dirtail = dir;
  return dir;
}
예제 #23
0
TempDir::TempDir(const string& appName)
{
  for (int i=(std::rand()%0xffff); ; ++i) {
    m_path = join_path(get_temp_path(),
                       appName + convert_to<string>(i));

    if (!directory_exists(m_path)) {
      make_directory(m_path);
      break;
    }
  }
}
예제 #24
0
static struct directory *
attach_directory (const char *name)
{
  char *cname = normalize_filename (name);
  struct directory *dir = make_directory (name, cname);
  if (dirtail)
    dirtail->next = dir;
  else
    dirhead = dir;
  dirtail = dir;
  return dir;
}
예제 #25
0
static int
tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
{
	if (extractFlag==FALSE || tostdoutFlag==TRUE)
		return( TRUE);

	if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0)
		return( FALSE);

	fixUpPermissions(header);
	return( TRUE);
}
예제 #26
0
bool create_directory_if_missing(const std::string& dirname)
{
	if(is_directory(dirname)) {
		DBG_FS << "directory " << dirname << " exists, not creating\n";
		return true;
	} else if(file_exists(dirname)) {
		ERR_FS << "cannot create directory " << dirname << "; file exists\n";
		return false;
	}
	DBG_FS << "creating missing directory " << dirname << '\n';
	return make_directory(dirname);
}
예제 #27
0
 void test_control_rewinds_read_location() {
   char *dir = make_directory();
   String* path = String::create(state, dir);
   d->open(state, path);
   d->read(state);
   d->read(state);
   TS_ASSERT(d->read(state)->nil_p());
   d->control(state, Fixnum::from(1), Fixnum::from(0));
   String* name = as<String>(d->read(state));
   TS_ASSERT_EQUALS(name->c_str()[0], '.');
   remove_directory(dir);
 }
예제 #28
0
gchar *archive_decompress(const gchar *filename)
{
    gchar *tmpdir, *cmd, *escaped_filename;
    ArchiveType type;
#ifndef HAVE_MKDTEMP
#ifdef S_IRGRP
    mode_t mode755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
#else
    mode_t mode755 = S_IRWXU;
#endif
#endif

    if ((type = archive_get_type(filename)) <= ARCHIVE_DIR)
        return NULL;

#ifdef HAVE_MKDTEMP
    tmpdir = g_build_filename(g_get_tmp_dir(), "audacious.XXXXXXXX", NULL);
    if (!mkdtemp(tmpdir))
    {
        g_free(tmpdir);
        AUDDBG("Unable to load skin: Failed to create temporary "
               "directory: %s\n", g_strerror(errno));
        return NULL;
    }
#else
    tmpdir = g_strdup_printf("%s/audacious.%ld", g_get_tmp_dir(), (long)rand());
    make_directory(tmpdir, mode755);
#endif

    escaped_filename = escape_shell_chars(filename);
    cmd = archive_extract_funcs[type] (escaped_filename, tmpdir);
    g_free(escaped_filename);

    if (!cmd)
    {
        AUDDBG("extraction function is NULL!\n");
        g_free(tmpdir);
        return NULL;
    }

    AUDDBG("Attempt to execute \"%s\"\n", cmd);

    if (system(cmd) != 0)
    {
        AUDDBG("could not execute cmd %s\n", cmd);
        g_free(cmd);
        return NULL;
    }
    g_free(cmd);

    return tmpdir;
}
예제 #29
0
파일: incremen.c 프로젝트: xrg/tar
/* Return a directory entry for a given file NAME, or zero if none found.  */
static struct directory *
find_directory (const char *name)
{
  if (! directory_table)
    return 0;
  else
    {
      struct directory *dir = make_directory (name);
      struct directory *ret = hash_lookup (directory_table, dir);
      free_directory (dir);
      return ret;
    }
}
예제 #30
0
파일: write_entry.c 프로젝트: aunali1/exopc
void  _nc_set_writedir(char *dir)
/* set the write directory for compiled entries */
{
    const char *destination;

    if (dir != 0)
	(void) _nc_tic_dir(dir);
    else if (getenv("TERMINFO") != NULL)
	(void) _nc_tic_dir(getenv("TERMINFO"));

    destination = _nc_tic_dir(0);
    if (make_directory(destination) < 0)
    {
	char	*home;

	/* ncurses extension...fall back on user's private directory */
	if ((home = getenv("HOME")) != (char *)NULL &&
	    strlen(home) + sizeof(PRIVATE_INFO) <= PATH_MAX)
	{
	    char *temp = malloc(sizeof(PRIVATE_INFO) + strlen(home));
	    if (temp == NULL)
		_nc_err_abort("Out of memory");
	    (void) sprintf(temp, PRIVATE_INFO, home);
	    destination = temp;

	    if (make_directory(destination) < 0)
		_nc_err_abort("%s: permission denied (errno %d)",
			destination, errno);
	}
    }

    /*
     * Note: because of this code, this logic should be exercised
     * *once only* per run.
     */
    if (chdir(_nc_tic_dir(destination)) < 0)
	_nc_err_abort("%s: not a directory", destination);
}