Beispiel #1
0
int fs_mkdir(const char *pth, mode_t mode)
{
    printf("%s: %s\n", __func__, pth);

    char *path = (char*)malloc(strlen(pth)+1);
    strncpy(path, pth, strlen(pth)+1);

    int i;
    int slash;
    node_t* parent;
    char* file_name;
    for (i = 0; i < strlen(path); ++i)
	if (path[i] == '/')
	    slash = i;
    file_name = path + slash + 1;
    if (slash == 0) 
    {
	parent = find_node_by_name("/");
	parent->size = -2;
    } else {
	path[slash] = '\0';
	parent = find_node_by_name(path);
    }
    if ( (2 + parent->size) != 0)
	return -ENOENT;

    int node_offset = get_new_node_offset();
    if (node_offset == -1)
	return EFAULT;
    memcpy(nodes + node_offset, file_name, 6);
    *((int16_t*) (nodes + node_offset + 6)) = (int16_t) -2; 
    memset(nodes + node_offset + 8, 0, 26);

    int sector = fs_get_free_sector();
    if (sector == -1) 
	return -ENOSPC;
    map[sector] = 0xFF;
    nodes[node_offset + 8] = sector;

    char empty_sector[SECTOR_SIZE];
    memset(empty_sector, 0, sector);
    device_flush();

    fs_update_nodes();
    fs_update_map();

    if ( -1 == write_node_info_to_parent_sector
	 (file_name, parent->sectors[0], node_offset / 32))
	return EFAULT;

    return 0;
}
Beispiel #2
0
int fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fileInfo)
{
    printf("%s: %s\n", __func__, path);
    node_t* n = find_node_by_name(path);
    if (n->size == -1)
    {
	puts("Error while readdir");
	return -ENOENT;
    } else {
	if (n->size == -2)
	{
	    filler(buf, ".", NULL, 0);
	    filler(buf, "..", NULL, 0);
	    file_info_t* fi = get_file_info_by_node(n);
	    int name_index = 0;
	    char name[MAX_NAME_LEN+1] = {0};
	    while (fi->f_data[name_index] != 0 && name_index < SECTOR_SIZE)
	    {
		memcpy (name, &(fi->f_data[name_index]), 6);
		filler(buf, name, NULL, 0);
		name_index += 8;
	    }
	    return 0;
	}
	return -ENOENT;
    }
}
Beispiel #3
0
int fs_rmdir(const char *path)
{
    printf("%s: %s\n", __func__, path);

    node_t* node = find_node_by_name(path);

    file_info_t* dir_info = get_file_info_by_node(node);
    if ( dir_info->f_data[0] != 0)
	return -ENOTEMPTY;

    int i;
    for (i = 0; node->sectors[i] != 0; i++)
	map[ node->sectors[i] ] = 0x00;

    node_t* parent_node = find_parent_node(path);
    file_info_t* parent_fi = get_file_info_by_node(parent_node);
    unsigned char* par_data = parent_fi->f_data;
	
    for (i = 0; i < SECTOR_SIZE; i++)
	if (strncmp( (char*)(par_data + i), node->name, 6) == 0)
	    break;

    for ( i += 8; i < SECTOR_SIZE; i++)
	par_data[ i - 8 ] = par_data[i];

    fs_update_nodes();
    fs_update_map();

    device_write_sector(par_data, parent_node->sectors[0]);
    device_flush();
    return 0;
}
Beispiel #4
0
node_t* find_parent_node(const char* pth)
{
    printf("%s: %s\n", __func__, pth);

    char *path = (char*)malloc(strlen(pth)+1);
    strncpy(path, pth, strlen(pth)+1);

    int i;
    int slash;
    for (i = 0; i < strlen(path); ++i)
	if (path[i] == '/')
	    slash = i;
    if (slash == 0)
	return find_node_by_name("/");
    path[slash] = '\0';
    return find_node_by_name(path);
}
Beispiel #5
0
int fs_open(const char *path, struct fuse_file_info *fileInfo)
{
    printf("%s: %s\n", __func__, path);
    node_t *node = find_node_by_name(path);
    file_info_t* fi = get_file_info_by_node(node);
    fileInfo->fh = (uint64_t)fi;

    return 0;
}
Beispiel #6
0
int fs_opendir(const char *path, struct fuse_file_info *fileInfo)
{
    printf("%s: %s\n", __func__, path);
    node_t* n = find_node_by_name(path);
    if (n->size == -1) 
	return -ENOENT;
    if (n->size != -2)
	return -ENOENT;
    return 0;
}
Beispiel #7
0
int fs_mknod(const char *pth, mode_t mode, dev_t dev)
{
    printf("%s: %s\n", __func__, pth);
    if( S_ISREG(mode) == 0 )
	return -EPERM;

    char *path = (char*)malloc(strlen(pth)+1);
    strncpy(path, pth, strlen(pth)+1);

    int i;
    int slash;
    node_t* parent;
    char* file_name;
    for (i = 0; i < strlen(path); ++i)
	if (path[i] == '/')
	    slash = i;
    if (slash == 0) 
    {
	parent = find_node_by_name("/");
	parent->size = -2;
    } else {
	path[slash] = '\0';
	parent = find_node_by_name(path);
    }
    if (parent->size != -2)
	return -ENOENT;
    file_name = path + slash + 1;

    int node_offset = get_new_node_offset();
    if (node_offset == -1)
	return EFAULT;
    memcpy(nodes + node_offset, file_name, 6);
    memset(nodes + node_offset + 6, 0, 26);
    fs_update_nodes();

    if ( -1 == write_node_info_to_parent_sector
	 (file_name, parent->sectors[0], node_offset / 32))
	return EFAULT;
    return 0;
}
Beispiel #8
0
int port_remove_listener(const char *owner, const char *dev)
{
	struct rfcomm_node *node;

	node = find_node_by_name(connected_nodes, dev);
	if (!node)
		return -ENOENT;
	if (strcmp(node->owner, owner) != 0)
		return -EPERM;

	g_dbus_remove_watch(node->conn, node->listener_id);

	connected_nodes = g_slist_remove(connected_nodes, node);
	rfcomm_node_free(node);

	return 0;
}
Beispiel #9
0
int port_unregister(const char *path)
{
	struct rfcomm_node *node;
	char dev[16];
	int16_t id;

	if (sscanf(path, SERIAL_MANAGER_PATH"/rfcomm%hd", &id) != 1)
		return -ENOENT;

	snprintf(dev, sizeof(dev), "/dev/rfcomm%hd", id);
	node = find_node_by_name(bound_nodes, dev);
	if (!node)
		return -ENOENT;

	g_dbus_unregister_interface(node->conn, path, SERIAL_PORT_INTERFACE);

	return 0;
}
Beispiel #10
0
int fs_truncate(const char *pth, off_t new_size)
{
    printf("%s: %s\n", __func__, pth);

    char *path = (char*)malloc(strlen(pth)+1);
    strncpy(path, pth, strlen(pth)+1);

    node_t* node = find_node_by_name(path);
    if (new_size <= node->size)
	return -EFBIG;
    int sectors_count = fs_sectors_count(new_size);
    int i, slash;
    for ( i = sectors_count; node->sectors[i] != 0; i++)
    {
	map[ node->sectors[i] ] = 0x00;
	node->sectors[i] = 0;
    }
    node->size = new_size;

    file_info_t* parent_fi = get_file_info_by_node(find_parent_node(path));
	
    char* file_name;
    for (i = 0; i < strlen(path); ++i)
	if (path[i] == '/')
	    slash = i;
    file_name = path + slash + 1;

    for (int i = 0; i < SECTOR_SIZE; i += 8)
	if (strncpy( (char*)&(parent_fi->f_data[i]), file_name, 6) == 0)
	    break;

    int node_number = parent_fi->f_data[i + 7];

    *((int16_t*)(&(nodes[ 32*node_number + 6 ]))) = (int16_t) new_size;
    memcpy( &(nodes[32*node_number + 8]), node->sectors, 24);

    fs_update_nodes();
    fs_update_map();
    return 0;
}
Beispiel #11
0
int fs_getattr(const char *path, struct stat *statbuf)
{
    printf("%s: %s\n", __func__, path);

    int path_len = strlen(path);

    statbuf->st_uid = 0;
    statbuf->st_gid = 0;
    if ( (path_len == 1) && path[0] == '/') {
        statbuf->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
        statbuf->st_nlink = 1;
        statbuf->st_ino = 0;
        statbuf->st_size = SECTOR_SIZE;
        statbuf->st_blksize = SECTOR_SIZE;
        statbuf->st_blocks = 1;
        return 0;
    }

    node_t* n = find_node_by_name(path);
    puts("OK");
    statbuf->st_blksize = SECTOR_SIZE;
    if (n->size == -1)
    {
	puts("file not found");
	return -ENOENT;
    } else {
	if (n->size == -2)	// if dir
	{
	    statbuf->st_mode = S_IFDIR | 0777;
	    statbuf->st_nlink = 3;
	    return 0;
	} else {
	    statbuf->st_mode = S_IFREG | 0666;
	    statbuf->st_nlink = 1;
	    statbuf->st_size = n->size;
	    return 0;
	}
    }
}
Beispiel #12
0
void main_loop(pDataStruct workingData){
	char *commandList[] = {"set", "get", "add", "delete","exit","help","ls","cat","cd"};
	char *setList[] = {"date", "user", "group","help"};
	char *getList[] = {"date", "user", "group", "workingDir","help", "userlist", "grouplist", "usersingroup"};
	char *addList[] = {"user", "group", "file", "directory", "usertogroup","perm", "appendfile","help"};
	char *deleteList[] = {"user", "group", "file", "directory", "usertogroup","perm", "filebytes","help"};
	char arg[16][MAXCOMMAND];
	char command[MAXCOMMAND];
	int argnum, i, j, tempNum, cmd, size;
	char *c;
	char temp[MAXPATH];
	char *tempBuf;
	unsigned char *rcvBuf;
	pNode tempNode = NULL;
	pUser tempUser = NULL;
	pGroup tempGroup = NULL;
	pFile tempFile = NULL;
	pPerms tempPerms = NULL;
	pFileChunk tempFileChunk = NULL;
	pGroupUserList tempGroupUserList = NULL;

	while(1){
		argnum = i = j = tempNum = cmd = 0;
		c = 0;
		bzero(temp, sizeof(temp));
		tempBuf = NULL;
		tempNode = NULL;
		tempUser = NULL;
		tempGroup = NULL;
		tempFile = NULL;
		tempPerms = NULL;
		tempFileChunk = NULL;
		tempGroupUserList = NULL;		
		bzero(command, sizeof(command));
		print_prompt(workingData);
		get_string(command, MAXCOMMAND-1, "");
		for (j = 0;j<16;j++){
			bzero(arg[j],MAXCOMMAND);
		}
		argnum = parse_arg(arg, command);
		tempNum = 0;
		cmd = 100;

		for (i=0;i<sizeof(commandList)/4;i++){
			if (strcmp(commandList[i],arg[0]) == 0 ){
				cmd = i;
			}
		}
		switch(cmd)
		{
			case 0x0 : //set
				cmd = 100;
				for (i=0;i<sizeof(setList)/4;i++){
					if (strcmp(setList[i],arg[1]) == 0){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //set date
						workingData->date = get_time();
						break;//end set date

					case 1 : //set user

						if ( strcmp(arg[2],"") == 0){
							puts("missing user name or number");
							break;
						}
						tempUser = find_user(arg[2],workingData);
						if ( tempUser != NULL ){
							workingData->currentUser = tempUser;
						} else { 
							printf("unknown user: @s\n",arg[2]);
						}
						break;//end set user

					case 2 : //set group

						if ( strcmp(arg[2], "") == 0){
							puts("missing group name or number");
							break;
						}
						tempGroup = find_group(arg[2], workingData);
						if ( tempGroup != NULL){
							workingData->currentGroup = tempGroup;
						} else {
							printf("unknown group: @s\n",arg[2]);
						}
						break;

					case 3 : //set help
						print_help(setList,(sizeof(setList)/4));
						break;//end set help

					default :
						printf("Invalid command: @s\n",arg[1]);

				}

				break;//end set 

			case 1 ://get
				cmd = 100;
				for (i=0;i<sizeof(getList)/4;i++){
					if (strcmp(getList[i],arg[1]) == 0){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //get date
						strofdate( workingData->date);
						break;//end get date

					case 1 : //get user
						printf("@s\n",workingData->currentUser->name);
						break;//end get user

					case 2 : //get group
						printf("@s\n",workingData->currentGroup->name);
						break;//end get group

					case 3 : //get workingDir
						bzero(temp,MAXPATH);
						str_of_path( temp, workingData, workingData->workingDir);
						printf("@s/\n",temp);
						break;//end get workingDir

					case 4 : //get help
						print_help(getList,(sizeof(getList)/4));
						break;//end get help

					case 5 : //get userlist
						print_user_list(workingData);

						break;//end get userlist

					case 6 : //get grouplist
						print_group_list(workingData);
						break;//end get grouplist

					case 7 : //get usersingroup
						if ( strcmp(arg[2], "") == 0){
							puts("missing group name or number");
							break;
						}
						tempGroup = find_group(arg[2], workingData);
						if ( tempGroup != NULL ){
							print_users_in_group(tempGroup);
							break;
						}
						printf("unknown group: @s\n",arg[2]);
						break;//end get useringroup						

					default :
						printf("Invalid command: @s\n",arg[1]);
				}

				break;//end get

			case 2 ://add
				cmd = 100;
				for (i=0;i<sizeof(addList)/4;i++){
					if ( strcmp(addList[i],arg[1]) == 0 ){
						cmd = i;
					}
				}
				switch(cmd)
				{
					case 0 : //add user

						if ( strcmp(arg[2],"") == 0 ){
							bzero(temp,MAXPATH);
							get_string(temp, 128, "UserName");
							strcpy(arg[2],temp);
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser == NULL ){
							add_user( arg[2], workingData );
						} else {
							puts("Username already in use");
						}
						break;//end add user

					case 1 : //add group
						if ( strcmp(arg[2],"") == 0 ){
							bzero(temp,MAXPATH);
							get_string(temp, 128, "GroupName");
							strcpy(arg[2], temp);
						}
						tempGroup = find_group( arg[2], workingData);
						if ( tempGroup == NULL ){
							add_group( arg[2], workingData );
						} else {
							puts("Groupname already in use");
						}
						break;//end add group

					case 2 : //add file
						//add file name size
						tempNum = atoi(arg[3]);
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						if ( tempNum > MAXUPLOAD ){
							puts("Too big");
							break;
						}
						if ( find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode) != NULL ){
							puts("There is another file or directory with that name");
							break;
						}
						if ( tempNum > 0){
							rcvBuf = mallocOrDie(tempNum,"Failed to allocate tempBuf");
							puts("-----Begin File-----");//five - 
							if ( tempNum != receive_bytes(rcvBuf,tempNum) ){
								die("Failed to reveive file");
								break;
							}
						} else {rcvBuf = NULL;}
						tempNode = add_file( workingData->workingDir, workingData->date, tempNum, arg[2], (char *)rcvBuf, workingData->currentUser );
						break;//end add file

					case 3 : //add directory

						if (strcmp(arg[2],"") == 0){
							puts("Directory name required");
							break;
						}
						if ( find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode) != NULL ){
							puts("There is another file or directory with that name");
							break;
						}
						tempNode = add_directory( workingData->date, arg[2], workingData->workingDir, workingData->currentUser );
						break;//end add directory

					case 4 : //add useringroup
						if (strcmp(arg[2],"") == 0){
							puts("username or number required");
							break;
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser != NULL ){//user exists
							tempGroupUserList = is_user_in_group( tempUser, workingData->currentGroup );
							if ( tempGroupUserList == NULL ){//user is not already in group	
								add_user_to_group( tempUser, workingData->currentGroup );
							} else {printf("@s is already in @s\n",arg[2], workingData->currentGroup->name);}
						} else {
							puts("User does not exist, add user first");
						}

						break;//end add useringroup
						
					case 5 : //add perm
						if (  ( strcmp(arg[2],"") == 0 )||( strcmp(arg[2]," ") == 0 )  ){//arg[2] name of file or dir
							puts("name of file or directory required");
							break;
						}
						if (!(  (strcmp(arg[3],"user") == 0) ^ (strcmp(arg[3],"group") == 0) )) {//arg[3] user, group
							puts("'user' or 'group'");
							break;
						}
						if (strcmp(arg[4],"") == 0){
							puts("user name, group name, or number required");//arg[4] name or number of user or group
							break;
						}
						tempNode = find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode );//validate file or dir
						if (tempNode == NULL){
							puts("Invalid file or directory");
							break;
						}
						if (tempNode->type == LINK){
							tempNode = tempNode->directoryHeadNode;
							break;
						}
						if (strcmp(arg[3],"user") == 0){
							tempUser = find_user(arg[4],workingData);
							tempGroup = NULL;
							if (tempUser == NULL){
								printf("user @s not found\n",arg[4]);
								break;
							}
						} else {
							tempGroup = find_group(arg[4],workingData);
							tempUser = NULL;
							if (tempGroup == NULL){
								printf("group @s not found\n",arg[4]);
								break;
							}
						}

						validate_current_perms(tempNode, workingData);
						tempPerms = add_perm(tempUser, tempGroup, tempNode ); 
						break;//end add perm

					case 6 : //add appendfile
						tempNum = atoi(arg[3]);
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						if ( tempNum > MAXUPLOAD ){
							puts("Too big");
							break;
						}
						tempFile = find_file_by_name(arg[2],workingData->workingDir);
						if (  tempFile == NULL ){
							puts("No file in working directory by that name");
							break;
						}
						if ( tempNum > 0){
							tempBuf = mallocOrDie(tempNum,"Failed to allocate tempBuf");
							puts("-----Begin File-----");//five - 
							if ( tempNum != receive_bytes((unsigned char *)tempBuf,tempNum) ){
								die("Failed to reveive file");
								break;
							}
						} else {
							tempBuf = NULL;
							puts("Can not add 0 bytes to file");
							break;
						}
						tempFileChunk = add_file_chunk( tempBuf, tempFile, tempNum );
						break;//end add appendfile

					case 7 : //add help
						print_help(addList, (sizeof(addList)/4));	
						break;//end add help

					default :
						printf("Invalid command: @s\n",arg[1]);
				}

				break;//end add
			case 3 ://delete 	
				cmd = 100;
				for (i=0;i<sizeof(deleteList)/4;i++){
					if ( strcmp(deleteList[i],arg[1]) == 0 ){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //delete user
						bzero(temp,MAXPATH);
						if ( strcmp(arg[2],"") == 0 ){
							get_string(temp, 128, "User Name or number");
							strcpy(arg[2],temp);
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser != NULL ){
							remove_user(tempUser , workingData );
						} else {
							puts("No such user found");
						}
						break;//end delete user

					case 1 : //delete group
						bzero(temp,MAXPATH);
						if ( strcmp(arg[2],"") == 0 ){
							get_string(temp, 128, "Group Name or number");
							strcpy(arg[2],temp);
						}
						tempGroup = find_group( arg[2], workingData);
						if ( tempGroup != NULL ){
							remove_group(tempGroup , workingData );
						} else {
							puts("no such group found");
						}
						break;//end delete group 

					case 2 : //delete file 
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						tempNode = find_file_node_by_name(arg[2],workingData->workingDir);
						if (tempNode == NULL){
							puts("No such file");
							break;
						}
						delete_node(tempNode, workingData);
						break;//end delete file 

					case 3 : //delete directory
						if (strcmp(arg[2],"") == 0){
							puts("Directory name required");
							break;
						}
						tempNode = find_directory_by_name(arg[2],workingData->workingDir);
						if (tempNode == NULL){
							puts("No such directory");
							break;
						}
						delete_node(tempNode, workingData);
						break;//end delete directory

					case 4 : //delete usertogroup
						if (strcmp(arg[2],"") == 0){
							puts("User name required");
							break;
						}
						if (strcmp(arg[3],"") == 0){
							puts("group name required");
							break;
						}
						tempUser = find_user(arg[2],workingData);
						if (tempUser == NULL){
							puts("No such user");
							break;
						}
						tempGroup = find_group(arg[3],workingData);
						if (tempGroup == NULL){
							puts("No such group");
							break;
						}
						tempGroupUserList = is_user_in_group(tempUser, tempGroup);
						if (tempGroupUserList == NULL){
							puts("User is not in group");
							break;
						}
						remove_user_from_group(tempUser, tempGroup);
						break;//end delete usertogroup

					case 5 : //delete perm
						if (strcmp(arg[3],"") == 0){
							puts("User or group name required");
							break;
						}
						if (strcmp(arg[2],"") == 0){
							puts("file name required");
							break;
						}
						tempNode = find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode);
						if (tempNode == NULL){
							puts("No such file");
							break;
						}
						tempPerms = find_perm_by_name(arg[3],tempNode,workingData);
						if (tempPerms != NULL){
							delete_perms(tempNode, tempPerms);
						} else {
							puts("No such user permission on file");
						}
						break;//end delete perm

					case 6 : //delete filebytes [file] [numbytes]
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						size = strict_atoi(arg[3]);
						if (size == 0){
							puts("zero bytes deleted");
							break;
						}
						//validate file						
						tempNode = find_file_node_by_name(arg[2],workingData->workingDir);
						tempFile = tempNode->file;
						if (tempNode == NULL){
							puts("No such file");
							printf("@s\n",arg[2]);
							break;
						}
						tempNum = get_file_size(tempFile);
						if (size > tempNum){
							puts("Too many bytes");
						} 
						//tempNum is new file size
						tempNum = tempNum - size;
						delete_file_bytes(tempFile, tempNum);
						break;//end delete file 

					case 7 : //delete help
						print_help(deleteList, (sizeof(deleteList)/4));	
						break;//end delete help

					default:
						print_help(deleteList, (sizeof(deleteList)/4));	
						//break;//end delete help

				}
				break;//end delete
				

			case 4 ://cgc_exit
				puts("exiting");
				goto cgc_exit;
				break;

			case 5 ://help
				print_help(commandList, (sizeof(commandList)/4));
				break;
			case 6 ://ls
				print_working_dir(workingData);
				break;
			case 7 ://cat
				if (strcmp(arg[1],"") == 0){
					puts("Filename required");
					break;
				}
				tempFile = find_file_by_name(arg[1], workingData->workingDir);							
				if ( tempFile != NULL ){
					tempFileChunk = tempFile->head;
					puts("-----Begin File-----");//five - 
					while ( tempFileChunk != NULL ){
						if (tempFileChunk->chunkSize != cgc_write(tempFileChunk,tempFileChunk->chunkSize)){
							puts("file write failed");
						}
/*
						c = tempFileChunk->chunk;
						for ( i = 0; i < tempFileChunk->chunkSize; i++ ){//putc each byte of every chunk
							putc( *c);
							c++;
						}
*/						
						tempFileChunk = tempFileChunk->next;	
					}
					puts("-----END File-----");//five - 
				}
				break;
			case 8 ://cd
				if (strcmp(arg[1],"") == 0){
					puts("directory required");
					break;
				}
				if (strcmp(arg[1],"..") == 0){
					if (workingData->workingDir->parent != NULL){
						workingData->workingDir = workingData->workingDir->parent;
					}
					break;
				}
				tempNode = find_directory_by_name(arg[1],workingData->workingDir);
				if ( tempNode != NULL ){
					workingData->workingDir = tempNode;
					break;
				}
				puts("No such directory in working directory");
				break;//end cd

			default :
				printf("Invalid command @s\n",arg[0]);
				print_help(commandList, (sizeof(commandList)/4));
				
		}
	}
	cgc_exit:
	return;
}