Example #1
0
/*
	Remove two directories in basepath
*/
static int sticky_rmdirs (
	STICKY_PATHS &p,
	UMSDOS_REGISTER &reg)
{
	int ret = -1;
	unlink (p.file1);
	unlink (p.file2);
	if (util_rmdir (p.dir1,reg,0)!=-1
		&& util_rmdir (p.dir2,reg,0) != -1){
		ret = 0;
	}
	return ret;
}
Example #2
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
}
Example #3
0
File: util.c Project: vifino/dwb
/*util_get_directory_content(GString **, const char *filename) {{{*/
void 
util_get_directory_content(GString *buffer, const char *dirname, const char *extension, const char *firstfile) 
{
    GDir *dir;
    char *content;
    GError *error = NULL;
    const char *filename;
    char *filepath;
    char *firstdot;

    if ( (dir = g_dir_open(dirname, 0, NULL)) ) 
    {
        while ( (filename = g_dir_read_name(dir)) ) 
        {
            if (*filename == '.') 
                continue;
            if (extension) 
            {
                firstdot = strchr(filename, '.');
                if (!firstdot)
                    continue;
                if (g_strcmp0(firstdot+1, extension))
                    continue;
            }
            filepath = g_build_filename(dirname, filename, NULL);
            if (g_file_get_contents(filepath, &content, NULL, &error)) {
                if (firstfile != NULL && g_strcmp0(firstfile, filename) == 0) {
                    g_string_prepend(buffer, content);
                }
                else {
                    g_string_append(buffer, content);
                }
            }
            else 
            {
                fprintf(stderr, "Cannot read %s: %s\n", filename, error->message);
                g_clear_error(&error);
            }
            g_free(filepath);
            g_free(content);
        }
        g_dir_close (dir);
    }

}/*}}}*/
gboolean 
util_rmdir(const char *path, gboolean only_content, gboolean recursive) 
{
    GDir *dir = g_dir_open(path, 0, NULL);
    if (dir == NULL) 
        return false;
    const char *filename = NULL;
    char *fullpath = NULL;
    while ( (filename = g_dir_read_name(dir)) )  
    {
        fullpath = g_build_filename(path, filename, NULL);
        if (!g_file_test(fullpath, G_FILE_TEST_IS_DIR)) 
            unlink(fullpath);
        else if (recursive) 
        {
            util_rmdir(fullpath, false, true);
            rmdir(fullpath);
        }
        g_free(fullpath);
    }
    if (filename == NULL && !only_content) 
        rmdir(path);

    g_dir_close(dir);
    return true;
}
Example #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();
}
Example #5
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();
}
Example #6
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();
}
Example #7
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();
}
Example #8
0
static void dir_removeall (
	const char *basepath,
	const char *recur,	// Sub-directory name
	int count,
	UMSDOS_REGISTER &reg)
{
	/* Remove all subdirectory */
	for (int i=count-1; i>0 && reg.getnberr()==0; i--){
		char path[MAXSIZ_PATH];
		strcpy (path,basepath);
		for (int j=0; j<i; j++){
			strcat (path,"/");
			strcat (path,recur);
		}
		util_rmdir (path,reg,0);
	}
}
Example #9
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();
}