Exemplo n.º 1
0
static void dir_spcent_test(
	const char *path,
	UMSDOS_REGISTER &reg)
{
	char spath[MAXSIZ_PATH];
	sprintf (spath,"%s/.",path);
	char sspath[MAXSIZ_PATH];
	sprintf (sspath,"%s/..",path);
	util_mkdir(spath,0777,reg,EEXIST);
	util_mkdir(sspath,0777,reg,EEXIST);
#ifndef KERN_22X
	util_rmdir(spath,reg,EPERM);
	util_rmdir(sspath,reg,EPERM);
#endif
}
Exemplo n.º 2
0
static int dir_walk(const char *basepath)
{
	#define RECUR_NAME "RECUR"
	Rdir_walk.verbose ("Creating directories %s/"
		RECUR_NAME "/" RECUR_NAME "/" RECUR_NAME "\n"
		,basepath);
	for (int i=1; i<4 && Rdir_walk.getnberr()==0; i++){
		char path[MAXSIZ_PATH];
		strcpy (path,basepath);
		for (int j=0; j<i; j++) strcat (path,"/" RECUR_NAME);
		util_mkdir(path,0777,Rdir_walk,0);
	}
	if (Rdir_walk.getnberr()==0){
		char wpath[MAXSIZ_PATH];
		struct stat info1;
		sprintf (wpath,"%s/" RECUR_NAME
			"/./../"
			RECUR_NAME "/" RECUR_NAME "/" RECUR_NAME
			"/../../../" RECUR_NAME
			,basepath);
		util_stat (wpath,&info1,Rdir_walk,0);
	}
	dir_removeall (basepath,RECUR_NAME,4,Rdir_walk);
	return Rdir_walk.getnberr();
}
Exemplo n.º 3
0
void
weechat_create_home_dir ()
{
    char *ptr_weechat_home, *config_weechat_home;
    struct stat statinfo;

    /*
     * weechat_home is not set yet: look for environment variable
     * "WEECHAT_HOME"
     */
    if (!weechat_home)
    {
        ptr_weechat_home = getenv ("WEECHAT_HOME");
        if (ptr_weechat_home && ptr_weechat_home[0])
            weechat_set_home_path (ptr_weechat_home);
    }

    /* weechat_home is still not set: try to use compile time default */
    if (!weechat_home)
    {
        config_weechat_home = WEECHAT_HOME;
        if (!config_weechat_home[0])
        {
            string_fprintf (stderr,
                            _("Error: WEECHAT_HOME is undefined, check build "
                              "options\n"));
            weechat_shutdown (EXIT_FAILURE, 0);
            /* make C static analyzer happy (never executed) */
            return;
        }
        weechat_set_home_path (config_weechat_home);
    }

    /* if home already exists, it has to be a directory */
    if (stat (weechat_home, &statinfo) == 0)
    {
        if (!S_ISDIR (statinfo.st_mode))
        {
            string_fprintf (stderr,
                            _("Error: home (%s) is not a directory\n"),
                            weechat_home);
            weechat_shutdown (EXIT_FAILURE, 0);
            /* make C static analyzer happy (never executed) */
            return;
        }
    }

    /* create home directory; error is fatal */
    if (!util_mkdir (weechat_home, 0755))
    {
        string_fprintf (stderr,
                        _("Error: cannot create directory \"%s\"\n"),
                        weechat_home);
        weechat_shutdown (EXIT_FAILURE, 0);
        /* make C static analyzer happy (never executed) */
        return;
    }
}
Exemplo n.º 4
0
static int hlink_simple (const char *basepath)
{
	char dpath[MAXSIZ_PATH];
	sprintf (dpath,"%s/dir",basepath);
	if (util_mkdir (dpath,0777,Rhlink_simple,0)!=-1){
		char fpath[MAXSIZ_PATH];
		sprintf (fpath,"%s/file",dpath);
		FILE *fout = util_fopen (fpath,"w",Rhlink_simple);
		if (fout != NULL){
			fprintf (fout,"hello\n");
			fclose (fout);
			hlink_many (fpath,fpath,1,Rhlink_simple);
			/* #Specification: utstgen / hard link / cases / across directory boundary
				The target of the link is not in the same directory
				as the new link.
			*/
			char ddpath[MAXSIZ_PATH];
			sprintf (ddpath,"%s/sdir",dpath);
			if (util_mkdir (ddpath,0777,Rhlink_simple,0)!=-1){
				char spath[MAXSIZ_PATH];
				sprintf (spath,"%s/file",ddpath);
				hlink_many (fpath,spath,0,Rhlink_simple);
				util_rmdir (ddpath,Rhlink_simple,0);
			}
			util_unlink (fpath,Rhlink_simple,0);
			/* #Specification: utstgen / hard link / cases / target does not exist
				Many hard links are attempted to a file which
				does not exist.
			*/
			hlink_many (fpath,fpath,0,Rhlink_simple);
		}
		/* #Specification: utstgen / hard link / to a directory
			A hard link can't be made to a directory.
		*/
		char spath[MAXSIZ_PATH];
		sprintf (spath,"%s/error",basepath);
		util_hardlink (dpath,spath,Rhlink_simple,EPERM);
		util_rmdir (dpath,Rhlink_simple,0);
	}
	return Rhlink_simple.getnberr();
}
Exemplo n.º 5
0
int tst_util_mkdir()
{
	//
	// int util_mkdir(const char *path);
	//

	int ret;

	// create directory
	ret = util_mkdir("util/mkdir/new\n");
	TEST_ASSERT(ret == 0, "Unable to create directory\n");

	// This should not fail, as long as the dir exists
	ret = util_mkdir("util/mkdir/new");
	TEST_ASSERT(ret == 0, "Failed when creating already existing directory\n");

	// This should fail - NULL string
	ret = util_mkdir(0);
	TEST_ASSERT(ret != 0 && errno == EINVAL, "Accepted NULL argument\n");

	//
	// util_mkdir wont take a string with more than 4096 chars
	// This must fail, with errno as ENAMETOOLONG
	//
	char s[5000] = {64};
	memset(s, 64, 5000);
	s[4999] = 0;
	ret = util_mkdir(s);
	TEST_ASSERT(ret != 0 && errno == ENAMETOOLONG, "Path too long");
	
	//
	// This should work - 4096 bytes string
	//
	s[4096] = 0;
	ret = util_mkdir(s);
	TEST_ASSERT(ret == 0, "Wont take a 4096 byte string");


	return 0;
}
Exemplo n.º 6
0
/*
	Create one directory with specific ID and permissions.
*/
static int sticky_mkdir (
	const char *dir,
	STICKY_MODOWN &s,
	UMSDOS_REGISTER &reg)
{
	int ret = -1;
	if (util_mkdir (dir,0777,reg,0) != -1
		&& util_chown (dir,s.uid,s.gid,reg,0) != -1
		&& util_chmod (dir,s.mode,reg,0)!=-1){
		ret = 0;
	}
	return ret;
}
Exemplo n.º 7
0
int
util_mkdir_parents (const char *directory, int mode)
{
    char *string, *ptr_string, *pos_sep;
    struct stat buf;
    int rc;
    
    if (!directory)
        return 0;
    
    string = strdup (directory);
    if (!string)
        return 0;
    
    ptr_string = string;
    while (ptr_string[0] == DIR_SEPARATOR_CHAR)
    {
        ptr_string++;
    }
    
    while (ptr_string && ptr_string[0])
    {
        pos_sep = strchr (ptr_string, DIR_SEPARATOR_CHAR);
        if (pos_sep)
            pos_sep[0] = '\0';
        
        rc = stat (string, &buf);
        if ((rc < 0) || !S_ISDIR(buf.st_mode))
        {
            /* try to create directory */
            if (!util_mkdir (string, mode))
            {
                free (string);
                return 0;
            }
        }
        
        if (pos_sep)
        {
            pos_sep[0] = DIR_SEPARATOR_CHAR;
            ptr_string = pos_sep + 1;
        }
        else
            ptr_string = NULL;
    }
    
    free (string);
    
    return 1;
}
Exemplo n.º 8
0
static int dir_simple(const char *basepath)
{
	char path[MAXSIZ_PATH];
	sprintf (path,"%s/dir",basepath);
	Rdir_simple.verbose ("Creating directory %s\n",path);
	if (util_mkdir(path,0777,Rdir_simple,0)!=-1){
		if (util_dir_count (path,Rdir_simple)!=2){
			Rdir_simple.prterr ("Empty directory not empty ??\n");
		}
		util_mkdir  (path,0777,Rdir_simple,EEXIST);
		util_create (path,0666,Rdir_simple,EISDIR);
		char fpath[MAXSIZ_PATH];
		sprintf (fpath,"%s/file1",path);
		util_create (fpath,0666,Rdir_simple,0);
		util_mkdir  (fpath,0777,Rdir_simple,EEXIST);
		util_rmdir (path,Rdir_simple,ENOTEMPTY);
		util_rmdir (fpath,Rdir_simple,ENOTDIR);
		util_unlink (fpath,Rdir_simple,0);
		util_mkdir (path,0777,Rdir_simple,EEXIST);
		util_unlink (path,Rdir_simple,EISDIR);
		util_rmdir (path,Rdir_simple,0);
	}
	return Rdir_simple.getnberr();
}
Exemplo n.º 9
0
static int dir_spcent(const char *basepath)
{
	char path[MAXSIZ_PATH];
	sprintf (path,"%s/dir",basepath);
	Rdir_spcent.verbose ("Creating directory %s\n",path);
	/* #Specification: utstgen / creating . and ..
		A check is done that the special entries . and .. can't be
		created nor removed.
	*/
	if (util_mkdir(path,0777,Rdir_spcent,0)!=-1){
		dir_spcent_test (path,Rdir_spcent);
		dir_spcent_test (basepath,Rdir_spcent);
		util_rmdir (path,Rdir_spcent,0);
	}
	return Rdir_spcent.getnberr();
}
Exemplo n.º 10
0
static void dir_recur(
	const char *basepath,
	const char *recur,
	UMSDOS_REGISTER &reg)
{
	for (int i=0; i<10 && reg.getnberr()==0; i++){
		char path[MAXSIZ_PATH];
		strcpy (path,basepath);
		for (int j=0; j<i; j++){
			strcat (path,"/");
			strcat (path,recur);
		}
		if (i == 0 || util_mkdir(path,0777,reg,0)!=-1){
			if (dir_simple (path) == -1){
				reg.prterr ("Failure at level %d\n",i);
				break;
			}
		}
	}
	dir_removeall (basepath,recur,10,reg);
}
Exemplo n.º 11
0
void util_make_path(const char *_path) {
  char *active_path;
  char *path = (char *) _path;
  int current_pos = 0;
  
  if (!util_is_directory(path)) {
    int i = 0;
    active_path = util_calloc(strlen(path) + 1 , sizeof * active_path );
    do {
      int n = strcspn(path , UTIL_PATH_SEP_STRING);
      if (n < strlen(path))
        n += 1;
      path += n;
      i++;
      strncpy(active_path , _path , n + current_pos); 
      active_path[n+current_pos] = '\0';
      current_pos += n; 
      
      if (!util_is_directory(active_path)) {
        if (util_mkdir(active_path) != 0) { 
          bool fail = false;
          switch (errno) {
          case(EEXIST):
            if (util_is_directory(active_path))
              fail = false;
            break;
          default:
            fail = true;
            break;
          }
          if (fail)
            util_abort("%s: failed to make directory:%s - aborting\n: %s(%d) \n",__func__ , active_path , strerror(errno), errno);
        }
      }
      
    } while (strlen(active_path) < strlen(_path));
    free(active_path);
  }
}
Exemplo n.º 12
0
static int dir_removebusy(const char *basepath)
{
	char path[MAXSIZ_PATH];
	sprintf (path,"%s/dir",basepath);
	Rdir_spcent.verbose ("Creating directory %s\n",path);
	/* #Specification: utstgen / removing a busy directory
		A check is done that a busy directory can't be removed.
		Here is the sequence we test. It must fail with EBUSY.
		
		mkdir dir
		cd dir
		rm ../dir
	*/
	if (util_mkdir(path,0777,Rdir_removebusy,0)!=-1){
		char curdir[MAXSIZ_PATH];
		getcwd(curdir,sizeof(curdir)-1);
		if (util_chdir(path,Rdir_removebusy,0)!=-1){
			util_rmdir (path,Rdir_removebusy,EBUSY);
			util_chdir (curdir,Rdir_removebusy,0);
		}
		util_rmdir (path,Rdir_removebusy,0);
	}
	return Rdir_removebusy.getnberr();
}
Exemplo n.º 13
0
static int hlink_simple (const char *basepath)
{
	/* #Specification: utstspc / hard link / subdirectory of a dos directory
		We create two subdirectory in a DOS directory. We switch
		those to Umsdos mode (umssync). We set a lot of hard link
		between those two directories.

		This test try to demonstrate that the only thing that
		matter is that both subdirectory must be umsdos directories.
		But the parents don't have to.
	*/
	char dpath1[MAXSIZ_PATH];
	sprintf (dpath1,"%s/dir1",basepath);
	if (utilspc_mkdir_udos (dpath1,0777,Rhlink_simple)!=-1){
		char fpath[MAXSIZ_PATH];
		sprintf (fpath,"%s/file",dpath1);
		FILE *fout = util_fopen (fpath,"w",Rhlink_simple);
		if (fout != NULL){
			fprintf (fout,"hello\n");
			fclose (fout);
			hlink_many (fpath,fpath,1,Rhlink_simple);
			/* #Specification: utstspc / hard link / cases / across directory boundary
				The target of the link is not in the same directory
				as the new link.
			*/
			char dpath2[MAXSIZ_PATH];
			sprintf (dpath2,"%s/dir2",basepath);
			if (utilspc_mkdir_udos (dpath2,0777,Rhlink_simple)!=-1){
				char spath[MAXSIZ_PATH];
				sprintf (spath,"%s/file",dpath2);
				hlink_many (fpath,spath,1,Rhlink_simple);
				util_rmdir (dpath2,Rhlink_simple,0);
			}
			char dpath3[MAXSIZ_PATH];
			sprintf (dpath3,"%s/dir3",basepath);
			if (util_mkdir (dpath3,0777,Rhlink_simple,0)!=-1){
				/* #Specification: utstspc / hard link / in a DOS directory
					A test is done to demonstrate that a hard link can't be
					created in a DOS directory.
				*/
				char spath[MAXSIZ_PATH];
				sprintf (spath,"%s/file",dpath3);
				util_hardlink (fpath,spath,Rhlink_simple,EINVAL);
				util_rmdir (dpath3,Rhlink_simple,0);
			}
			util_unlink (fpath,Rhlink_simple,0);
			/* #Specification: utstspc / hard link / cases / target does not exist
				Many hard links are attempted to a file which
				does not exist.
			*/
			hlink_many (fpath,fpath,0,Rhlink_simple);
		}
		/* #Specification: utstspc / hard link / to a directory
			A hard link can't be made to a directory.
		*/
		char spath[MAXSIZ_PATH];
		sprintf (spath,"%s/error",basepath);
		util_hardlink (dpath1,spath,Rhlink_simple,EPERM);
		util_rmdir (dpath1,Rhlink_simple,0);
	}
	return Rhlink_simple.getnberr();
}
Exemplo n.º 14
0
void
weechat_create_home_dir ()
{
    char *ptr_home, *config_weechat_home = WEECHAT_HOME;
    int dir_length;
    struct stat statinfo;

    if (!weechat_home)
    {
        if (strlen (config_weechat_home) == 0)
        {
            string_iconv_fprintf (stderr,
                                  _("Error: WEECHAT_HOME is undefined, check "
                                    "build options\n"));
            weechat_shutdown (EXIT_FAILURE, 0);
            /* make C static analyzer happy (never executed) */
            return;
        }

        if (config_weechat_home[0] == '~')
        {
            /* replace leading '~' by $HOME */
            ptr_home = getenv ("HOME");
            if (!ptr_home)
            {
                string_iconv_fprintf (stderr,
                                      _("Error: unable to get HOME directory\n"));
                weechat_shutdown (EXIT_FAILURE, 0);
                /* make C static analyzer happy (never executed) */
                return;
            }
            dir_length = strlen (ptr_home) + strlen (config_weechat_home + 1) + 1;
            weechat_home = malloc (dir_length);
            if (weechat_home)
            {
                snprintf (weechat_home, dir_length,
                          "%s%s", ptr_home, config_weechat_home + 1);
            }
        }
        else
        {
            weechat_home = strdup (config_weechat_home);
        }

        if (!weechat_home)
        {
            string_iconv_fprintf (stderr,
                                  _("Error: not enough memory for home "
                                    "directory\n"));
            weechat_shutdown (EXIT_FAILURE, 0);
            /* make C static analyzer happy (never executed) */
            return;
        }
    }

    /* if home already exists, it has to be a directory */
    if (stat (weechat_home, &statinfo) == 0)
    {
        if (!S_ISDIR (statinfo.st_mode))
        {
            string_iconv_fprintf (stderr,
                                  _("Error: home (%s) is not a directory\n"),
                                  weechat_home);
            weechat_shutdown (EXIT_FAILURE, 0);
        }
    }

    /* create home directory; error is fatal */
    if (!util_mkdir (weechat_home, 0755))
    {
        string_iconv_fprintf (stderr,
                              _("Error: cannot create directory \"%s\"\n"),
                              weechat_home);
        weechat_shutdown (EXIT_FAILURE, 0);
    }
}
Exemplo n.º 15
0
int archive_extract_all( char *arch_file, char *dest_dir, char *suffix )
{
    int                     ret, flags;     
    char                    *pwd = NULL, *filename = NULL, *filename_new = NULL, *hardlink = NULL;
    struct archive          *arch_r = NULL, *arch_w = NULL;
    struct archive_entry    *entry = NULL;


    if( !arch_file )
        return -1;

    arch_r = archive_read_new();
    archive_read_support_format_all( arch_r );
    archive_read_support_compression_all( arch_r );

    if( archive_read_open_filename( arch_r, arch_file, 10240 ) != ARCHIVE_OK )
        goto errout;

    if( dest_dir )
    {
        if( util_mkdir( dest_dir ) == -1 )
        {
            if( errno == EEXIST )
            {
                if( access( dest_dir, R_OK | W_OK | X_OK ) == -1 )
                {
                    goto errout;
                }

            }
            else
            {
                goto errout;
            }
        }
        pwd = getcwd( NULL, 0 );
        chdir( dest_dir );
    }

    flags = ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS | ARCHIVE_EXTRACT_OWNER;
    arch_w = archive_write_disk_new();
    archive_write_disk_set_options( arch_w, flags );
    archive_write_disk_set_standard_lookup( arch_w );

    while( archive_read_next_header( arch_r, &entry ) == ARCHIVE_OK ) 
    {
        if( suffix )
            filename = (char *)archive_entry_pathname( entry );
        
#ifdef DEBUG
        if( !filename )
            filename = (char *)archive_entry_pathname( entry );

        printf("extract:%s\n", filename );
#endif

        if( suffix && archive_entry_filetype( entry ) != AE_IFDIR  )
        {
            filename_new = util_strcat( filename, suffix, NULL );
            archive_entry_set_pathname( entry, filename_new );
            free( filename_new );

            if( archive_entry_nlink( entry ) > 0 )
            {
                hardlink = (char *)archive_entry_hardlink( entry );
                if( hardlink )
                {
                    filename_new = util_strcat( hardlink, suffix, NULL );
                    archive_entry_set_hardlink( entry, filename_new );
                    free( filename_new );
                }
            }

        }

        ret = archive_read_extract2( arch_r, entry, arch_w );
        if( ret != ARCHIVE_OK && ret != ARCHIVE_WARN ) 
        {
            goto errout;
        }


#ifdef DEBUG
        if( ret != ARCHIVE_OK)
        {
            printf("ret:%d, file:%s, link:%d, size:%d, read_err:%s, write_err:%s\n", ret, filename, archive_entry_nlink(entry), archive_entry_size(entry), archive_error_string(arch_r), archive_error_string(arch_w));
        }
#endif
    }


    archive_read_finish( arch_r );
    archive_write_finish( arch_w );
    if( pwd )
    {
        chdir( pwd );
        free( pwd );
    }
    return 0;

errout:

    if( arch_r )
        archive_read_finish( arch_r );

    if( arch_w )
        archive_write_finish( arch_w );

    if( pwd )
    {
        chdir( pwd );
        free( pwd );
    }
    return -1;
}