/** * @brief Create a file * * Help from FUSE: * * This is called for creation of all non-directory, non-symlink nodes. * If the filesystem defines a create() method, then for regular files that will be called instead. * * @param path file path * @param mode creation mode * @param device device where the device will be created (contains both major and minor numbers) * @return 0 on success and <0 on error **/ static int my_mknod(const char *path, mode_t mode, dev_t device) { char modebuf[10]; mode_string(mode, modebuf); fprintf(stderr, "--->>>my_mknod: path %s, mode %s, major %d, minor %d\n", path, modebuf, (int)MAJOR(device), (int)MINOR(device)); // We check that the length of the file name is correct if(strlen(path + 1) > myFileSystem.superBlock.maxLenFileName) { return -ENAMETOOLONG; } // There exist an available inode if(myFileSystem.numFreeNodes <= 0) { return -ENOSPC; } // There is still space for a new file if(myFileSystem.directory.numFiles >= MAX_FILES_PER_DIRECTORY) { return -ENOSPC; } // The directory exists if(findFileByName(&myFileSystem, (char *)path + 1) != -1) return -EEXIST; /// Update all the information in the backup file: int idxNodoI, idxDir; if((idxNodoI = findFreeNode(&myFileSystem)) == -1 || (idxDir = findFreeFile(&myFileSystem)) == -1) { return -ENOSPC; } // Update root folder myFileSystem.directory.files[idxDir].freeFile = false; myFileSystem.directory.numFiles++; strcpy(myFileSystem.directory.files[idxDir].fileName, path + 1); myFileSystem.directory.files[idxDir].nodeIdx = idxNodoI; myFileSystem.numFreeNodes--; // Fill the fields of the new inode if(myFileSystem.nodes[idxNodoI] == NULL) myFileSystem.nodes[idxNodoI] = malloc(sizeof(NodeStruct)); myFileSystem.nodes[idxNodoI]->fileSize = 0; myFileSystem.nodes[idxNodoI]->numBlocks = 0; myFileSystem.nodes[idxNodoI]->modificationTime = time(NULL); myFileSystem.nodes[idxNodoI]->freeNode = false; reserveBlocksForNodes(&myFileSystem, myFileSystem.nodes[idxNodoI]->blocks, 0); updateDirectory(&myFileSystem); updateNode(&myFileSystem, idxNodoI, myFileSystem.nodes[idxNodoI]); return 0; }
void* my_alloc(size_t size) { /**********************************/ /* Hole einen Freispeicher Knoten */ /**********************************/ node* freeNode = findFreeNode(size); if (freeNode == 0) { char* data = get_block_from_system(); if (data == 0) return 0; /* System out of memory */ freeNode = (node*)data; freeNode->size = BLOCKSIZE - offset; freeNode->left = 0; freeNode->right = 0; freeNode->next = 0; } else removeFreeNode(freeNode); /***********************************/ /* Belege Platz im freien Speicher */ /***********************************/ /* Schritt 1: Pruefe ob Speicher uebrig bleibt */ size_t leftover = freeNode->size - size; node* nLeftover = 0; if (leftover >= offset + ALLIGN) { /* Es ist noch Platz für mindestens 8 Byte * erzeuge neuen Freispeicherknoten */ nLeftover = (node*)( ((char*)freeNode) + offset + size); nLeftover->size = (int)leftover - offset; nLeftover->left = 0; nLeftover->right = 0; insertFreeNode(nLeftover); freeNode->size = (int) size; } /* Schritt 2: Freispeicherknoten durch Belegtspeicherknoten ersetzen */ /* freeNode Pointer kann jetzt als Belegtspeicherpointer genutzt werden: */ freeNode->left = 0; freeNode->right = 0; insertToTree(freeNode, &allocTree); /* Schritt 3: Neu belegten Speicher zurueckgeben */ return ((char*)freeNode)+offset; }
int insertKey(char *key,int key_len,char *val,int val_len,int ttl) { long len_t=val_len; int count=(val_len/BLOCK_SIZE)+1; if(global_head->freeBlockNum<count||global_head->freeNodeNum==0) return -1; int already=findKey(key,key_len); if(already!=-1) removeKey(already); int index=findFreeNode(); if(index==-1) return -1; int i; int sum=0; for (i = 0; i < count; ++i) { int t=findFreeBlock(); long ttt=len_t; if(t==-1) return -1; (global_head->p[index])->block[i]=t; if(ttt>BLOCK_SIZE) ttt=BLOCK_SIZE; memcpy(AT(t),val+sum,ttt); //long tt=snprintf(AT(t),ttt+1,"%s",val+sum); sum+=ttt; len_t-=ttt; setBlockUsed(t); } (global_head->p[index])->block[i]=-1; global_head->freeBlockNum-=count; global_head->usedBlockNum+=count; global_head->freeNodeNum--; global_head->usedNodeNum++; global_head->p[index]->size=val_len; global_head->p[index]->is_free=0; global_head->p[index]->ttl=time(0)+ttl; snprintf(global_head->p[index]->key,key_len+1,"%s",key); global_head->p[index]->key_len=key_len; for (i = 0; i < count; ++i) { setBlockUsed((global_head->p[index])->block[i]); } return 1; }