Esempio n. 1
0
static void *
thread_function(void *arg) {
	struct thread_info *info = (struct thread_info *) arg;

	printf("thread %d: %s\n", info->tid, dirname_r(info->path));
	return NULL;
}
Esempio n. 2
0
static void TestDirname(const char* in, const char* expected_out, int expected_rc,
                        char* buf, size_t buf_size, int expected_errno) {
  errno = 0;
  int rc = dirname_r(in, buf, buf_size);
  ASSERT_EQ(expected_rc, rc) << in;
  if (rc != -1 && buf != NULL) {
    ASSERT_STREQ(expected_out, buf) << in;
  }
  ASSERT_EQ(expected_errno, errno) << in;
}
Esempio n. 3
0
char *
dirname (const char *path)
{
  static char *bname = NULL;

  if (bname == NULL)
    {
      bname = (char *) malloc (PATH_MAX);
      if (bname == NULL)
	return (NULL);
    }

  return (dirname_r (path, bname, PATH_MAX) < 0) ? NULL : bname;
}
Esempio n. 4
0
static int transfer_pull_dir(const char *path)
{
    int res;
    char *pr, *pc;

    if (!strcmp(path, "/"))
        return -1;

    pr = remote_path(path);
    pc = cache_path(path);

    if (!pr || !pc)
    {
        free(pr);
        free(pc);
        return -1;
    }

    res = clone_dir(pr, pc);

    if (res && errno == ENOENT)
    {
        int res2;
        char *parent = dirname_r(path);

        if (parent)
        {
            res2 = transfer_pull_dir(parent);
            free(parent);

            if (!res2)
                res = clone_dir(pr, pc);
        }
    }

    free(pr);
    free(pc);

    return res;
}
Esempio n. 5
0
int
main(int argc, char *argv[]) {
	if (argc < 3)
		helpAndLeave(argv[0], EXIT_FAILURE);

	char *main_thread_dirname;
	int num_threads = argc - 2; /* discard program name and main thread path */
	int s, i;
	struct thread_info thread_infos[num_threads];
	pthread_t *threads;
	void *res;

	main_thread_dirname = dirname_r(argv[1]);

	threads = malloc(num_threads * sizeof(pthread_t));
	if (!threads)
		pexit("malloc");

	for (i = 0; i < num_threads; ++i) {
		thread_infos[i].tid = i + 1;
		thread_infos[i].path = argv[i + 2];

		s = pthread_create(&threads[i], NULL, thread_function, &thread_infos[i]);
		if (s != 0)
			pthread_pexit(s, "pthread_create");
	}

	for (i = 0; i < num_threads; ++i) {
		s = pthread_join(threads[i], &res);
		if (s != 0)
			pthread_pexit(s, "pthread_join");
	}

	printf("Main thread: %s\n", main_thread_dirname);

	free(threads);
	exit(EXIT_SUCCESS);
}
Esempio n. 6
0
int git2_mkdir_2file(const char *file_path)
{
	const int mode = 0755; /* or 0777 ? */
	int error = GIT_OK;
	char target_folder_path[GIT_PATH_MAX];
	
	error = dirname_r(target_folder_path, sizeof(target_folder_path), file_path);
	
	if (error < GIT_OK)
		return GIT_ERROR;
	
	/* Does the containing folder exist? */
	if (git2_isdir(target_folder_path)) {
		joinpath(target_folder_path, target_folder_path, ""); /* Ensure there's a trailing slash */
		
		/* Let's create the tree structure */
		error = git2_mkdir_recurs(target_folder_path, mode);
		if (error < GIT_OK)
			return error;	/* The callee already takes care of setting the correct error message. */
	}
	
	return GIT_OK;
}
char* dirname(const char* path) {
    char* buf = g_dirname_tls_buffer.get();
    int rc = dirname_r(path, buf, g_dirname_tls_buffer.size());
    return (rc < 0) ? NULL : buf;
}
Esempio n. 8
0
int transfer_instant_pull(const char *path)
{
    int res;
    char *pc, *pr;
    size_t p_len = strlen(path);
    bool path_equal = false;

    VERBOSE("instant_pulling %s", path);

    pthread_mutex_lock(&m_instant_pull);

    worker_block();

    pr = remote_path2(path, p_len);
    pc = cache_path2(path, p_len);

    pthread_mutex_lock(&m_transfer);
    if (t_state.active)
        path_equal = !strcmp(path, t_state.job->path);
    pthread_mutex_unlock(&m_transfer);

    /* requested file is already being transfered (normally).
       just continue the transfer until it is finished */
    if (path_equal)
    {
        /* continuing a running transfer() only works if !worker_blocked() */
        worker_unblock();
        do
        {
            res = transfer(NULL, NULL);
        }
        while (ONLINE && res == TRANSFER_OK);

        res = (res == TRANSFER_FINISH) ? 0 : 1;
        worker_block();
    }
    else
    {
        res = copy_file(pr, pc);

        /* if copy_file failed, possibly because the file's directory didn't
           exist in the cache yet. create it and retry */
        if (res && errno == ENOENT)
        {
            char *dir = dirname_r(path);

            if (dir)
            {
                transfer_pull_dir(dir);
                free(dir);
                res = copy_file(pr, pc);
            }
        }
    }

    worker_unblock();

    copy_attrs(pr, pc);
    free(pr);
    free(pc);

    /* if copying failed, return error and dont set sync */
    if (res)
    {
        ERROR("instant_pull on %s FAILED", path);
        pthread_mutex_unlock(&m_instant_pull);
        return -1;
    }

    /* file is in sync now */
    job_delete(path, JOB_PULL);
    sync_set(path, 0);

    pthread_mutex_unlock(&m_instant_pull);
    return 0;
}
Esempio n. 9
0
char * get_suggested_filename (char *src, char *dst_ext)
{
	char	*tmp = NULL;
	char	*base = NULL;
	char	*dir_name = NULL;
#ifdef __APPLE__
	char	bname[FILENAME_MAX];
	char	dname[FILENAME_MAX];
#else
	char	saved[FILENAME_MAX];
	int		copylen;
#endif
	char	*ext = NULL;
	int		len = FILENAME_MAX;
	if (!src) {
		printf("[%s] Invalid argument.\n", __FUNCTION__);
		return tmp_name;
	}
	if (!dst_ext)
		dst_ext = dfl_ext;
#ifdef __APPLE__
	base = basename_r(src, bname);
	dir_name = dirname_r (src, dname);
#else
	copylen = strlen(src);
	if (copylen >= FILENAME_MAX) 
		copylen = FILENAME_MAX-1;
	strncpy (saved, src, copylen);
	saved[copylen] = '\0';
	base = basename(saved);
	dir_name = dirname (saved);
#endif
	ext = get_extension(src);
	NIL_DEBUG("Dirname: %s\n", dir_name);
	NIL_DEBUG("Basename: %s\n", base);
	NIL_DEBUG("Extension: %s\n", ext?ext:"(nil)");
	NIL_DEBUG("Remove extenstion [%s] --> ", base);
	tmp = strrchr(base, '.');
	if (tmp) {
		*tmp = 0x00;
		NIL_DEBUG("[%s]: changed\n", base);
	} else {
		NIL_DEBUG("[%s]: Not changed.\n", base);
	}
	tmp = (char *)malloc(FILENAME_MAX);
	if (tmp) {
		int i = 0;
		snprintf (tmp,FILENAME_MAX,"%s/%s.%s",dir_name, base, dst_ext);
		NIL_DEBUG ("Temporary Filename: %s\n", tmp);
		while (existFile(tmp)){
			snprintf (tmp,len,"%s/%s_%d.%s",dir_name, base, i, dst_ext);
			NIL_DEBUG ("Temporary Filename: %s\n", tmp);
			i++;
		}
	} else {
		tmp = tmp_name;
	}
#if 0
	{
		char *p = dir_name;
		p += strlen(dir_name);
		printf ("%p(p+strlen) %p(base) \n", p, base);
		if ( base == p+1 ){
			*p = '/';
		}
	}
#endif
	return tmp;
}