Exemplo n.º 1
0
bool Utils::isFileNameValid(const QString &fileName, const QString &dir, QWidget *parent)
{
	// проверяем, что имя не пустое
	if (fileName.isEmpty())
		return false;

	// проверяем, что файл находится в заданном каталоге
	if (!fileName.startsWith(dir))
	{
		QMessageBox::warning(parent, "", "Неверный путь к файлу " + fileName + "\nВы можете работать с файлами только внутри папки " + dir);
		return false;
	}

	// проверяем относительный путь к файлу на валидность
	QString relativePath = fileName.mid(dir.size());
	if (!isFileNameValid(relativePath))
	{
		QMessageBox::warning(parent, "", "Неверный путь к файлу " + relativePath + "\nПереименуйте файлы и папки так, чтобы они "
			"состояли только из латинских букв, цифр, тире и знаков подчеркивания");
		return false;
	}

	return true;
}
/**
 * opens the given file
 */
int sfs_fopen(char *name) {
    // Check that the name is valid
    if (isFileNameValid(name) == -1) {
        return -1;
    }

    int directoryIndex = DirectoryCache_getNameIndex(directoryCache, name);
    if (directoryIndex == -1) {
        if (INodeTable_getOpenIndex(*iNodeTable) == -1 || FileDescriptorTable_getOpenIndex(*fileDescriptorTable) == -1) {
            // There are no open iNodes, so we can't procede!
            printf("INodeTable or FileDescriptorTable full!\n");
            return -1;
        }

        // The directory doesn't exist, so we will create it
        directoryIndex = DirectoryCache_getOpenIndex(*directoryCache);
        if (directoryIndex == -1) {
            printf("Directory index full!\n");
            // No more directories
            return -1;
        }
        DirectoryCache_markClosed(directoryCache, directoryIndex);

        // Copy the name
        strcpy(directoryCache->directory[directoryIndex].name, name);
        directoryCache->directory[directoryIndex].i_node_index = -1;
    }

    // Configure the iNode if Necessary
    int iNodeIndex = directoryCache->directory[directoryIndex].i_node_index;
    if (iNodeIndex == -1) {
        iNodeIndex = INodeTable_getOpenIndex(*iNodeTable);
        if (iNodeIndex == -1) {
            // No more iNodes
            return -1;
        }
        INodeTable_markClosed(iNodeTable, iNodeIndex);

        INode_new(&iNodeTable->i_node[iNodeIndex]);
        directoryCache->directory[directoryIndex].i_node_index = iNodeIndex;
    }

    // Configure the FD table
    int fdIndex = FileDescriptorTable_getIndexOfInode(*fileDescriptorTable, iNodeIndex);
    if (fdIndex == -1) {
        // Get a new fd table index
        fdIndex = FileDescriptorTable_getOpenIndex(*fileDescriptorTable);
        if (fdIndex == -1) {
            printf("FD index full!\n");
            // No more file descriptors
            return -1;
        }
        FileDescriptorTable_markInUse(fileDescriptorTable, fdIndex);

        // Point to the iNode
        fileDescriptorTable->fd[fdIndex].i_node_number = iNodeIndex;
        // Open in append mode
        fileDescriptorTable->fd[fdIndex].read_write_pointer = iNodeTable->i_node[iNodeIndex].size;
    } else {
        // If we don't do this, then the 0 index gets messed up!
        FileDescriptorTable_markInUse(fileDescriptorTable, fdIndex);
    }

    return fdIndex;
}