int write_code(const char *name, FILE *fp, int len, int address) { char *buf; if (write_tape_header(CODE_T, name, len, address)) { return 1; } buf = (char *)malloc(len); if (!buf) { perror("4 Out of memory, 0:1"); return 1; } /* Read binary into buf */ if (fread(buf, len, 1, fp) != 1) { fprintf(stderr, "R Tape loading error, 0:1\n"); free(buf); return 1; } if (write_data_block(buf, len)) { free(buf); return 1; } free(buf); return 0; }
/* * close an open GIF image */ int gifout_close_image() { /* make sure there's an image open */ if (!image_open) return GIFLIB_ERR_NIO; /* flush any remaining code */ if (old_code != NULL_CODE) PUT_CODE(old_code); /* output end of info code */ PUT_CODE(eoi_code); /* flush any extra bits */ while (work_bits > 0) { PUT_BYTE(work_data & 0xFF); work_data >>= 8; work_bits -= 8; } /* flush any extra bytes */ if (buf_idx > 0) write_data_block(buf_idx, buf, outs); /* trailing zero byte */ putc(0, outs); /* mark image as closed */ image_open = 0; /* done! */ return GIFLIB_SUCCESS; }
int write_program(const char *basicbuf, int len, const char *name) { if (write_tape_header(PROGRAM_T, name, len, 0)) { return 1; } if (write_data_block(basicbuf, len)) { return 1; } return 0; }
int jdk_client_socket::write_string_block( const char *str ) { size_t len = strlen(str); return write_data_block(str,len); }
static void ext4_write_data_block(void *priv, u64 off, u8 *data, int len) { write_data_block(priv, off, data, len); }
Uint append_data(const List<T>& list) { return write_data_block(reinterpret_cast<const char*>(list.array().data()), sizeof(T)*list.size(), list.name(), list.size(), 1, class_name<T>()); }
Uint append_data(const Table<T>& table) { return write_data_block(reinterpret_cast<const char*>(table.array().data()), sizeof(T)*table.row_size()*table.size(), table.name(), table.size(), table.row_size(), class_name<T>()); }
/** * write buf characters into disk */ int sfs_fwrite(int fileID, const char *buf, int length){ // Get the iNode of the desired file FileDescriptor* fileDescriptor = &fileDescriptorTable->fd[fileID]; INode* fileINode = &iNodeTable->i_node[fileDescriptor->i_node_number]; // Keep track of how much we have written, and how much we must write int totalBytesWritten = 0; int totalAmountLeftToWrite = length; // We might have to access the indirect block IndirectBlockPointer indirectBlock = {}; // Create an empty buffer void* newBuffer = malloc(DISK_BLOCK_SIZE); memset(newBuffer, '\0', DISK_BLOCK_SIZE); /* * Set up the indirect block */ // Initialize indirect buffer if necessary if (totalAmountLeftToWrite > 0 && fileINode->ind_pointer <= 0) { // Create an index for the ind pointer fileINode->ind_pointer = FreeBitMap_getFreeBit(*freeBitMap); if (fileINode->ind_pointer < 0) { // No space left! return -1; } // mark the bit unfree FreeBitMap_markBitUnfree(freeBitMap, fileINode->ind_pointer); } else { // There already is an ind pointer, so we load from disk read_data_block(fileINode->ind_pointer, &indirectBlock); } // Begin write while (totalAmountLeftToWrite > 0) { // Empty the contents of newBuffer memset(newBuffer, '\0', DISK_BLOCK_SIZE); int currentBlockIndex = fileDescriptor->read_write_pointer / DISK_BLOCK_SIZE; int startingIndexOfBlock = fileDescriptor->read_write_pointer % DISK_BLOCK_SIZE; int amountToWrite; if (totalAmountLeftToWrite > (DISK_BLOCK_SIZE - startingIndexOfBlock)) { amountToWrite = DISK_BLOCK_SIZE - startingIndexOfBlock; } else { amountToWrite = totalAmountLeftToWrite; } /* * Handle getting the disk index for the data */ int blockIsNew = 0; int diskIndex; if (isIndexInIndirectBlock(currentBlockIndex)) { // We are using the indirect block diskIndex = indirectBlock.block[indirectBlockIndex(currentBlockIndex)]; if (diskIndex <= 0) { blockIsNew = 1; // Get a new bitmap index diskIndex = FreeBitMap_getFreeBit(*freeBitMap); if (diskIndex == -1) { // Disk is full! break; } // Mark it unfree FreeBitMap_markBitUnfree(freeBitMap, diskIndex); // Save it to the indirect block indirectBlock.block[indirectBlockIndex(currentBlockIndex)] = diskIndex; } } else { // We are using the iNode Directly diskIndex = fileINode->pointer[currentBlockIndex]; if (diskIndex < 0) { blockIsNew = 1; // Get a new bitmap index diskIndex = FreeBitMap_getFreeBit(*freeBitMap); if (diskIndex == -1) { // Disk is full! break; } // Mark it unfree FreeBitMap_markBitUnfree(freeBitMap, diskIndex); fileINode->pointer[currentBlockIndex] = diskIndex; } } // If the block is already partially full, then we need to load the old data if (!blockIsNew) { read_data_block(diskIndex, newBuffer); } // Copy the desired amount of data onto it memcpy(newBuffer + startingIndexOfBlock, buf, (size_t) amountToWrite); write_data_block(diskIndex, newBuffer); // Save how much we have written to the file pointer fileDescriptor->read_write_pointer += amountToWrite; // Track how much we have written. This will be returned to user totalBytesWritten += amountToWrite; totalAmountLeftToWrite -= amountToWrite; // Increment the buffer buf += amountToWrite; // Record file size change if (fileDescriptor->read_write_pointer > fileINode->size) { fileINode->size = fileDescriptor->read_write_pointer; } } // Write the data block to disk if (fileINode->ind_pointer > -1) { write_data_block(fileINode->ind_pointer, &indirectBlock); } // Free the new buffer free(newBuffer); // Save our cached version of the file system save_local_file_system_to_disk(freeBitMap, iNodeTable, directoryCache); return totalBytesWritten; }