Exemplo n.º 1
0
int main(void) {
   put_str("I am kernel\n");
   init_all();
/********  测试代码  ********/
   printf("/dir1 content before delete /dir1/subdir1:\n");
   struct dir* dir = sys_opendir("/dir1/");
   char* type = NULL;
   struct dir_entry* dir_e = NULL;
   while((dir_e = sys_readdir(dir))) { 
      if (dir_e->f_type == FT_REGULAR) {
	 type = "regular";
      } else {
	 type = "directory";
      }
      printf("      %s   %s\n", type, dir_e->filename);
   }
   printf("try to delete nonempty directory /dir1/subdir1\n");
   if (sys_rmdir("/dir1/subdir1") == -1) {
      printf("sys_rmdir: /dir1/subdir1 delete fail!\n");
   }

   printf("try to delete /dir1/subdir1/file2\n");
   if (sys_rmdir("/dir1/subdir1/file2") == -1) {
      printf("sys_rmdir: /dir1/subdir1/file2 delete fail!\n");
   } 
   if (sys_unlink("/dir1/subdir1/file2") == 0 ) {
      printf("sys_unlink: /dir1/subdir1/file2 delete done\n");
   }
   
   printf("try to delete directory /dir1/subdir1 again\n");
   if (sys_rmdir("/dir1/subdir1") == 0) {
      printf("/dir1/subdir1 delete done!\n");
   }

   printf("/dir1 content after delete /dir1/subdir1:\n");
   sys_rewinddir(dir);
   while((dir_e = sys_readdir(dir))) { 
      if (dir_e->f_type == FT_REGULAR) {
	 type = "regular";
      } else {
	 type = "directory";
      }
      printf("      %s   %s\n", type, dir_e->filename);
   }

/********  测试代码  ********/
   while(1);
   return 0;
}
Exemplo n.º 2
0
void tm_process_remove_kerfs_entries(struct process *proc)
{
	__remove_kerfs_proc_entry(proc, "cmask");
	__remove_kerfs_proc_entry(proc, "command");
	__remove_kerfs_proc_entry(proc, "effective_gid");
	__remove_kerfs_proc_entry(proc, "effective_uid");
	__remove_kerfs_proc_entry(proc, "exit_reason.cause");
	__remove_kerfs_proc_entry(proc, "exit_reason.coredump");
	__remove_kerfs_proc_entry(proc, "exit_reason.pid");
	__remove_kerfs_proc_entry(proc, "exit_reason.ret");
	__remove_kerfs_proc_entry(proc, "exit_reason.sig");
	__remove_kerfs_proc_entry(proc, "flags");
	__remove_kerfs_proc_entry(proc, "global_sig_mask");
	__remove_kerfs_proc_entry(proc, "heap_end");
	__remove_kerfs_proc_entry(proc, "heap_start");
	__remove_kerfs_proc_entry(proc, "real_gid");
	__remove_kerfs_proc_entry(proc, "real_uid");
	__remove_kerfs_proc_entry(proc, "refs");
	__remove_kerfs_proc_entry(proc, "stime");
	__remove_kerfs_proc_entry(proc, "thread_count");
	__remove_kerfs_proc_entry(proc, "tty");
	__remove_kerfs_proc_entry(proc, "utime");
	//__remove_kerfs_proc_entry(proc, "maps");
	char dir[128];
	snprintf(dir, 128, "/dev/process/%d", proc->pid);
	sys_rmdir(dir);
}
Exemplo n.º 3
0
/*
 * remove a empty directories
 */
int rmdir(const char * path)
{
    char buf[MAX_PATH];
    int err;

    if((err = vfs_path_conv(path, buf)) !=0 )
        return err;

    return sys_rmdir(buf);
}
Exemplo n.º 4
0
static int parasite_get_proc_fd()
{
	int ret, fd = -1;
	char buf[2];

	ret = sys_readlink("/proc/self", buf, sizeof(buf));
	if (ret < 0 && ret != -ENOENT) {
		sys_write_msg("Can't readlink /proc/self\n");
		return ret;
	}

	/* Fast path -- if /proc belongs to this pidns */
	if (ret == 1 && buf[0] == '1') {
		fd = sys_open("/proc", O_RDONLY, 0);
		goto out_send_fd;
	}

	if (sys_mkdir(proc_mountpoint, 0700)) {
		sys_write_msg("Can't create a directory ");
		sys_write_msg(proc_mountpoint);
		sys_write_msg("\n");
		return ret;
	}

	if (sys_mount("proc", proc_mountpoint, "proc", MS_MGC_VAL, NULL)) {
		sys_write_msg("mount failed\n");
		ret = -1;
		goto out_rmdir;
	}

	fd = sys_open(proc_mountpoint, O_RDONLY, 0);

	if (sys_umount2(proc_mountpoint, MNT_DETACH)) {
		sys_write_msg("Can't umount procfs\n");
		return -1;
	}

out_rmdir:
	if (sys_rmdir(proc_mountpoint)) {
		sys_write_msg("Can't remove directory\n");
		return -1;
	}

out_send_fd:
	if (fd < 0)
		return fd;
	ret = send_fd(tsock, NULL, 0, fd);
	sys_close(fd);
	return ret;
}
Exemplo n.º 5
0
asmlinkage long sys_csci3411_remove_attr    ( char *filename, char *attrname )
{
    struct stat sb	;	/* necessary structure for using sys_newstat()  */
	char *fstring	;	/* file/directory to remove attribute for       */
	char *dstring	;	/* temporary string for strstr()        */
	char buf[128]	;	/* copy of the original filename        */
	char loc[128]	;	/* full filepath for attr folder        */
    char loc2[128]	;	/* full filepath for attrname file      */
	bool_t filetype	;	/* tag for file/directory               */
	mm_segment_t fs	;	/* FS segment used to make sys calls    */
    
    fs = get_fs();
    set_fs(get_ds());
    
	/* check if filename is an existing file or directory */
	if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) )
		filetype = CSCI3411_FILE;
	else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) )
		filetype = CSCI3411_DIR;
	else
		return -1;	/* file/directory does not exist */
	
	/* split filename strings into containing folder and file */
	fstring  = strrchr(filename, '/');
	fstring += sizeof(char);
	copy_from_user( buf, filename, sizeof(buf)/sizeof(char));
	dstring = strstr( buf, fstring );
	strncpy( dstring,"\0",1);
	sprintf(loc,"%s.%s_attr",buf,fstring);
	
	/* check if attributes directory exists, return error and exit if not */
	if( sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode) )
	{
        sprintf(loc2,"%s/%s",loc,attrname);
        if( sys_newstat(loc2,&sb)==0 && S_ISREG(sb.st_mode) )
        {
            if( sys_unlink(loc2) )
                return -1;
            sys_rmdir(loc);
        }
        else
            return -1;	/* file/directory does not exist */
	}
	else
		return -1;	/* file/directory does not exist */
	set_fs(fs);
	return 0;
}
Exemplo n.º 6
0
/*
 * process RMDIR command
 */
static void rfs_rmdir(struct aipc_rfs_msg *msg)
{
    struct aipc_rfs_open *param = (struct aipc_rfs_open*)msg->parameter;
    int    ret;
    mm_segment_t oldfs = get_fs();

    set_fs(KERNEL_DS);
    ret = sys_rmdir(param->name);
    set_fs(oldfs);

    if(ret < 0) {
        DMSG("rfs_rmdir %s %d\n", param->name, ret);
    }
    msg->msg_len = sizeof(struct aipc_rfs_msg) + sizeof(int);
    msg->parameter[0] = ret;
}
Exemplo n.º 7
0
void tm_thread_remove_kerfs_entries(struct thread *thr)
{
	__remove_kerfs_thread_entry(thr, "blocklist");
	__remove_kerfs_thread_entry(thr, "cpuid");
	__remove_kerfs_thread_entry(thr, "flags");
	__remove_kerfs_thread_entry(thr, "priority");
	__remove_kerfs_thread_entry(thr, "refs");
	__remove_kerfs_thread_entry(thr, "sig_mask");
	__remove_kerfs_thread_entry(thr, "state");
	__remove_kerfs_thread_entry(thr, "system");
	__remove_kerfs_thread_entry(thr, "timeslice");
	__remove_kerfs_thread_entry(thr, "usermode_stack_end");
	char dir[128];
	snprintf(dir, 128, "/dev/process/%d/%d", thr->process->pid, thr->tid);
	sys_rmdir(dir);
}
int rdr_rm_dir(char *path)
{
	char *pdst = path;
	int ret = 0;

	while (*pdst)
		pdst++;
	pdst--;
	if (*pdst == '/')
		*pdst = '\0';
	ret = sys_rmdir(path);
	if (ret != 0)
		pr_err("rdr:%s():del %s failed\n", __func__, path);

	return ret;
}
Exemplo n.º 9
0
dev_t name_to_dev_t(char *name)
{
	char s[32];
	char *p;
	dev_t res = 0;
	int part;

#ifdef CONFIG_SYSFS
	int mkdir_err = sys_mkdir("/sys", 0700);
	if (sys_mount("sysfs", "/sys", "sysfs", 0, NULL) < 0)
		goto out;
#endif

	if (strncmp(name, "/dev/", 5) != 0) {
		unsigned maj, min;

		if (sscanf(name, "%u:%u", &maj, &min) == 2) {
			res = MKDEV(maj, min);
			if (maj != MAJOR(res) || min != MINOR(res))
				goto fail;
		} else {
			res = new_decode_dev(simple_strtoul(name, &p, 16));
			if (*p)
				goto fail;
		}
		goto done;
	}
	name += 5;
	res = Root_NFS;
	if (strcmp(name, "nfs") == 0)
		goto done;
	res = Root_RAM0;
	if (strcmp(name, "ram") == 0)
		goto done;

	if (strlen(name) > 31)
		goto fail;
	strcpy(s, name);
	for (p = s; *p; p++)
		if (*p == '/')
			*p = '!';
	res = try_name(s, 0);
	if (res)
		goto done;

	while (p > s && isdigit(p[-1]))
		p--;
	if (p == s || !*p || *p == '0')
		goto fail;
	part = simple_strtoul(p, NULL, 10);
	*p = '\0';
	res = try_name(s, part);
	if (res)
		goto done;

	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
		goto fail;
	p[-1] = '\0';
	res = try_name(s, part);
done:
#ifdef CONFIG_SYSFS
	sys_umount("/sys", 0);
out:
	if (!mkdir_err)
		sys_rmdir("/sys");
#endif
	return res;
fail:
	res = 0;
	goto done;
}
Exemplo n.º 10
0
static int syscall_dispatch(uint32_t sysnum, uint32_t args, regs_t *regs)
{
    switch (sysnum) {
        case SYS_waitpid:
            return sys_waitpid((waitpid_args_t *)args);
            
        case SYS_exit:
            do_exit((int)args);
            panic("exit failed!\n");
            return 0;
            
        case SYS_thr_exit:
            kthread_exit((void *)args);
            panic("thr_exit failed!\n");
            return 0;
            
        case SYS_thr_yield:
            sched_make_runnable(curthr);
            sched_switch();
            return 0;
            
        case SYS_fork:
            return sys_fork(regs);
            
        case SYS_getpid:
            return curproc->p_pid;
            
        case SYS_sync:
            sys_sync();
            return 0;
            
#ifdef __MOUNTING__
        case SYS_mount:
            return sys_mount((mount_args_t *) args);
            
        case SYS_umount:
            return sys_umount((argstr_t *) args);
#endif
            
        case SYS_mmap:
            return (int) sys_mmap((mmap_args_t *) args);
            
        case SYS_munmap:
            return sys_munmap((munmap_args_t *) args);
            
        case SYS_open:
            return sys_open((open_args_t *) args);
            
        case SYS_close:
            return sys_close((int)args);
            
        case SYS_read:
            return sys_read((read_args_t *)args);
            
        case SYS_write:
            return sys_write((write_args_t *)args);
            
        case SYS_dup:
            return sys_dup((int)args);
            
        case SYS_dup2:
            return sys_dup2((dup2_args_t *)args);
            
        case SYS_mkdir:
            return sys_mkdir((mkdir_args_t *)args);
            
        case SYS_rmdir:
            return sys_rmdir((argstr_t *)args);
            
        case SYS_unlink:
            return sys_unlink((argstr_t *)args);
            
        case SYS_link:
            return sys_link((link_args_t *)args);
            
        case SYS_rename:
            return sys_rename((rename_args_t *)args);
            
        case SYS_chdir:
            return sys_chdir((argstr_t *)args);
            
        case SYS_getdents:
            return sys_getdents((getdents_args_t *)args);
            
        case SYS_brk:
            return (int) sys_brk((void *)args);
            
        case SYS_lseek:
            return sys_lseek((lseek_args_t *)args);
            
        case SYS_halt:
            sys_halt();
            return -1;
            
        case SYS_set_errno:
            curthr->kt_errno = (int)args;
            return 0;
            
        case SYS_errno:
            return curthr->kt_errno;
            
        case SYS_execve:
            return sys_execve((execve_args_t *)args, regs);
            
        case SYS_stat:
            return sys_stat((stat_args_t *)args);
            
        case SYS_uname:
            return sys_uname((struct utsname *)args);
            
        case SYS_debug:
            return sys_debug((argstr_t *)args);
        case SYS_kshell:
            return sys_kshell((int)args);
        default:
            dbg(DBG_ERROR, "ERROR: unknown system call: %d (args: %#08x)\n", sysnum, args);
            curthr->kt_errno = ENOSYS;
            return -1;
    }
}
Exemplo n.º 11
0
/*****************************************************************************
 函 数 名  : power_on_log_save
 功能描述  : 保存开机log
 输入参数  :
 输出参数  : 无
 返 回 值  :
 调用函数  :
 被调函数  :
*****************************************************************************/
static int power_on_log_save( void )
{
    long pos;
    unsigned int pf;
    mm_segment_t old_fs;
    ssize_t rt = 0;
    char buf[128] = {0};
    struct rtc_time tm;
    char *power_on_reason[4];
#if (MBB_CHARGE == FEATURE_ON)
    char *power_on_mode[5];
#else
    char *power_on_mode[3];
#endif
    BATT_LEVEL_ENUM  battery_level;

    tm = power_item_info.time;
    battery_level = chg_get_batt_level();


    power_on_reason[0] = "Charger";
    power_on_reason[1] = "Power Key";
    power_on_reason[2] = "Warm Reset";
    power_on_reason[3] = "Unknown";
#if (MBB_CHG_PLATFORM_V7R2 == FEATURE_ON)
    power_on_mode[0] = "EXCEPTION";
    power_on_mode[1] = "NORMAL";
    power_on_mode[2] = "PWN CHARGING";
	power_on_mode[3] = "UPDATE";
    power_on_mode[4] = "INVALID";
#else
    power_on_mode[0] = "PWN CHARGING";
    power_on_mode[1] = "NORMAL";
    power_on_mode[2] = "UPDATE";
#endif
    /* 记录开机信息(时间、次数、关机原因) */
    snprintf(buf, sizeof(buf) - 1, "power on reason(E5): %s, power on mode : %s, current battery voltage: %d, current time: %4d-%02d-%02d %02d:%02d:%02d\r\n", \
            power_on_reason[power_item_info.reason], power_on_mode[power_item_info.mode], battery_level, tm.tm_year, tm.tm_mon, \
            tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    #if ( FEATURE_ON == MBB_MLOG )
    mlog_print(MLOG_POWER, mlog_lv_info, "POWERON mode %s\n" ,power_on_mode[power_item_info.mode]);
    mlog_print(MLOG_POWER, mlog_lv_info, "POWERON due to %s\n" ,power_on_reason[power_item_info.reason]);
    mlog_set_statis_info("on_times",1); 
    #endif

    old_fs = get_fs(); //lint !e63

    set_fs(KERNEL_DS);

    /*coverity[check_return] */
    pf = (unsigned int)sys_open(EXCH_POWER_LOG_PATH, O_RDWR | O_CREAT, 0666);
    /*coverity[unchecked_value] */
    if(IS_ERR((const void*)pf))
    {
        pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_POWER_LOG_PATH);
        return (int)pf;
    }

    /*coverity[unchecked_value] */
    pos = sys_lseek(pf, 0, SEEK_END);
    if(pos > EXCH_ONOFF_LOG_MAX){
        /* 文件超过 16k,删除重新打开 */
        sys_rmdir(EXCH_POWER_LOG_PATH);
        /*coverity[check_return] */
        pf = (unsigned int)sys_open(EXCH_POWER_LOG_PATH, O_RDWR | O_CREAT, 0666);
        /*coverity[unchecked_value] */
        if(IS_ERR((const void*)pf))
        {
            pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_POWER_LOG_PATH);
            return (int)pf;
        }
    }
    else{
        /*coverity[unchecked_value] */
        sys_lseek(pf, pos, SEEK_SET);
    }

    /*coverity[unchecked_value] */
    rt = sys_write(pf, (const char*)buf, strlen(buf));
    if(rt<0)
    {
        pr_dbg("error occured happened when write file %s, exiting.\n", EXCH_POWER_LOG_PATH);
        /*coverity[unchecked_value] */
        sys_close(pf);
        set_fs(old_fs);
        return (int)rt;
    }

    /*coverity[unchecked_value] */
    sys_close(pf);
    set_fs(old_fs);

    pr_dbg(KERN_DEBUG "power on log save.\n ");

    return (int)rt;
}
/******************************************************************************
*  Function:  power_off_log_save
*  Description: save the power off log( reason and battery voltage ).
*  Input:
*         None
*  Output:
*         None
*  Return:
*         None
*  Note  :
********************************************************************************/
LOCAL_1 int power_off_log_save( void )
{
    long pos;
    unsigned int pf;
    mm_segment_t old_fs;
    struct rtc_time tm;
    struct timespec ts;
    int     rt;
    char    buf[128];
    char    *reboot_reason[] = {"NORMAL", "BAD BATTERY", "LOWBATTERY", "OVERTEMP", \
                                    "RM_CHARGER", "UPDATE", "REBOOT", "INVALID"};

    BATT_LEVEL_ENUM                 battery_level = chg_get_batt_level();
    DRV_SHUTDOWN_REASON_ENUM        rb;

    getnstimeofday(&ts);

    rtc_time_to_tm((unsigned long)ts.tv_sec, &tm);

    power_off_ctrl.time = tm;
    rb = power_off_ctrl.reason;

    pr_dbg("%4d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year, tm.tm_mon, \
        tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    /* 记录关机信息(时间、次数、关机原因) */
    snprintf(buf, sizeof(buf) - 1, "system close reason(E5): %s, current battery voltage: %d, current time: %4d-%02d-%02d %02d:%02d:%02d\n", \
        reboot_reason[rb], battery_level, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    old_fs = get_fs(); //lint !e63

    set_fs(KERNEL_DS);

    /*coverity[check_return] */
    pf = (unsigned int)sys_open(EXCH_RESET_LOG_PATH, O_RDWR | O_CREAT, 0666);
    /*coverity[unchecked_value] */
    if(IS_ERR((const void*)pf))
    {
        pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_RESET_LOG_PATH);
        return (int)pf;
    }

    /*coverity[unchecked_value] */
    pos = sys_lseek(pf, 0, SEEK_END);
    if(pos > EXCH_ONOFF_LOG_MAX){
        /* 文件超过 16k,删除重新打开 */
        sys_rmdir(EXCH_RESET_LOG_PATH);
        /*coverity[check_return] */
        pf = (unsigned int)sys_open(EXCH_RESET_LOG_PATH, O_RDWR | O_CREAT, 0666);
        /*coverity[unchecked_value] */
        if(IS_ERR((const void*)pf))
        {
            pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_RESET_LOG_PATH);
            return (int)pf;
        }
    }
    else{
        /*coverity[unchecked_value] */
        sys_lseek(pf, pos, SEEK_SET);
    }

    /*coverity[unchecked_value] */
    rt = sys_write(pf, (const char*)buf, strlen(buf));
    if(rt<0)
    {
        pr_dbg("error occured happened when write file %s, exiting.\n", EXCH_RESET_LOG_PATH);
        /*coverity[unchecked_value] */
        sys_close( pf );
        set_fs(old_fs);
        return rt;
    }

    pr_dbg(KERN_DEBUG "power off log save.");

    /*coverity[unchecked_value] */
    sys_close( pf );
    set_fs(old_fs);

    return rt;
}
Exemplo n.º 13
0
dev_t name_to_dev_t(char *name)
{
	char s[32];
	char *p;
	dev_t res = 0;
	int part;

#ifdef CONFIG_SYSFS
	int mkdir_err = sys_mkdir("/sys", 0700);
	if (sys_mount("sysfs", "/sys", "sysfs", 0, NULL) < 0)
		goto out;
#endif

	if (strncmp(name, "/dev/", 5) != 0) {
		unsigned maj, min;

		if (sscanf(name, "%u:%u", &maj, &min) == 2) {
			res = MKDEV(maj, min);
			if (maj != MAJOR(res) || min != MINOR(res))
				goto fail;
		} else {
			res = new_decode_dev(simple_strtoul(name, &p, 16));
			if (*p)
				goto fail;
		}
		goto done;
	}
	name += 5;
	res = Root_NFS;
	if (strcmp(name, "nfs") == 0)
		goto done;
	res = Root_RAM0;
	if (strcmp(name, "ram") == 0)
		goto done;

#if defined(CONFIG_MTD_BLOCK) || defined(CONFIG_MTD_BLOCK_RO)
	/* Allow specification of MTD device by name, e.g.
	 *   root=/dev/mtdblock:foo
	 * Similar to JFFS2-specific hack in prepare_namespace(),
	 * but more generic.
	 */
	if (strncmp(name, "mtdblock:", sizeof("mtdblock:") - 1) == 0) {
		struct mtd_info *mtd =
		   get_mtd_device_nm(name + sizeof("mtdblock:") - 1);
		if (unlikely(!mtd))
			goto fail;

		sprintf(name, "mtdblock%d", mtd->index);
		put_mtd_device(mtd);
	}
#endif

	if (strlen(name) > 31)
		goto fail;
	strcpy(s, name);
	for (p = s; *p; p++)
		if (*p == '/')
			*p = '!';
	res = try_name(s, 0);
	if (res)
		goto done;

	while (p > s && isdigit(p[-1]))
		p--;
	if (p == s || !*p || *p == '0')
		goto fail;
	part = simple_strtoul(p, NULL, 10);
	*p = '\0';
	res = try_name(s, part);
	if (res)
		goto done;

	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
		goto fail;
	p[-1] = '\0';
	res = try_name(s, part);
done:
#ifdef CONFIG_SYSFS
	sys_umount("/sys", 0);
out:
	if (!mkdir_err)
		sys_rmdir("/sys");
#endif
	return res;
fail:
	res = 0;
	goto done;
}
Exemplo n.º 14
0
asmlinkage long sys_csci3411_remove_attr_all	( char *filename )
{
    struct stat sb  ;       /* Necessary structure for using sys_newstat()  */
    char *fstring   ;       /* File/directory to give attribute to          */
	char *dstring   ;       /* temporary string for strstr()                */
	char loc[128]   ;       /* Full filepath for attr folder                */
	char loc2[128]  ;       /* holds filepath for attrname file to remove   */
	char buff[128]  ;       /* holds copy of original filename              */
	char buf2[1024];        /* buffer used for the sys_getdents() call      */
    char total_attr[1024] = "\0"; /* Contains all the attribute names */
    struct linux_dirent *dent=NULL;   /* Necessary for looking through the directory */
	bool_t filetype	;       /* tag for file/directory                       */
    int i,nread,dir ;       /* necessary values for sys_getdents()          */
    char *aPtr      ;       /* holds strsep tokens                          */
	char *nPtr      ;       /* points to colon-seperated list               */
    
    mm_segment_t fs	;       /* FS segment used to make sys calls            */
    	
    fs = get_fs();
    set_fs(get_ds());
    
	/* Check if filename is an existing file or directory */
	if( sys_newstat(filename,&sb)==0 && S_ISREG(sb.st_mode) )
		filetype = CSCI3411_FILE;
	else if( sys_newstat(filename,&sb)==0 && S_ISDIR(sb.st_mode) )
		filetype = CSCI3411_DIR;
	else
		return -1;	/* file/directory does not exist */
    
	/* Split filename strings into containing folder and file */
	fstring  = strrchr(filename, '/');
	fstring += sizeof(char);
	copy_from_user( buff, filename, sizeof(buff)/sizeof(char));
	dstring = strstr( buff, fstring );
	strncpy( dstring,"\0",1);
	sprintf(loc,"%s.%s_attr",buff,fstring);
	
	// Check if the directory is valid
	if(!(sys_newstat(loc,&sb)==0 && S_ISDIR(sb.st_mode)))
		return -1;

	// Go inside the directory
    dir = sys_open(loc,O_RDONLY,0);
    if (dir == -1)  return -1;
    for( ; ; )
    {
        nread = sys_getdents(dir,buf2,1024);
        if(nread==-1) return -1;
        if(nread==0) break;
        
        // Read each file in the directory
            for(i = 0; i<nread;)
            {
                dent = (struct linux_dirent *)(buf2+i);
                // The first two entries are "." and "..", skip those
                if(strcmp((char *)(dent->d_name),".") && strcmp((char *)(dent->d_name),".."))
                {
                    // Put all the file names in total_attr
                    strcat(total_attr, (char *)(dent->d_name));
                    strcat(total_attr, ":");
                }
                i+= dent->d_reclen;
            }
    }
    sys_close(dir);
	total_attr[strlen(total_attr)-1] = '\0';	//	remove last ":"

	printk("%s\n",total_attr);
	nPtr = total_attr;
	do
	{
		aPtr = strsep(&nPtr,":");
		if(aPtr)
        {
			printk("%s\n",aPtr);
			/* remove file */
			memset(loc2,'\0',128);	//	reset the string!
			sprintf(loc2,"%s/%s",loc,aPtr);
		        if( sys_unlink(loc2) )
				return -1;
		        sys_rmdir(loc);
        }
	}while(aPtr);

	set_fs(fs);
	return 0;
}
int rdr_zip_dir(char *path, char *rgz, size_t rgz_flen)
{
	struct rdr_zip_file *dst;
	int zipfd, ret;
	struct kstat m_stat;

	rdr_debug("zip upload path to archiver file");
	/* check wether path is exist */
	if (path == NULL)
		return -1;
	ret = vfs_stat(path, &m_stat);
	if (ret) {
		DUMP_LOG(1);
		pr_err("rdr:%s():dir not exist, dont archive\n", __func__);
		return -1;
	}

	/* check path file count, if count=0, del and return, else fill field */
	rdr_dir_list(path, NULL, 0, 0, &ret, DT_REG);
	rdr_debug("file cnt is %d", ret);
	if (ret > 0) {
		char fname[RDR_FNAME_LEN];
		char timebuf[RDR_TIME_LEN];
		size_t siz = sizeof(struct rdr_zip_file) +
				sizeof(struct rdr_zip_head) * (ret - 1);
		size_t siz2 = siz + sizeof(struct rdr_struct_s);
		/* fill field */
		dst = vmalloc(siz2);
		if (dst == NULL) {
			pr_err("rdr:%s():vmalloc dst failed\n", __func__);
			return -1;
		}
		memset(dst, 0, siz);
		dst->zip_file_magic = RDR_ZIP_FILE_MAGIC;
		dst->file_number = ret;
		/* init zip env */
		rdr_zip_init(1);

		/* open zipfd */
		get_time_stamp(timebuf, RDR_TIME_LEN);
		snprintf(fname, sizeof(fname), "%s%s.rgz", path, timebuf);
		rdr_debug("archiver to %s", fname);
		zipfd = sys_open(fname, O_CREAT | O_RDWR, 0664);
		if (zipfd < 0) {
			vfree(dst);
			rdr_zip_init(0);
			return -1;
		}

		/* get dst file path and name, and open it. */
		ret = sys_lseek(zipfd, siz, SEEK_SET);
		if (ret < siz) {
			pr_err("rdr:%s():lseek to %u failed\n", __func__, siz);
			sys_close(zipfd);
			vfree(dst);
			rdr_zip_init(0);
			return -1;
		}
		rdr_dir_list(path, (rdr_funcptr_3)rdr_file2zfile,
					(u64)zipfd, (u64)dst, &ret, DT_REG);

		/* exit zip env */
		rdr_zip_init(0);

		/* write head to file: */
		ret = sys_lseek(zipfd, 0, SEEK_SET);
		if (ret != 0) {
			pr_err("rdr:%s():lseek failed\n", __func__);
			sys_close(zipfd);
			vfree(dst);
			return -1;
		}
		ret = sys_write(zipfd, (char *)dst, siz);
		if (ret < siz) {
			pr_err("rdr:%s():write head failed\n", __func__);
			sys_close(zipfd);
			vfree(dst);
			return -1;
		}

		/* close and free */
		strncpy(rgz, fname, rgz_flen);
		sys_close(zipfd);
		vfree(dst);
	} else {
		rdr_debug("delete empty dir %s", path);
		ret = sys_rmdir(path);/* del this dir */
		if (ret != 0)
			pr_err("rdr:%s():delete dir %s fail\n", __func__, path);
	}

	return 0;
}