Beispiel #1
0
void Ext2FS::initFsRoot()
{
    readSuperBlock();

    _blockSize = 1024 << _sb->log_block_size;

    int i = (_sb->block_count / _sb->blocks_per_group) +
            ((_sb->block_count % _sb->blocks_per_group) ? 1 : 0);

    int j = (_sb->inodes_count / _sb->inodes_per_group) +
            ((_sb->inodes_count % _sb->inodes_per_group) ? 1 : 0);

    _groupNumber = (i > j) ? i : j;

    readGroupBlock();

    struct file *rootFile = getRoot();

    struct filePrivateData *data = (struct filePrivateData*)kmalloc(sizeof(struct filePrivateData));

    rootFile->name = (char*)kmalloc(strlen("/") + 1);
    strcpy(rootFile->name, "/");

    data->inum = EXT2_INUM_ROOT;
    data->inode = readInode(EXT2_INUM_ROOT);

    rootFile->content = 0;
    rootFile->privateData = (void*)data;
    rootFile->parent = rootFile;
    rootFile->leaf = getDirEntries(rootFile);
    rootFile->next = 0;
    rootFile->prev = 0;
}
Beispiel #2
0
void AddThr::setDataForSync(const QString &pth, QTreeWidgetItem *par, bool notDir)
{
    if (notDir)
        setData({pth}, {}, par, false, FILE_SYNC); //File synchronization needs only one file!
    else
    {
        QStringList dirEntries = getDirEntries(pth);
        prependPath(dirEntries, pth);
        setData(dirEntries, {}, par, false, DIR_SYNC);
    }
}
Beispiel #3
0
int main (int argc, char *argv[]) {
  FILE *fileImgPtr;
  Directory currentDir;
  OpenFileTable ofTable;
  unsigned int currentDirCluster;
  int flag = -1;
  int runLoop = 1;

  // Check for correct number of arguments.
  if (argc != 2) {
    printf("Error: Incorrect number of arguments.\n");
    printf("Expected: osmagicFAT <file image>\n");
    return 1;
  }

  // Open the file image.
  fileImgPtr = fopen(argv[1], "rb+");
    if (fileImgPtr == NULL) {
      printf("Error: could not open file image\n.");
      return 1;
    }

  // Read and store the boot sector data.
  readBootSector(fileImgPtr);

  // Set current directory to the root directory.
  currentDirCluster = fsMetadata[ROOT_CLUSTER];

  // Initialize open file table.
  ofTable.entries = (OpenFileEntry*) malloc(sizeof(OpenFileEntry));
  ofTable.size = 0;

  printf("Please input a command. Type 'quit' to quit program.\n");
  //Loop to perform commands until user exits.
  while(runLoop) {
    // Allocate memory to hold commands.
    char input[256];
    // Save the number of tokens(arguments) in each input.
    int tokCount = 0;
    char **cmds = (char**) malloc(6*sizeof(char*));
    for (int itr = 0; itr < 6; itr++)
      cmds[itr]=(char*) malloc(1*sizeof(char));
    // Initialize current directory data
    currentDir.dirEntries = (unsigned char **) malloc(sizeof(unsigned char *));
    currentDir.size = 0;

    // Read and store the current directory.
    getDirEntries(fileImgPtr, currentDirCluster, &currentDir);

    // Print prompt and get user input.
    printf("=>");
    if (fgets(input, 256, stdin) == NULL){
      printf("Error! Invalid input!\n");
      exit(-1);
    }
    tokCount = tokenize(input,cmds);

    // Compare first argument to perform command.
    if (strcmp(cmds[0], "quit") == 0){
      runLoop = 0;
    }

    else if (strcmp(cmds[0], "open") == 0) {
      if (tokCount != 3) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: open <filename> <flag>\n");
      }
      else {
        // Convert flag input string to int.
        if(strcmp(cmds[2], "r") == 0)
          flag = READ;
        else if(strcmp(cmds[2], "w") == 0)
          flag = WRITE;
        else if(strcmp(cmds[2], "rw") == 0)
          flag = READWRITE;
        else if(strcmp(cmds[2], "wr") == 0)
          flag = READWRITE;
        else {
          printf("Error: Invalid flag.\n");
          continue;
        }

        open(currentDir, fileImgPtr, &ofTable, cmds[1], flag);
      }
    }

    else if (strcmp(cmds[0], "close") == 0) {
      if(tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: close <filename>\n");
      }
      else {
        if(!closeFile(&ofTable, cmds[1]))
          printf("Error: File has not been opened.\n");
      }
    } // open

    else if (strcmp(cmds[0], "close") == 0) {

    } // close

    else if (strcmp(cmds[0], "create") == 0) {
      if (tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: create <filename>.\n");
      } // if
      else if (strcmp(cmds[1], ".") == 0
          || strcmp(cmds[1], "..") == 0) {
        printf("Error: Invalid arguments.\n");
        printf("\'%s\' is not a valid name\n", cmds[1]);
      }
      else {
        create(currentDir, currentDirCluster, fileImgPtr, cmds[1], 0);
      }
    } // create

    else if (strcmp(cmds[0], "rm") == 0) {
      if (tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: create <dirname>.\n");
      }
      // using . or .. is not allowed
      else if (strcmp(cmds[1], ".") == 0
          || strcmp(cmds[1], "..") == 0) {
        printf("Error: Invalid arguments.\n");
        printf("\'%s\' is not a valid name\n", cmds[1]);
      }
      else {
        int flag = 0;
        if(rm(currentDir, currentDirCluster, fileImgPtr, &ofTable, cmds[1], flag))
          printf("File has been removed.\n");
      }
    } // rm

    else if (strcmp(cmds[0], "size") == 0) {
      if(tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: size <filename>\n");
      }
      else {
        printf("%d bytes\n", size(currentDir, cmds[1]));
      }
    } // size

    else if (strcmp(cmds[0], "cd") == 0) {
      if(tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: cd <directory>\n");
      }
      else {
        // Find the cluster number of the desired directory and set the current
        // directory to that cluster.
        unsigned int newClusterNum = 0;
        unsigned int *newClusterPtr = &newClusterNum;
        if(cd(currentDir, cmds[1], newClusterPtr)) {
          currentDirCluster = *newClusterPtr;
        }
      }
    } // cd

    else if (strcmp(cmds[0], "ls") == 0) {
      //If only 1 argument, lists the current directory
      if(cmds[1] == NULL)
      	ls(currentDir);
      else {
    	  unsigned int newDirCluster;
    	  //newDir will be populated with entries from the specified directory
    	  Directory newDir;

    	  newDir.dirEntries = (unsigned char **) malloc(sizeof(unsigned char *));
    	  newDir.size = 0;
    	  unsigned int newClusterNum = 0;
    	  unsigned int *newClusterPtr = &newClusterNum;

    	  //Checks if specified directory exists
    	  if(cd(currentDir, cmds[1], newClusterPtr))
    	    {
    	      newDirCluster = *newClusterPtr;
    	      getDirEntries(fileImgPtr, newDirCluster, &newDir);
    	      ls(newDir);
    	    }

    	  // Free the dynamically allocated new directory.
    	  for(int i = 0; i < newDir.size; ++i)
    	    {
    	      free(newDir.dirEntries[i]);
    	    }
    	  free(newDir.dirEntries);
    	}
    } // ls

    else if (strcmp(cmds[0], "read") == 0) {
      if(tokCount != 4) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: read <filename> <position> <number of bytes>\n");
      }
      else {
        int pos, numBytes;
        pos = atoi(cmds[2]);
        numBytes = atoi(cmds[3]);
        readFile(currentDir, &ofTable, fileImgPtr, cmds[1], pos, numBytes);
      }
    } // read

    else if (strcmp(cmds[0], "write") == 0) {
      if(tokCount != 5) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: write <filename> <position> <number of bytes> <string>\n");
      }
      else {
        int pos, numBytes;
        pos = atoi(cmds[2]);
        numBytes = atoi(cmds[3]);
        writeFile(currentDir, currentDirCluster, &ofTable, fileImgPtr, cmds[1],
                  pos, numBytes, cmds[4]);
      }
    } // write

    else if (strcmp(cmds[0], "mkdir") == 0) {
      if (tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: create <dirname>.\n");
      }
      else if (strcmp(cmds[1], ".") == 0
          || strcmp(cmds[1], "..") == 0) {
        printf("Error: Invalid arguments.\n");
        printf("\'%s\' is not a valid name\n", cmds[1]);
      }
      else {
        create(currentDir, currentDirCluster, fileImgPtr, cmds[1], 1);
      }
    } // mkdir

    else if (strcmp(cmds[0], "rmdir") == 0) {
      if (tokCount != 2) {
        printf("Error: Invalid arguments.\n");
        printf("Expected: create <dirname>.\n");
      }
      else if (strcmp(cmds[1], ".") == 0
          || strcmp(cmds[1], "..") == 0) {
        printf("Error: Invalid arguments.\n");
        printf("\'%s\' is not a valid name\n", cmds[1]);
      }
      else {
        int flag = 0;
        if(rmDirectory(currentDir, currentDirCluster, fileImgPtr, cmds[1], flag))
          printf("Directory has been removed.\n");
      }
    } // rmdir

    else {
      printf("Invalid command. Please try again.\n");
    } // invalid

    // Free dynamically-allocated memory.
    for (int itr = 0; itr < 6; itr++)
      free(cmds[itr]);
    free(cmds);

    // Free the dynamically allocated current directory.
    for (int i = 0; i < currentDir.size; ++i) {
      free(currentDir.dirEntries[i]);
    }
    free(currentDir.dirEntries);

  } // while input

  //Close the file image.
  fclose(fileImgPtr);
  printf("Exiting program...\n");

  // Free the dynamically allocated open file table.
  // Free the cluster chains.
  for (int i = 0; i < ofTable.size; ++i) {
    free(ofTable.entries[i].clusterOffsets);
  }
  // Free the table.
  free(ofTable.entries);

  return 0;
}
Beispiel #4
0
struct file* Ext2FS::getFile(const char *filename)
{
    char *name;
    const char *beg_p, *end_p;
    struct file *file;
    struct filePrivateData *data;

	// TODO: Faire le reste !
	/*if(path[O] != '/')
		fp = current->pwd;
	else*/
        file = getRoot();

	beg_p = filename;

	while(*beg_p == '/')
		beg_p++;
    end_p = beg_p + 1;

	while(*beg_p != 0)
	{
        data = (struct filePrivateData*)file->privateData;

        if(!data->inode)
            data->inode = readInode(data->inum);

		if(!isDirectory(file))
			return 0;

		while(*end_p != 0 && *end_p != '/')
			end_p++;

		name = (char*)kmalloc(end_p - beg_p + 1);
		memcpy(name, beg_p, end_p - beg_p);
		name[end_p - beg_p] = 0;

		if(strcmp("..", name) == 0)
			file = file->parent;
		else if(strcmp(".", name) == 0)
			{}
		else
        {
            //Screen::getScreen().printError("Filename : %s, dir : %s, inum %u", name, file->name, data->inum);
            //Screen::getScreen().printError("Data inode size : %u", data->inode->size);
            getDirEntries(file);

            //Screen::getScreen().printError("Filename : %s, dir : %s", name, file->name);

			if(!(file = isCachedLeaf(file, name)))
            {
                kfree(name);
				return 0;
            }
		}

		beg_p = end_p;

		while(*beg_p == '/')
			beg_p++;

        end_p = beg_p + 1;

        kfree(name);
	}

	return file;
}
Beispiel #5
0
APIRET fsFindFirst(ServerData * pServerData, struct
   findfirst * pfindfirst)
{
   APIRET rc;
   CryptedVolume * pVolume = pfindfirst->pVolData->pVolume;
   CHAR szDir[CCHMAXPATH];
   SearchData * pSearchData;
   PGEALIST pgeas = 0;
   struct iso_directory_record * idr;
   IsoDirEntry* pIsoDirEntry=NULL;
   int iSize;
   int iExtent;

   pfindfirst->pSearchData = 0;

   if (VERIFYFIXED(pfindfirst->szName) ||
       verifyPathName(pfindfirst->szName))
      return ERROR_INVALID_PARAMETER;
   
   logMsg(L_DBG, "FS_FINDFIRST, curdir=%s, name=%s, "
      "iCurDirEnd=%d, fsAttr=%04hx, cMatch=%d, "
      "usLevel=%d, fsFlags=%04hx, cbData=%d",
      pfindfirst->cdfsi.cdi_curdir,
      pfindfirst->szName,
      pfindfirst->iCurDirEnd,
      pfindfirst->fsAttr,
      pfindfirst->cMatch,
      pfindfirst->usLevel,
      pfindfirst->fsFlags,
      pfindfirst->cbData);

   /* FIL_STANDARD    : 1
      FIL_QUERYEASIZE : 2
      FIL_QUERYEASFROMLIST : 3 */
   if (pfindfirst->usLevel != FIL_STANDARD &&
       pfindfirst->usLevel != FIL_QUERYEASIZE &&
       pfindfirst->usLevel != FIL_QUERYEASFROMLIST
       ) {
     logMsg(L_EVIL, "unknown FS_FINDFIRST info level: %d",
            pfindfirst->usLevel);
     return ERROR_NOT_SUPPORTED;
   }


   /* Allocate the SearchData structure. */
   pSearchData = malloc(sizeof(SearchData));
   if (!pSearchData)
      return ERROR_NOT_ENOUGH_MEMORY;
   pSearchData->pFirstInIsoDir = 0;
   pSearchData->flAttr = pfindfirst->fsAttr;

   /* Split the search specification. */
   splitPath(pfindfirst->szName + 2, szDir, pSearchData->szName);

   logMsg(L_DBG, "dir=%s, spec=%s", szDir, pSearchData->szName);

   if (!*pSearchData->szName) {
      freeSearchData(pSearchData);
      return ERROR_INVALID_PARAMETER;
   }

   if(strlen(szDir)) {
     /* Walk the directory tree to find the name in szDir. */
     pIsoDirEntry=isoQueryIsoEntryFromPath( pfindfirst->pVolData,
                              szDir);

     if(!pIsoDirEntry) {
       logMsg(L_DBG, "Entry for %s not found", szDir);
       freeSearchData(pSearchData);
       return ERROR_PATH_NOT_FOUND;
     }
     logMsg(L_DBG, "Entry for %s found.", szDir);

     iExtent=pIsoDirEntry->iExtent;
     iSize=pIsoDirEntry->iSize;

     if(!S_ISDIR(pIsoDirEntry->fstat_buf.st_mode)) {
       free(pIsoDirEntry);
       logMsg(L_DBG, "Entry is not an directory", szDir);
       freeSearchData(pSearchData);
       return ERROR_PATH_NOT_FOUND;
     }
     free(pIsoDirEntry);
   }
   else {
     iExtent=pfindfirst->pVolData->iRootExtent;
     /* Get the contents of the directory from ISO file. */
     idr=(struct iso_directory_record *)pfindfirst->pVolData->ipd.root_directory_record;
     iSize=isonum_733((unsigned char *)idr->size);
   }
   pIsoDirEntry=getDirEntries(szDir, iExtent,
               iSize, pfindfirst->pVolData, 0);

   pSearchData->pFirstInIsoDir=pIsoDirEntry;
   pSearchData->pIsoNext=pIsoDirEntry;

   if(pIsoDirEntry) {
#if 0
     logMsg(L_DBG, "Found following entries:");
     do {
       logMsg(L_DBG, "Name is: %s, extent: %d, parent extent: %d",
              pIsoDirEntry->chrName,pIsoDirEntry->iExtent,pIsoDirEntry->iParentExtent);
       pIsoDirEntry=pIsoDirEntry->pNext;
     }while(pIsoDirEntry);     
#endif
   }
   else {
     freeSearchData(pSearchData);
     return ERROR_FILE_NOT_FOUND;
   }


   /* The GEAs are stored in the exchange buffer which is
      about to be overwritten; so make a copy. */
   if (pfindfirst->usLevel == FIL_QUERYEASFROMLIST) {
     pgeas = alloca(((PGEALIST) pServerData->pData)->cbList);
     memcpy(pgeas, pServerData->pData,
            ((PGEALIST) pServerData->pData)->cbList);
   }
   
   /* Store up to the requested number of items. */
   rc = storeDirContents(
      pVolume,
      pSearchData,
      pgeas,
      (char *) pServerData->pData,
      pfindfirst->cbData,
      pfindfirst->usLevel,
      &pfindfirst->cMatch,
      pfindfirst->fsFlags);
   if (rc && (rc != ERROR_EAS_DIDNT_FIT)) {
     freeSearchData(pSearchData);
     return rc;
   }

   logMsg(L_DBG, "%d entries returned", pfindfirst->cMatch);

#if 0 /*!! ???????????*/
   if(!pfindfirst->cMatch) {
     freeSearchData(pSearchData);
     /*return ERROR_NO_MORE_FILES;*/
     return ERROR_FILE_NOT_FOUND;
   }
#endif

   pfindfirst->pSearchData = pSearchData;

   pfindfirst->pVolData->cSearches++;

   return rc;
}