Ejemplo n.º 1
0
//adds an entry to the directory structure
int Add_to_Directory(char* Filename, int inodeNumber)
{
    int fileExist = Search_Directory(Filename);
    Inode curNode = Inode_Read(inodeNumber);
    
    //if file does not exist and inode is not in use
    if ((fileExist == -1) && (curNode.Inode_Number == -1))
    {
        //create directory entry and claim inode
        strcpy(Directory_Structure[inodeNumber].Filename, Filename);
        Directory_Structure[inodeNumber].Inode_Number = inodeNumber;
        Inode_List[inodeNumber].Inode_Number = inodeNumber;
        FILE *File = fopen(Log_Filename,"a");
        log_timestamp(File);
        fprintf(File,"Directory\t\t\t Write\n");
        fclose(File);
        return 1;
    }
    
    FILE *File = fopen(Log_Filename,"a");
    log_timestamp(File);
    fprintf(File,"Directory\t\t\t Read\n");
    fclose(File);
    return 0;   //file already exists or inode is already initialized
}
Ejemplo n.º 2
0
//creates a new file inode and adds it to directory
int Create_File(char* filename)
{
    //check if file exists already
    if (Search_Directory(filename) != -1)
    {
		printf("Error, file already exists\n");
		return -1;
	}     //file already exists
    
    //get next free inode
    Super_block curSuper = Superblock_Read();
    int nextInode = curSuper.next_free_inode;
    if (nextInode > 127)
    {
		printf("Error, out of Inodes\n");
		return -1;
	}     //out of inodes
    curSuper.next_free_inode++;
    Superblock_Write(curSuper);
    
    //initialize new inode
    Inode newNode;
    newNode.Inode_Number = nextInode;
    newNode.User_Id = getuid();
    newNode.Group_Id = getgid();
    newNode.File_Size = 0;
    newNode.Start_Block = -1;
    newNode.End_Block = -1;
    newNode.Flag = 0;
    
    //create directory entry
    if (!Add_to_Directory(filename, nextInode))
    {
		printf("Error, could not add entry to directory\n");
		curSuper.next_free_inode--;
        Superblock_Write(curSuper);
		return -1;
	}     //could not add entry to directory
	
    //update inode in list
    if (!Inode_Write(nextInode, newNode))
    {
		printf("Error, could not update Inode\n");
		curSuper.next_free_inode--;
        Superblock_Write(curSuper);
		return -1;
	}     //could not update inode
	
    return nextInode;
}
Ejemplo n.º 3
0
//sets the flag in the inode corresponding to the filename
    //return the inode number for the file
int Open_File(char* filename)
{
    int curNodeNum = Search_Directory(filename);
    if (curNodeNum == -1)
    {
		printf("Error, file does not exist\n");
		return -1;
	}    //file does not exist
    
    Inode curNode = Inode_Read(curNodeNum);
    if (curNode.Inode_Number == -1)
    {
		printf("Error, Inode is invalid\n");
		return -1;
	}    //inode is not valid file
    
    //set flag and update inode
    curNode.Flag = 1;
    Inode_Write(curNodeNum, curNode);
    
    return curNodeNum;
}
Ejemplo n.º 4
0
Archivo: test.c Proyecto: jz685/OS
//Read all files in the input directory and copy all files to mini file system
int ReadDir(const char *dir)
{
	int file_count = 0;
	int temp = 0;
	struct dirent* ptr;
	DIR* srcdir = opendir(dir);
	pid_t childpid;

	if (srcdir == NULL)
	{
		perror("opendir");
		return -1;
	}

	while((ptr = readdir(srcdir)) != NULL)										// traverse done
	{
		char* d_name;
		d_name = ptr -> d_name;

		if (ptr -> d_type & DT_DIR)									// check whether it is a directory
		{
			if(strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0)
			{
				char path[100];
				sprintf(path, "%s/%s", dir, d_name);
				temp = ReadDir(path);
				file_count += temp;
			}
		}else{
			printf("\n/////////////////////////START//////////////////////////\n%s/%s\n", dir, d_name);
			if (isJPG(d_name)){
				//---------------Jia------------------
				//fork
				
				childpid = fork();
				
				//error handling
				if (childpid == -1) 
				{
					perror("Failed to fork");
					return 1;
				}
				//child code
				if (childpid == 0) 
				{
					fprintf(stderr, "--------------I am the child----------------\n");
					char filepath[100];
					char newpath[100];
					sprintf(filepath, "%s/%s", dir, d_name);
					sprintf(newpath, "%s%s", filepath, "encd");
					fprintf(stderr,"filepath is %s \t newpath is %s\t\n", filepath, newpath);
					char cmd[200];
					strcpy(cmd, "base64 ");
					strcat(cmd, filepath);
					strcat(cmd, " > ");
					strcat(cmd, newpath);
					execlp("/bin/bash", "bash", "-c", cmd, NULL);
					//execl("/usr/bin/base64", "base64", filepath, ">", newpath);
				}else{
					//wait
					while(r_wait(NULL) > 0)
					{
						fprintf(stderr, "Parent Wait\n");
					}
					fprintf(stderr, "--------------I am the parent----------------\n");
					//---------------end jia---------------
					//--------------------Create---------------------------
					int f = Create_File(d_name);
					//printInode(Inode_List[Superblock.next_free_inode-1]);
					if(f == -3)
					{
						printf("File could not be added to Directory.\n");
						continue;
					}else if (f == -2)
					{
						printf("File could not be written to the new inode.\n");
						continue;
					}else if (f == -1)
					{
						printf("File already existed in directory.\n");
						continue;
					}else{
						//printf("ptr->d_name: %s\n", d_name);			// #################
						file_count++;
						//------------------------------Copy image to mini file system-------------------------------------------
						FILE *fp;
						int ch;
						//fprintf(stderr, "d_name is: %s\n", d_name);
						char filepath[100];
						sprintf(filepath, "%s/%s", dir, d_name);
						//--------------jia--------------------
						char newpath[100];
						sprintf(newpath, "%s%s", filepath, "encd");
						fprintf(stderr,"filepath is %s \t newpath is %s\t\n", filepath, newpath);
						fp = fopen(newpath, "r");
						///--------------end jia----------------
						
						//fp = fopen(filepath, "r");
						if (fp == NULL) {
							fprintf(stderr, "Cannot open %s/%s .....\n", dir, d_name);
							break;														// what we should do if could not read file!!!!!!!!!!!!!!
						}else{
							//------------------------------Copy file to mini_filesystem---------------------------------------
							fseek(fp, 0L, SEEK_END);
							int sz = ftell(fp);
							fseek(fp, 0L, SEEK_SET);
							char* to_write = (char*) malloc(sizeof(char)*sz);
							int i = 0;
							while( (ch = fgetc(fp)) != EOF )
							{
								to_write[i] = ch;
								//fprintf(stderr, "%c\t", ch);
								i++;
							}
							//fprintf(stderr, "\nto_write is: %s\n", to_write);
							Write_File(Search_Directory(d_name), 0, to_write, sz);
							fprintf(stderr,"---------------\n");
							printInode(Inode_List[Superblock.next_free_inode-1]);
							fprintf(stderr,"---------------\n/////////////////////////END///////////////////////////\n");
						}
						
					}
				}
			}else{
				printf("The file is not JPG file, skipped it.\n");
			}
			
		}
		
	}
	closedir(srcdir);
	return file_count;
}