/* scroll everything up a line */ void conio_scroll() { int i; switch (conio_ttymode) { case CONIO_TTY_PVR: memmove(conio_virtscr, conio_virtscr[1], (CONIO_NUM_ROWS - 1) * CONIO_NUM_COLS); for (i = 0; i < CONIO_NUM_COLS; i++) conio_virtscr[CONIO_NUM_ROWS - 1][i] = ' '; conio_cursor.row--; break; case CONIO_TTY_SERIAL: scif_write_buffer((const uint8 *)"\x1b[M", 3, 1); break; case CONIO_TTY_STDIO: fs_write(conio_serial_fd, (void *)"\x1b[M", 3); break; } }
uint8_t do_write(uint64_t lv,uint32_t ts,char *ptr) { uint32_t inode,indx,opflag; uint64_t chunkid; EAT(ptr,lv,'('); GETU32(inode,ptr); EAT(ptr,lv,','); GETU32(indx,ptr); if (*ptr==',') { EAT(ptr,lv,','); GETU32(opflag,ptr); } else { opflag=1; } EAT(ptr,lv,')'); EAT(ptr,lv,':'); GETU64(chunkid,ptr); return fs_write(ts,inode,indx,opflag,chunkid); }
/* clear the screen */ void conio_clear() { int row, col; switch (conio_ttymode) { case CONIO_TTY_PVR: /* fill screen with spaces */ for (row = 0; row < CONIO_NUM_ROWS; row++) for (col = 0; col < CONIO_NUM_COLS; col++) conio_virtscr[row][col] = ' '; break; case CONIO_TTY_SERIAL: scif_write_buffer((uint8 *)"\x1b[2J", 4, 1); break; case CONIO_TTY_STDIO: fs_write(conio_serial_fd, (void *)"\x1b[2J", 4); break; } }
/* move the cursor ahead, scroll if we need to */ void conio_advance_cursor() { switch (conio_ttymode) { case CONIO_TTY_PVR: conio_cursor.col++; if (conio_cursor.col >= CONIO_NUM_COLS) { conio_cursor.col = 0; conio_cursor.row++; if (conio_cursor.row >= CONIO_NUM_ROWS) conio_scroll(); } break; case CONIO_TTY_SERIAL: scif_write_buffer((const uint8 *)"\x1b[1C", 4, 1); break; case CONIO_TTY_STDIO: fs_write(conio_serial_fd, (void *)"\x1b[1C", 4); break; } }
/* Copies a file from 'src' to 'dst'. The amount of the file actually copied without error is returned. */ ssize_t fs_copy(const char * src, const char * dst) { char *buff; ssize_t left, total, r; file_t fs, fd; /* Try to open both files */ fs = fs_open(src, O_RDONLY); if (fs == FILEHND_INVALID) { return -1; } fd = fs_open(dst, O_WRONLY | O_TRUNC); if (fd == FILEHND_INVALID) { fs_close(fs); return -2; } /* Get the source size */ left = fs_total(fs); total = 0; /* Allocate a buffer */ buff = malloc(65536); /* Do the copy */ while (left > 0) { r = fs_read(fs, buff, 65536); if (r <= 0) break; fs_write(fd, buff, r); left -= r; total += r; } /* Free the buffer */ free(buff); /* Close both files */ fs_close(fs); fs_close(fd); return total; }
static int test_read_timeout(struct harness_t *harness_p) { struct fs_file_t file; uint8_t byte; uint8_t buf[516]; int i, j; BTASSERT(fs_open(&file, "foo.txt", FS_WRITE | FS_CREAT | FS_TRUNC) == 0); byte = 0; for (i = 0; i < 130; i++) { BTASSERT(fs_write(&file, &byte, 1) == 1); byte++; } BTASSERT(fs_close(&file) == 0); /* Input read request packet. */ socket_stub_input(0, "\x00""\x01""foo.txt""\x00""octet""\x00", 16); /* Read the packet three times (first transmission + two retransmissions). */ for (i = 0; i < 3; i++) { socket_stub_output(&buf[0], 134); BTASSERT(buf[0] == 0); BTASSERT(buf[1] == 3); BTASSERT(buf[2] == 0); BTASSERT(buf[3] == 1); byte = 0; for (j = 0; j < 130; j++) { BTASSERT(buf[4 + j] == byte); byte++; } } thrd_sleep_ms(10); return (0); }
/* move the cursor */ void conio_gotoxy(int x, int y) { switch (conio_ttymode) { case CONIO_TTY_PVR: conio_cursor.col = x; conio_cursor.row = y; break; case CONIO_TTY_SERIAL: { char tmp[256]; sprintf(tmp, "\x1b[%d;%df", x, y); scif_write_buffer(tmp, strlen(tmp), 1); break; } case CONIO_TTY_STDIO: { char tmp[256]; sprintf(tmp, "\x1b[%d;%df", x, y); fs_write(conio_serial_fd, tmp, strlen(tmp)); break; } } }
static int g_write (lua_State *L, int f, int arg) { int nargs = lua_gettop(L) - 1; int status = 1; for (; nargs--; arg++) { #if 0 if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ status = status && fs_printf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; } else #endif { size_t l; const char *s = luaL_checklstring(L, arg, &l); status = status && (fs_write(f, s, l) == l); } } return pushresult(L, status, NULL); }
/* * Write out a multiline string to a file with appropriately converted * linefeed characters. */ static int write_lines(const char *start, int length, fs_file fh) { #ifdef _WIN32 static const char crlf[] = "\r\n"; #else static const char crlf[] = "\n"; #endif int total_written = 0; int datalen; int written; char *lf; while (total_written < length) { lf = strchr(start, '\n'); datalen = lf ? (int) (lf - start) : length - total_written; written = fs_write(start, 1, datalen, fh); if (written < 0) break; total_written += written; if (written < datalen) break; if (lf) { if (fs_puts(crlf, fh) < 0) break; total_written += 1; start = lf + 1; } } return total_written; }
static int fs_sis_write(struct fs_file *_file, const void *data, size_t size) { struct sis_fs_file *file = (struct sis_fs_file *)_file; if (file->hash_input != NULL && stream_cmp_block(file->hash_input, data, size) && i_stream_is_eof(file->hash_input)) { /* try to use existing file */ if (fs_sis_try_link(file)) return 0; } if (fs_write(file->super, data, size) < 0) { fs_sis_file_copy_error(file); return -1; } T_BEGIN { fs_sis_replace_hash_file(file); } T_END; return 0; }
static void queue(unsigned src, unsigned buf) { unsigned frame_count = BUFFER_SIZE / (ima_info.channel_count * sizeof (int16_t)); size_t size; if (frame_count > ima_info.frame_count - offset) frame_count = ima_info.frame_count - offset; ima_decode(buffer, offset, frame_count, ima_info.blocks, ima_info.channel_count, &ima_state); offset += frame_count; size = frame_count * ima_info.channel_count * sizeof (int16_t); if (size > 0) { alBufferData( buf, AL_FORMAT_STEREO16, buffer, size, ima_info.sample_rate); alSourceQueueBuffers(src, 1, &buf); if (wav_fd > 0) fs_write(wav_fd, buffer, size); } }
int fsutil_write_file(const char *path, const void *data, uint32_t len) { struct fs_file *file; int rc; rc = fs_open(path, FS_ACCESS_WRITE, &file); if (rc != 0) { goto done; } rc = fs_write(file, data, len); if (rc != 0) { goto done; } rc = 0; done: fs_close(file); return rc; }
/* Kernel debug logging facility */ void dbglog(int level, const char *fmt, ...) { va_list args; /* If this log level is blocked out, don't even bother */ if(level > dbglog_level) return; /* We only try to lock if the message isn't urgent */ if(level >= DBG_ERROR && !irq_inside_int()) spinlock_lock(&mutex); va_start(args, fmt); (void)vsprintf(printf_buf, fmt, args); va_end(args); if(irq_inside_int()) dbgio_write_str(printf_buf); else fs_write(1, printf_buf, strlen(printf_buf)); if(level >= DBG_ERROR && !irq_inside_int()) spinlock_unlock(&mutex); }
/* move the cursor back, don't scroll (we can't) */ void conio_deadvance_cursor() { switch (conio_ttymode) { case CONIO_TTY_PVR: conio_cursor.col--; if (conio_cursor.col < 0) { if (conio_cursor.row != 0) { conio_cursor.col = CONIO_NUM_COLS - 1; conio_cursor.row--; } else { conio_cursor.col = 0; } } break; case CONIO_TTY_SERIAL: scif_write(8); scif_write(32); scif_write(8); break; case CONIO_TTY_STDIO: fs_write(conio_serial_fd, (void *)"\x8\x20\x8", 3); break; } }
/** * This function handles the write test eeprom command * * \param rq request message * \param rs response message */ void bmc_oem_write_test_eeprom(ipmbMSG_t *rq, ipmbMSG_t *rs) { rs->data_len = 1; /* Check if request length is valid */ if (rq->data_len > 4) { rs->data[0] = CC_REQ_DATALEN_INVALID; return; } /* check for valid iana and fill iana in response */ if (check_iana(rq, rs)) { return; } rs->data[0] = CC_NODE_BUSY; /* write test byte to end of eeprom */ if (fs_write(FILE_TEST, 0, 1, &rq->data[3]) == E_OK) { rs->data[0] = CC_COMPLETED_NORMALLY; } }
void netcfg_vmuify(const char *filename_in, const char *filename_out) { int fd, pkg_size; uint8 *buf; uint8 *pkg_out; vmu_pkg_t pkg; dbgp("Opening source file\n"); fd = fs_open(filename_in, O_RDONLY); buf = (uint8 *) malloc(fs_total(fd)); fs_read(fd, buf, fs_total(fd)); dbgp("Read %i bytes\n", fs_total(fd)); strcpy(pkg.desc_short, "KallistiOS 1.3"); strcpy(pkg.desc_long, "KOS Network Settings"); strcpy(pkg.app_id, "KOS"); pkg.icon_cnt = 1; pkg.icon_anim_speed = 1; memcpy(&pkg.icon_pal[0], netcfg_icon, 32); pkg.icon_data = netcfg_icon + 32; pkg.eyecatch_type = VMUPKG_EC_NONE; pkg.data_len = fs_total(fd); pkg.data = buf; dbgp("Building package\n"); vmu_pkg_build(&pkg, &pkg_out, &pkg_size); fs_close(fd); dbgp("Closing source file\n"); dbgp("Opening output file\n"); fd = fs_open(filename_out, O_WRONLY); dbgp("Writing..\n"); fs_write(fd, pkg_out, pkg_size); dbgp("Closing output file\n"); fs_close(fd); free(buf); dbgp("VMUification complete\n"); }
int builtin_bzip2_cmd(int argc, char *argv[]) { if(argc < 3) { ds_printf("Usage: %s option infile outfile\n" "Options: \n" " -9 -Compress with mode from 0 to 9\n" " -d -Decompress\n\n" "Examples: %s -9 /cd/file.dat /ram/file.dat.bz2\n" " %s -d /ram/file.dat.bz2 /ram/file.dat\n", argv[0], argv[0], argv[0]); return CMD_NO_ARG; } char dst[MAX_FN_LEN]; char buff[512]; int len = 0; file_t fd; BZFILE *zfd; if(argc < 4) { char tmp[MAX_FN_LEN]; relativeFilePath_wb(tmp, argv[2], strrchr(argv[2], '/')); sprintf(dst, "%s.bz2", tmp); } else { strcpy(dst, argv[3]); } if(!strcmp("-d", argv[1])) { ds_printf("DS_PROCESS: Decompressing '%s' ...\n", argv[2]); zfd = BZ2_bzopen(argv[2], "rb"); if(zfd == NULL) { ds_printf("DS_ERROR: Can't create file: %s\n", argv[2]); return CMD_ERROR; } fd = fs_open(dst, O_WRONLY | O_CREAT); if(fd < 0) { ds_printf("DS_ERROR: Can't open file: %s\n", dst); BZ2_bzclose(zfd); return CMD_ERROR; } while((len = BZ2_bzread(zfd, buff, sizeof(buff))) > 0) { fs_write(fd, buff, len); } BZ2_bzclose(zfd); fs_close(fd); ds_printf("DS_OK: File decompressed."); } else if(argv[1][1] >= '0' && argv[1][1] <= '9') { char mode[3]; sprintf(mode, "wb%c", argv[1][1]); ds_printf("DS_PROCESS: Compressing '%s' with mode '%c' ...\n", argv[2], argv[1][1]); zfd = BZ2_bzopen(dst, mode); if(zfd == NULL) { ds_printf("DS_ERROR: Can't create file: %s\n", dst); return CMD_ERROR; } fd = fs_open(argv[2], O_RDONLY); if(fd < 0) { ds_printf("DS_ERROR: Can't open file: %s\n", argv[2]); BZ2_bzclose(zfd); return CMD_ERROR; } while((len = fs_read(fd, buff, sizeof(buff))) > 0) { if(!BZ2_bzwrite(zfd, buff, len)) { ds_printf("DS_ERROR: Error writing to file: %s\n", dst); BZ2_bzclose(zfd); fs_close(fd); return CMD_ERROR; } } BZ2_bzclose(zfd); fs_close(fd); ds_printf("DS_OK: File compressed."); } else { return CMD_NO_ARG; } return CMD_OK; }
/* * tftp_server_process * @data: TFTP server data for which a request is needed to be processed. * @resume_status: Resumption status. * This is callback function to process a request for a TFTP server. */ static void tftp_server_process(void *data, int32_t resume_status) { TFTP_SERVER *tftp_server = (TFTP_SERVER *)data; FS_BUFFER_LIST *rx_buffer; uint32_t data_len; int32_t status = SUCCESS, received; uint16_t opcode, block; uint8_t last_block = FALSE, data_buffer[TFTP_BUFFER_SIZE]; FD fd; /* Remove some compiler warnings. */ UNUSED_PARAM(resume_status); SYS_LOG_FUNCTION_ENTRY(TFTPS); /* If the timeout was enabled and it has now occurred. */ if ((tftp_server->port_suspend.timeout_enabled == TRUE) && (INT32CMP(current_system_tick(), tftp_server->port_suspend.timeout) >= 0)) { /* If we have a connection. */ if (tftp_server->fd != NULL) { /* Close the file descriptor. */ fs_close(&tftp_server->fd); SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "file transfered interrupted", ""); } /* Reset the current timeout. */ tftp_server->port_suspend.timeout = MAX_WAIT; tftp_server->port_suspend.timeout_enabled = FALSE; } /* Receive incoming data from the UDP port. */ received = fs_read(&tftp_server->port, (uint8_t *)&rx_buffer, sizeof(FS_BUFFER_LIST)); /* If some data was received. */ if (received >= (int32_t)sizeof(uint32_t)) { /* Acquire lock for the buffer file descriptor. */ ASSERT(fd_get_lock(rx_buffer->fd) != SUCCESS); /* Pull the opcode from the buffer. */ status = fs_buffer_list_pull(rx_buffer, &opcode, sizeof(uint16_t), (FS_BUFFER_PACKED)); if (status == SUCCESS) { SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_DEBUG, "received a request 0x%04X", opcode); /* Process the TFTP opcode. */ switch (opcode) { /* Read request. */ case TFTP_OP_READ_REQ: /* Write request. */ case TFTP_OP_WRITE_REQ: /* If we can establish connection with a new client. */ if (tftp_server->fd == NULL) { /* Pull the file name from the frame. */ for (data_len = 0; ((status == SUCCESS) && (data_len < TFTP_BUFFER_SIZE)); data_len++) { /* Pull the filename from the buffer. */ status = fs_buffer_list_pull(rx_buffer, &data_buffer[data_len], sizeof(uint8_t), 0); /* If filename was terminated. */ if (data_buffer[data_len] == '\0') { break; } } /* If file name was successfully parsed. */ if ((status == SUCCESS) && (data_len < TFTP_BUFFER_SIZE)) { /* Release lock for buffer file descriptor. */ fd_release_lock(rx_buffer->fd); /* If read was requested. */ if (opcode == TFTP_OP_READ_REQ) { SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "read requested for \"%s\"", data_buffer); /* Open the required file. */ fd = fs_open((char *)data_buffer, (FS_READ)); } else { SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "write requested for \"%s\"", data_buffer); /* Open the required file. */ fd = fs_open((char *)data_buffer, (FS_WRITE | FS_CREATE)); } /* If file was not found or was not opened. */ if (fd == NULL) { /* A file system error was detected. */ status = TFTP_ERROR_FS; SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_ERROR, "error opening file \"%s\"", data_buffer); } else { /* Save the opened file descriptor. */ tftp_server->fd = fd; /* Save the client address. */ memcpy(&tftp_server->client_address, &tftp_server->port.last_datagram_address, sizeof(SOCKET_ADDRESS)); /* Initialize session variables. */ tftp_server->block_num = 0; tftp_server->tx_block_len = 0; tftp_server->last_block = FALSE; } /* Acquire lock for the buffer file descriptor. */ ASSERT(fd_get_lock(rx_buffer->fd) != SUCCESS); } else { /* File name was too long. */ status = TFTP_LONG_FILENAME; } } else { /* No more clients can be connected. */ status = TFTP_ERROR_EXHAUSTED; } break; /* Data request. */ case TFTP_OP_DATA: /* If we have a open file and this is the client who opened it. */ if ((tftp_server->fd != NULL) && (memcmp(&tftp_server->client_address, &tftp_server->port.last_datagram_address, sizeof(SOCKET_ADDRESS)) == 0)) { /* Pull the block number from the buffer. */ status = fs_buffer_list_pull(rx_buffer, &block, sizeof(uint16_t), (FS_BUFFER_PACKED)); if (status == SUCCESS) { /* If this is the last block. */ if (rx_buffer->total_length != TFTP_BLOCK_SIZE) { /* Mark this as last block. */ last_block = TRUE; } /* If client skipped a block. */ if (block > (tftp_server->block_num + 1)) { /* This is an unknown block. */ status = TFTP_OUTOFBOUND_BLOCK; } /* If this is the anticipated block. */ else if (block == (tftp_server->block_num + 1)) { /* While we have some data to write. */ while ((status == SUCCESS) && (rx_buffer->total_length > 0)) { /* Pull some data from the buffer. */ data_len = ((rx_buffer->total_length > TFTP_BUFFER_SIZE) ? TFTP_BUFFER_SIZE : rx_buffer->total_length); status = fs_buffer_list_pull(rx_buffer, data_buffer, data_len, 0); if (status == SUCCESS) { /* Release lock for buffer file descriptor. */ fd_release_lock(rx_buffer->fd); /* Write a chuck on the file. */ if (fs_write(tftp_server->fd, data_buffer, (int32_t)data_len) <= 0) { /* File error. */ status = TFTP_ERROR_FS; } /* Acquire lock for the buffer file descriptor. */ ASSERT(fd_get_lock(rx_buffer->fd) != SUCCESS); } } if (status == SUCCESS) { /* Save the current block number. */ tftp_server->block_num = block; } } /* In case this is a retransmission, just send an ACK. */ /* If this was the last block. */ if (last_block == TRUE) { /* Close the file descriptor. */ fs_close(&tftp_server->fd); /* Stop the timer. */ tftp_server->port_suspend.timeout = MAX_WAIT; tftp_server->port_suspend.timeout_enabled = FALSE; SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "file transfered successfully", ""); } } } else { /* Transaction ID is not known. */ status = TFTP_UNKNOWN_TID; } break; /* ACK was received. */ case TFTP_OP_ACK: /* If we have a session open for this client. */ if ((tftp_server->fd != NULL) && (memcmp(&tftp_server->client_address, &tftp_server->port.last_datagram_address, sizeof(SOCKET_ADDRESS)) == 0)) { /* Pull the block number from the buffer. */ status = fs_buffer_list_pull(rx_buffer, &block, sizeof(uint16_t), (FS_BUFFER_PACKED)); if (status == SUCCESS) { /* If other side has ACKed the block we sent earlier. */ if (block == tftp_server->block_num) { /* If we just received ACK for the last block. */ if (tftp_server->last_block == TRUE) { /* Close the file descriptor. */ fs_close(&tftp_server->fd); /* Stop the timer. */ tftp_server->port_suspend.timeout = MAX_WAIT; tftp_server->port_suspend.timeout_enabled = FALSE; SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "file transfered successfully", ""); /* Nothing to be sent in reply. */ status = TFTP_FRAME_DROP; } } else if (block > tftp_server->block_num) { /* This is an unknown block. */ status = TFTP_OUTOFBOUND_BLOCK; } else { /* Lets send the old frame again. */ /* TODO seek the file back 1 block. */ /* For now send an error. */ status = TFTP_ERROR_FS; } } } else { /* Transaction ID is not known. */ status = TFTP_UNKNOWN_TID; } break; case TFTP_OP_ERR: /* If we have a session open for this client. */ if ((tftp_server->fd != NULL) && (memcmp(&tftp_server->client_address, &tftp_server->port.last_datagram_address, sizeof(SOCKET_ADDRESS)) == 0)) { /* Close the file descriptor. */ fs_close(&tftp_server->fd); /* Stop the timer. */ tftp_server->port_suspend.timeout = MAX_WAIT; tftp_server->port_suspend.timeout_enabled = FALSE; SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "file transfered interrupted", ""); } /* Lets drop this frame. */ status = TFTP_FRAME_DROP; break; /* Unknown opcode. */ default: SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_ERROR, "unknown or unsupported opcode 0x%04X", opcode); /* This opcode is not supported. */ status = TFTP_NOT_SUPPORTED; break; } /* Discard any data in the buffer. */ fs_buffer_list_pull(rx_buffer, NULL, rx_buffer->total_length, 0); /* If we have a session open for this client and we just received some data from it. */ if ((tftp_server->fd != NULL) && (memcmp(&tftp_server->client_address, &tftp_server->port.last_datagram_address, sizeof(SOCKET_ADDRESS)) == 0)) { /* Reset the timer to terminate this connection. */ tftp_server->port_suspend.timeout = current_system_tick() + TFTP_CLI_TIMEOUT; tftp_server->port_suspend.timeout_enabled = TRUE; } /* If request was processed successfully. */ if (status == SUCCESS) { /* Process the request. */ switch (opcode) { /* We successfully processed a write request. */ case TFTP_OP_WRITE_REQ: case TFTP_OP_DATA: /* Push the acknowledgment opcode. */ opcode = TFTP_OP_ACK; break; /* We successfully processed a read request. */ case TFTP_OP_READ_REQ: case TFTP_OP_ACK: /* We will send file content in reply. */ opcode = TFTP_OP_DATA; /* Send the next block. */ tftp_server->block_num++; break; } /* Push required opcode on the buffer. */ status = fs_buffer_list_push_offset(rx_buffer, &opcode, 2, 0, (FS_BUFFER_PACKED | FS_BUFFER_TAIL)); if (status == SUCCESS) { /* Push block we are transmitting or acknowledging. */ status = fs_buffer_list_push_offset(rx_buffer, &tftp_server->block_num, 2, 0, (FS_BUFFER_PACKED | FS_BUFFER_TAIL)); } /* If we need to add data. */ if (opcode == TFTP_OP_DATA) { /* Add data block. */ for (data_len = 0; ((status == SUCCESS) && (data_len < TFTP_BLOCK_SIZE));) { /* Calculate the number of bytes we need to read. */ status = (int32_t)(((data_len + TFTP_BUFFER_SIZE) > TFTP_BLOCK_SIZE) ? (TFTP_BLOCK_SIZE - data_len) : (TFTP_BUFFER_SIZE)); /* Read a chunk of buffer. */ status = fs_read(tftp_server->fd, data_buffer, status); /* If we did read some data. */ if (status > 0) { /* Update the data length. */ data_len += (uint32_t)status; /* Push the read buffer on the buffer. */ status = fs_buffer_list_push(rx_buffer, data_buffer, (uint32_t)status, (FS_BUFFER_TAIL)); } else { /* We must be at the end of the file. */ break; } } /* Save the number of bytes we sent in this block. */ tftp_server->tx_block_len = (uint16_t)data_len; /* If this was the last chunk. */ if (data_len != TFTP_BLOCK_SIZE) { /* We will be sending the last block. */ tftp_server->last_block = TRUE; } } } /* If we are not dropping this frame. */ else if (status != TFTP_FRAME_DROP) { /* If we have a session open for this client. */ if ((tftp_server->fd != NULL) && (memcmp(&tftp_server->client_address, &tftp_server->port.last_datagram_address, sizeof(SOCKET_ADDRESS)) == 0)) { /* Close the file descriptor. */ fs_close(&tftp_server->fd); /* Stop the timer. */ tftp_server->port_suspend.timeout = MAX_WAIT; tftp_server->port_suspend.timeout_enabled = FALSE; SYS_LOG_FUNCTION_MSG(TFTPS, SYS_LOG_INFO, "file transfered interrupted", ""); } /* Push the error opcode. */ opcode = TFTP_OP_ERR; /* Push error response on the buffer. */ if (fs_buffer_list_push_offset(rx_buffer, &opcode, 2, 0, (FS_BUFFER_PACKED | FS_BUFFER_TAIL)) == SUCCESS) { switch (status) { /* If client is not connected or we received an out of * bound frame. */ case TFTP_UNKNOWN_TID: case TFTP_OUTOFBOUND_BLOCK: /* Send transaction ID error. */ block = TFTP_ERROR_TID; break; /* Error is not mapped on the TFTP error. */ default: /* Push the error code. */ block = TFTP_ERROR_GEN; break; } /* Push error code on the buffer. */ if (fs_buffer_list_push(rx_buffer, &block, 2, (FS_BUFFER_PACKED | FS_BUFFER_TAIL)) == SUCCESS) { /* Push the required error message. */ switch (status) { /* TFTP command not supported. */ case TFTP_NOT_SUPPORTED: /* Set error that opcode is not supported. */ status = fs_buffer_list_push(rx_buffer, TFTP_ERRMSG_NOT_SUPPORTED, sizeof(TFTP_ERRMSG_NOT_SUPPORTED), (FS_BUFFER_TAIL)); break; /* Filename was too long. */ case TFTP_LONG_FILENAME: /* Send error that file name is too long. */ status = fs_buffer_list_push(rx_buffer, TFTP_ERRMSG_FILENAME, sizeof(TFTP_ERRMSG_FILENAME), (FS_BUFFER_TAIL)); break; /* Filename was not opened. */ case TFTP_ERROR_FS: /* Send error that file system did not open the file. */ status = fs_buffer_list_push(rx_buffer, TFTP_ERRMSG_FS, sizeof(TFTP_ERRMSG_FS), (FS_BUFFER_TAIL)); break; /* If no more clients can be connected. */ case TFTP_ERROR_EXHAUSTED: /* Send error that connections are exhausted. */ status = fs_buffer_list_push(rx_buffer, TFTP_ERRMSG_EXHAUSTED, sizeof(TFTP_ERRMSG_EXHAUSTED), (FS_BUFFER_TAIL)); break; /* If client is not connected. */ case TFTP_UNKNOWN_TID: /* Send error that client's transaction ID was not resolved. */ status = fs_buffer_list_push(rx_buffer, TFTP_ERRMSG_TID, sizeof(TFTP_ERRMSG_TID), (FS_BUFFER_TAIL)); break; /* Out of bound frame we received. */ case TFTP_OUTOFBOUND_BLOCK: /* Send error that an out of bound block was received. */ status = fs_buffer_list_push(rx_buffer, TFTP_ERRMSG_BLOCK, sizeof(TFTP_ERRMSG_BLOCK), (FS_BUFFER_TAIL)); break; /* Unknown error message. */ default: /* Error message not known. */ status = fs_buffer_list_push(rx_buffer, "", 1, (FS_BUFFER_TAIL)); break; } } } } } /* If we are not sending a reply. */ if (status != SUCCESS) { /* Free the buffer. */ fs_buffer_add_list_list(rx_buffer, FS_LIST_FREE, FS_BUFFER_ACTIVE); } /* Release lock for buffer file descriptor. */ fd_release_lock(rx_buffer->fd); if (status == SUCCESS) { /* Send this buffer back to the host. */ tftp_server->port.destination_address = tftp_server->port.last_datagram_address; tftp_server->port.destination_address.local_ip = IPV4_ADDR_UNSPEC; ipv4_get_device_address(rx_buffer->fd, &tftp_server->port.destination_address.local_ip, NULL); /* Send received data back on the UDP port. */ received = fs_write(&tftp_server->port, (uint8_t *)rx_buffer, sizeof(FS_BUFFER_LIST)); } } SYS_LOG_FUNCTION_EXIT_STATUS(TFTPS, status); } /* tftp_server_process */
static int test_read(struct harness_t *harness_p) { struct fs_file_t file; uint8_t byte; uint8_t buf[516]; int i; BTASSERT(fs_open(&file, "foo.txt", FS_WRITE | FS_CREAT | FS_TRUNC) == 0); byte = 0; for (i = 0; i < 1201; i++) { BTASSERT(fs_write(&file, &byte, 1) == 1); byte++; } BTASSERT(fs_close(&file) == 0); /* Input read request packet. */ socket_stub_input(0, "\x00""\x01""foo.txt""\x00""octet""\x00", 16); /* Wait for the first data packet (bytes 0..511). */ socket_stub_output(&buf[0], 516); BTASSERT(buf[0] == 0); BTASSERT(buf[1] == 3); BTASSERT(buf[2] == 0); BTASSERT(buf[3] == 1); byte = 0; for (i = 0; i < 512; i++) { BTASSERT(buf[4 + i] == byte); byte++; } /* Input bad acknowlegement packet. */ socket_stub_input(1, "\x00""\x04""\x00""\x02", 4); /* Input acknowlegement packet. */ socket_stub_input(1, "\x00""\x04""\x00""\x01", 4); /* Wait for the second data packet (bytes 512..1023). */ socket_stub_output(&buf[0], 516); BTASSERT(buf[0] == 0); BTASSERT(buf[1] == 3); BTASSERT(buf[2] == 0); BTASSERT(buf[3] == 2); for (i = 0; i < 512; i++) { BTASSERT(buf[4 + i] == byte); byte++; } /* Input acknowlegement packet. */ socket_stub_input(1, "\x00""\x04""\x00""\x02", 4); /* Wait for the second data packet (bytes 1024..1200). */ socket_stub_output(&buf[0], 181); BTASSERT(buf[0] == 0); BTASSERT(buf[1] == 3); BTASSERT(buf[2] == 0); BTASSERT(buf[3] == 3); for (i = 0; i < 177; i++) { BTASSERT(buf[4 + i] == byte); byte++; } /* Input last acknowlegement packet. */ socket_stub_input(1, "\x00""\x04""\x00""\x03", 4); thrd_sleep_ms(10); return (0); }
void fs_remove(const char *key) { fs_write(key, NULL, 0); }
int writeIconGif(char *fn, VirtualFile *src) { uint8 hdr[13]={0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x20, 0x00, 0x20, 0x00, 0xB3, 0x00, 0x00}; uint8 ani[19]={0x21, 0xF9, 0x04, 0x00, 0x33, 0x00, 0x07, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00, 0x04}; uint8 web[19]={0x21, 0xFF, 0x0B, 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30, 0x03, 0x01, 0x00, 0x00, 0x00}; char fname[17]; uint8 x, y, z; uint8 tmp[1024]; uint8 *icon; uint16 *pal; file_t f; GifSave *gif; DreamcastFile *df; printf(" [+] Reading %s ... ", src->getFileName()); if (src->readData() != 0) { printf(" [-] Source file %s could not be readed.\n", src->getFileName()); return(-1); } printf("OK!\n"); df=src->getDCFile(); printFileInfo(df); if (fn == NULL) { sprintf(fname, "%s.GIF", df->getName()); } else { strcpy(fname, fn); } printf(" [+] Writing %s ... ", fname); if (!(f=fs_open(fname, "wb"))) { printf("ERROR: Can't open %s!\n", fname); printf(" [-] Destination file %s could not be written.\n", fname); return(-2); } fs_write(f, hdr, 13); pal=df->getIconPalette(); for (x=0; x<16; x++) { tmp[x*3]=((pal[x] & 0x0f00) >> 8) *17; tmp[x*3 + 1]=((pal[x] & 0x00f0) >> 4) *17; tmp[x*3 + 2]=(pal[x] & 0x000f) *17; } fs_write(f, tmp, 48); fs_write(f, web, 19); ani[4]=df->getAnimationSpeed()*4; gif=new GifSave(); for (z=0; z<df->getIcons(); z++) { icon=df->getIconBitmap(z); fs_write(f, ani, 19); for (y=0; y<32; y++) { for (x=0; x<32; x+=2) { tmp[y*32 + x]=(icon[y*16 + x/2] & 0xf0) >> 4; tmp[y*32 + x+1]=(icon[y*16 + x/2] & 0x0f); } } gif->LZW_Compress(4, tmp, 1024, f); fs_write(f, "\0\0", 2); } delete gif; fs_write(f, ";", 1); fs_close(f); printf("OK!\n"); return(0); }
void Speedtest_Run(GUI_Widget *widget) { uint8 *buff = (uint8*)0x8c400000; size_t buff_size = 0x10000; int size = 0x800000, cnt = 0, rs; int64 time_before, time_after; uint32 t; double speed; file_t fd; int read_only = 0; char name[64]; char result[128]; const char *wname = GUI_ObjectGetName((GUI_Object *)widget); if(!strncasecmp(wname, "/cd", 3)) { read_only = 1; snprintf(name, sizeof(name), "%s/1DS_CORE.BIN", wname); if(FileExists(name)) { goto readtest; } else { snprintf(name, sizeof(name), "%s/1ST_READ.BIN", wname); goto readtest; } } show_status_ok("Testing WRITE speed..."); GUI_LabelSetText(self.speedwr, "..."); GUI_LabelSetText(self.speedrd, " "); snprintf(name, sizeof(name), "%s/%s.tst", wname, lib_get_name()); if(FileExists(name)) { fs_unlink(name); } /* WRITE TEST */ fd = fs_open(name, O_CREAT | O_WRONLY); if (fd == FILEHND_INVALID) { ds_printf("DS_ERROR: Can't open %s for write: %d\n", name, errno); show_status_error("Can't open file for write"); return; } ShutdownVideoThread(); time_before = timer_ms_gettime64(); while(cnt < size) { rs = fs_write(fd, buff, buff_size); if(rs <= 0) { fs_close(fd); InitVideoThread(); ds_printf("DS_ERROR: Can't write to file: %d\n", errno); show_status_error("Can't write to file"); return; } buff += rs; cnt += rs; } time_after = timer_ms_gettime64(); InitVideoThread(); t = (uint32)(time_after - time_before); speed = size / ((float)t / 1000); fs_close(fd); snprintf(result, sizeof(result), "Write speed %.2f Kbytes/s (%.2f Mbit/s) Time: %ld ms", speed / 1024, ((speed / 1024) / 1024) * 8, t); GUI_LabelSetText(self.speedwr, result); show_status_ok("Complete!"); ds_printf("DS_OK: Complete!\n" " Test: write\n Time: %ld ms\n" " Speed: %.2f Kbytes/s (%.2f Mbit/s)\n" " Size: %d Kb\n Buff: %d Kb\n", t, speed / 1024, ((speed / 1024) / 1024) * 8, size / 1024, buff_size / 1024); readtest: show_status_ok("Testing READ speed..."); GUI_LabelSetText(self.speedrd, "..."); /* READ TEST */ fd = fs_open(name, O_RDONLY); if (fd == FILEHND_INVALID) { ds_printf("DS_ERROR: Can't open %s for read: %d\n", name, errno); show_status_error("Can't open file for read"); return; } if(read_only) { GUI_LabelSetText(self.speedwr, "Write test passed"); /* Reset ISO9660 filesystem cache */ fs_ioctl(fd, NULL, 0); fs_close(fd); fd = fs_open(name, O_RDONLY); } time_before = time_after = t = cnt = 0; speed = 0.0f; size = fs_total(fd); buff = rd_buff; ShutdownVideoThread(); time_before = timer_ms_gettime64(); while(cnt < size) { rs = fs_read(fd, buff, buff_size); if(rs <= 0) { fs_close(fd); InitVideoThread(); ds_printf("DS_ERROR: Can't read file: %d\n", errno); show_status_error("Can't read file"); return; } cnt += rs; } time_after = timer_ms_gettime64(); t = (uint32)(time_after - time_before); speed = size / ((float)t / 1000); fs_close(fd); if(!read_only) { fs_unlink(name); } else { cdrom_spin_down(); } snprintf(result, sizeof(result), "Read speed %.2f Kbytes/s (%.2f Mbit/s) Time: %ld ms", speed / 1024, ((speed / 1024) / 1024) * 8, t); InitVideoThread(); ds_printf("DS_OK: Complete!\n" " Test: read\n Time: %ld ms\n" " Speed: %.2f Kbytes/s (%.2f Mbit/s)\n" " Size: %d Kb\n Buff: %d Kb\n", t, speed / 1024, ((speed / 1024) / 1024) * 8, size / 1024, buff_size / 1024); GUI_LabelSetText(self.speedrd, result); show_status_ok("Complete!"); }
/** * @ingroup shell * * Shell command fstest. * @param nargs number of arguments in args array * @param args array of arguments * @return OK for success, SYSERR for syntax error */ shellcmd xsh_fstest(int nargs, char *args[]) { int rval; int fd, i, j; char *buf1, *buf2; /* Output help, if '--help' argument was supplied */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s\n\n", args[0]); printf("Description:\n"); printf("\tFilesystem Test\n"); printf("Options:\n"); printf("\t--help\tdisplay this help and exit\n"); return OK; } /* Check for correct number of arguments */ if (nargs > 1) { fprintf(stderr, "%s: too many arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } if (nargs < 1) { fprintf(stderr, "%s: too few arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } #ifdef FS bs_mkdev(0, MDEV_BLOCK_SIZE, MDEV_NUM_BLOCKS); /* device "0" and default blocksize (=0) and count */ fs_mkfs(0,DEFAULT_NUM_INODES); /* bsdev 0*/ testbitmask(); buf1 = getmem(SIZE*sizeof(char)); buf2 = getmem(SIZE*sizeof(char)); // Create test file fd = fs_create("Test_File", O_CREAT); // Fill buffer with random stuff for(i=0; i<SIZE; i++) { j = i%(127-33); j = j+33; buf1[i] = (char) j; } rval = fs_write(fd,buf1,SIZE); if(rval == 0 || rval != SIZE ) { printf("\n\r File write failed"); goto clean_up; } // Now my file offset is pointing at EOF file, i need to bring it back to start of file // Assuming here implementation of fs_seek is like "original_offset = original_offset + input_offset_from_fs_seek" fs_seek(fd,-rval); //read the file rval = fs_read(fd, buf2, rval); buf2[rval] = '\0'; if(rval == 0) { printf("\n\r File read failed"); goto clean_up; } printf("\n\rContent of file %s",buf2); rval = fs_close(fd); if(rval != OK) { printf("\n\rReturn val for fclose : %d",rval); } clean_up: freemem(buf1,SIZE); freemem(buf2,SIZE); #else printf("No filesystem support\n"); #endif return OK; }
uchar cmd_copyFile(char* from, char* to) { char buf[16]; uchar e, progress_i=0; ulong progress; ulong progress_x, progress_step; // Открываем исходный файл и получаем его длину if(e = fs_open(from)) return e; if(e = fs_getsize()) return e; // Расчет шага прогресса set32(&progress_step, &fs_result); div32_16(&progress_step, 40); // Интерфейс drawWindow(" kopirowanie "); drawWindowText(0, 0, "iz:"); drawWindowText(4, 0, from); drawWindowText(0, 1, "w:"); drawWindowText(4, 1, to); drawWindowText(0, 2, "skopirowano / bajt"); drawWindowProgress(0, 3, progress_width, '#'); i2s32(buf, &fs_result, 10, ' '); drawWindowText(23, 2, buf); drawEscButton(); // Создаем новый файл if(e = fs_swap()) return e; if(e = fs_create(to)) return e; // Копирование SET32IMM(&progress, 0); SET32IMM(&progress_x, 0); for(;;) { // Вывод прогресса i2s32(buf, &progress, 10, ' '); drawWindowText(12, 2, buf); // Копирование блока if(e = fs_swap()) return e; if(e = fs_read(panelB.files1, 1024) ) return e; if(fs_low == 0) return 0; // С перезагрузкой файлов if(e = fs_swap()) return e; if(e = fs_write(panelB.files1, fs_low)) return e; // Это недоработка компилятора, Он не поддерживает 32-х битной арифметиики add32_16(&progress, fs_low); // Прогресс add32_16(&progress_x, fs_low); while(progress_i < progress_width && cmp32_32(&progress_x, &progress_step) != 1) { sub32_32(&progress_x, &progress_step); drawWindowText(progress_i, 3, "\x17"); ++progress_i; } // Прерывание if(fs_bioskey(1) == KEY_ESC) { e = ERR_USER; break; } } // Удалить файл в случае ошибки. Ошибку удаления не обрабатываем. fs_delete(to); return e; }
static UTHR_DEFINE(_webdav_backend_fs_put_uthr) { UTHR_HEADER(WebdavBackendFsPutCtx, ctx); webdav_error_t error; ctx->fd = (fs_file_handle_t) 0; ctx->file_path = path_from_uri(ctx->pbctx, ctx->relative_uri); if (!ctx->file_path) { error = WEBDAV_ERROR_GENERAL; goto done; } bool created; bool create = true; const fs_error_t ret_open = fs_open(ctx->pbctx->fs, ctx->file_path, create, &ctx->fd, &created); if (ret_open) { log_info("Error opening \"%s\": %s", ctx->file_path, util_fs_strerror(ret_open)); switch (ret_open) { case FS_ERROR_DOES_NOT_EXIST: error = WEBDAV_ERROR_DOES_NOT_EXIST; break; case FS_ERROR_NOT_DIR: error = WEBDAV_ERROR_NOT_COLLECTION; break; case FS_ERROR_IS_DIR: error = WEBDAV_ERROR_IS_COL; break; default: error = WEBDAV_ERROR_GENERAL; break; } goto done; } ctx->resource_existed = !created; const fs_error_t ret_truncate = fs_ftruncate(ctx->pbctx->fs, ctx->fd, 0); if (ret_truncate) { log_info("Error truncated \"%s\": %s", ctx->file_path, util_fs_strerror(ret_truncate)); error = WEBDAV_ERROR_GENERAL; goto done; } ctx->total_amount_transferred = 0; while (true) { UTHR_YIELD(ctx, webdav_put_request_read(ctx->put_ctx, ctx->buf, sizeof(ctx->buf), _webdav_backend_fs_put_uthr, ctx)); UTHR_RECEIVE_EVENT(WEBDAV_PUT_REQUEST_READ_DONE_EVENT, WebdavPutRequestReadDoneEvent, read_done_ev); if (read_done_ev->error) { log_info("Error while reading data for %s: %d", ctx->relative_uri, read_done_ev->error); error = read_done_ev->error; goto done; } /* EOF */ if (!read_done_ev->nbyte) { break; } const size_t amount_read = read_done_ev->nbyte; size_t amount_written = 0; while (amount_written < amount_read) { /* need to initialize `new_amount_written` to avoid spurious -Wmaybe-uninitialized warnings from GCC */ size_t new_amount_written = 0; const fs_error_t ret_write = fs_write(ctx->pbctx->fs, ctx->fd, ctx->buf + amount_written, amount_read - amount_written, amount_written + ctx->total_amount_transferred, &new_amount_written); if (ret_write) { log_error("Couldn't write to resource \"%s\" (fd: %p)", ctx->relative_uri, (void *) ctx->fd); error = WEBDAV_ERROR_GENERAL; goto done; } assert(new_amount_written); amount_written += new_amount_written; } assert(amount_written == amount_read); ctx->total_amount_transferred += amount_written; } log_info("Resource \"%s\" created with %lu bytes", ctx->relative_uri, (unsigned long) ctx->total_amount_transferred); error = WEBDAV_ERROR_NONE; done: free(ctx->file_path); if (ctx->fd) { const fs_error_t ret_close = fs_close(ctx->pbctx->fs, ctx->fd); ASSERT_TRUE(!ret_close); } UTHR_RETURN(ctx, webdav_put_request_end(ctx->put_ctx, error, ctx->resource_existed)); UTHR_FOOTER(); }
/* ================= epng_write ================= */ static void epng_write (png_structp png_ptr, png_bytep data, png_size_t length) { if ((int)length != fs_write(png_get_io_ptr(png_ptr), data, length)) epng_error(png_ptr, "failed to write %i bytes\n"); }
int process_request(const char *line, FILE *fp, FileSystem *fs) { char command[4096]; #ifdef DEBUG fprintf(stderr, "process request `%s`\n", line); #endif sscanf(line, "%s", command); #ifdef DEBUG fprintf(stderr, "command is `%s`\n", command); #endif if (0 == strcmp("f", command)) { fs_format(fs); return RESULT_DONE; } else if (0 == strcmp("mk", command)) { char f[4096]; char parent_path[4096]; sscanf(line + 2, "%s", f); #ifdef DEBUG fprintf(stderr, "> mk `%s`\n", f); #endif if (fs_exists(fs, f)) { #ifdef DEBUG fprintf(stderr, "> `%s` already exists\n", f); #endif return RESULT_NO; } else { #ifdef DEBUG fprintf(stderr, "> going to create `%s`\n", f); #endif } fs_split_path(f, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_create(fs, f)) { #ifdef DEBUG fprintf(stderr, "> failed to create `%s`\n", f); #endif return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "> parent path `%s` is not a directory\n", parent_path); #endif return RESULT_NO; } else if (0 == strcmp("mkdir", command)) { char d[4096]; char parent_path[4096]; sscanf(line + 5, "%s", d); if (fs_exists(fs, d)) { return RESULT_NO; } fs_split_path(d, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_mkdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("rm", command)) { char f[4096]; sscanf(line + 2, "%s", f); if (fs_isfile(fs, f)) { if (fs_unlink(fs, f)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("cd", command)) { char path[4096]; sscanf(line + 2, "%s", path); if (fs_isdir(fs, path)) { if (fs_chdir(fs, path)) { return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "`%s` is not a directory\n", path); #endif return RESULT_NO; } else if (0 == strcmp("rmdir", command)) { char d[4096]; sscanf(line + 5, "%s", d); if (fs_isdir(fs, d)) { if (fs_rmdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("ls", command)) { fs_ls(fs, fp); return RESULT_ELSE; } else if (0 == strcmp("cat", command)) { char f[4096]; sscanf(line + 3, "%s", f); if (fs_isfile(fs, f)) { fs_cat(fs, f, fp); return RESULT_ELSE; } return RESULT_NO; } else if (0 == strcmp("w", command)) { char f[4096]; int l; char data[4096]; sscanf(line + 1, "%s %d %[^\n]", f, &l, data); if (fs_isfile(fs, f)) { if (fs_write(fs, f, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("i", command)) { char f[4096]; int pos; int l; char data[4096]; sscanf(line + 1, "%s %d %d %[^\n]", f, &pos, &l, data); if (fs_isfile(fs, f)) { if (fs_insert(fs, f, pos, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("d", command)) { char f[4096]; int pos; int l; sscanf(line + 1, "%s %d %d", f, &pos, &l); if (fs_isfile(fs, f)) { if (fs_delete(fs, f, pos, l)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("e", command)) { return RESULT_EXIT; } return RESULT_ELSE; }
static int rip_sec(int tn,int first,int count,int type,char *dst_file, int disc_type){ double percent,percent_last=0.0; maple_device_t *cont; cont_state_t *state; file_t hnd; int secbyte = (type == 4 ? 2048 : 2352) , i , count_old=count, bad=0, cdstat, readi; uint8 *buffer = (uint8 *)memalign(32, SEC_BUF_SIZE * secbyte); GUI_WidgetMarkChanged(self.app->body); // ds_printf("Track %d First %d Count %d Type %d\n",tn,first,count,type); /* if (secbyte == 2048) cdrom_set_sector_size (secbyte); else _cdrom_reinit (1); */ cdrom_set_sector_size (secbyte); if ((hnd = fs_open(dst_file,O_WRONLY | O_TRUNC | O_CREAT)) == FILEHND_INVALID) { ds_printf("Error open file %s\n" ,dst_file); cdrom_spin_down(); free(buffer); return CMD_ERROR; } LockVideo(); while(count) { int nsects = count > SEC_BUF_SIZE ? SEC_BUF_SIZE : count; count -= nsects; while((cdstat=cdrom_read_sectors(buffer, first, nsects)) != ERR_OK ) { if (atoi(GUI_TextEntryGetText(self.num_read)) == 0) break; readi++ ; if (readi > 5) break ; thd_sleep(200); } readi = 0; if (cdstat != ERR_OK) { if (!GUI_WidgetGetState(self.bad)) { UnlockVideo(); GUI_ProgressBarSetPosition(self.read_error, 1.0); for(;;) { cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER); if(!cont) continue; state = (cont_state_t *)maple_dev_status(cont); if(!state) continue; if(state->buttons & CONT_A) { GUI_ProgressBarSetPosition(self.read_error, 0.0); GUI_WidgetMarkChanged(self.app->body); ds_printf("DS_ERROR: Can't read sector %ld\n", first); free(buffer); fs_close(hnd); return CMD_ERROR; } else if(state->buttons & CONT_B) { GUI_ProgressBarSetPosition(self.read_error, 0.0); GUI_WidgetMarkChanged(self.app->body); break; } else if(state->buttons & CONT_Y) { GUI_ProgressBarSetPosition(self.read_error, 0.0); GUI_WidgetSetState(self.bad, 1); GUI_WidgetMarkChanged(self.app->body); break; } } } // Ошибка, попробуем по одному uint8 *pbuffer = buffer; LockVideo(); for(i = 0; i < nsects; i++) { while((cdstat=cdrom_read_sectors(pbuffer, first, 1)) != ERR_OK ) { readi++ ; if (readi > atoi(GUI_TextEntryGetText(self.num_read))) break ; if (readi == 1 || readi == 6 || readi == 11 || readi == 16 || readi == 21 || readi == 26 || readi == 31 || readi == 36 || readi == 41 || readi == 46) cdrom_reinit(); thd_sleep(200); } readi = 0; if (cdstat != ERR_OK) { // Ошибка, заполним нулями и игнорируем UnlockVideo(); cdrom_reinit(); memset(pbuffer, 0, secbyte); bad++; ds_printf("DS_ERROR: Can't read sector %ld\n", first); LockVideo(); } pbuffer += secbyte; first++; } } else { // Все ок, идем дальше first += nsects; } if(fs_write(hnd, buffer, nsects * secbyte) < 0) { // Ошибка записи, печально, прерываем процесс UnlockVideo(); free(buffer); fs_close(hnd); return CMD_ERROR; } UnlockVideo(); percent = 1-(float)(count) / count_old; if ((percent = ((int)(percent*100 + 0.5))/100.0) > percent_last) { percent_last = percent; GUI_ProgressBarSetPosition(self.pbar, percent); LockVideo(); } } UnlockVideo(); free(buffer); fs_close(hnd); ds_printf("%d Bad sectors on track\n", bad); return CMD_OK; }
void addto_dir(INODE *pdirinode, INODE *pfinode, char *filename) { int i, j, index = -1; DIR_ATTR da = {0}; FILE_ATTR dfa = {0}, fa[32] = {{0}}; int len = strlen(filename); char name[255]; /* read directory attribute */ if(pdirinode->filesz == 0) { da.nr_files = 1; da.nr_index = 1; da.filesz = sizeof(DIR_ATTR); fs_write(pdirinode, 0, sizeof(DIR_ATTR), &da); } else { fs_read(pdirinode, 0, sizeof(DIR_ATTR), &da); da.nr_files ++; } /* init destination fileattr */ dfa.inode = pfinode->inodeno; dfa.len = len; for(i = 0; i < da.nr_index; i += 32) { int left = min(sizeof(fa), (da.nr_index - i) * sizeof(FILE_ATTR)); fs_read(pdirinode, sizeof(DIR_ATTR) + i * sizeof(FILE_ATTR), left, (void*)fa); for(j = 0; j < left / sizeof(FILE_ATTR); j++) { if(fa[j].dirty == 0) { index = i + j; fa[j].dirty = 1; goto exitloop_addto_dir; } } } exitloop_addto_dir: if(index == -1) { index = da.nr_index; for(i = 0; i < da.nr_index; i += 32) { bool dirty = false; int left = min(sizeof(fa), (da.nr_index - i) * sizeof(FILE_ATTR)); int fop = sizeof(DIR_ATTR) + da.nr_index * sizeof(FILE_ATTR); fs_read(pdirinode, sizeof(DIR_ATTR) + i * sizeof(FILE_ATTR), left, fa); for(j = 0; j < left / sizeof(FILE_ATTR); j++) { /* move filename in nr_index's index */ if(fa[j].filename_st >= fop && fa[j].filename_st < fop + sizeof(FILE_ATTR)) { int t = max(pdirinode->filesz, fop + sizeof(FILE_ATTR)); if(t > pdirinode->filesz) { memset(name, 0, t - pdirinode->filesz); fs_write(pdirinode, pdirinode->filesz, t - pdirinode->filesz, name); } fs_read(pdirinode, fa[j].filename_st, fa[j].len + 1, name); fa[j].filename_st = t; fs_write(pdirinode, t, fa[j].len + 1, name); dirty = true; } } /* write back data if dirty */ if(dirty) fs_write(pdirinode, sizeof(DIR_ATTR) + i * sizeof(FILE_ATTR), left, fa); } da.nr_index ++; } /* update dirattr */ fs_write(pdirinode, 0, sizeof(DIR_ATTR), (void *)&da); /* write dest fileattr */ dfa.dirty = 1; fs_write(pdirinode, sizeof(DIR_ATTR) + index * sizeof(FILE_ATTR), sizeof(FILE_ATTR), &dfa); /* write filename */ fs_write(pdirinode, pdirinode->filesz, len + 1, filename); fs_read(pdirinode, pdirinode->filesz - len - 1, len + 1, name); /* write again to update filename_st */ dfa.filename_st = pdirinode->filesz - len - 1; fs_write(pdirinode, sizeof(DIR_ATTR) + index * sizeof(FILE_ATTR), sizeof(FILE_ATTR), &dfa); }
int gdfiles(char *dst_folder,char *dst_file,char *text){ file_t gdfd; FILE *gdifd; CDROM_TOC gdtoc; CDROM_TOC cdtoc; int track ,lba ,gdtype ,cdtype; uint8 *buff = memalign(32, 32768); cdrom_set_sector_size (2048); if(cdrom_read_toc(&cdtoc, 0) != CMD_OK) { ds_printf("DS_ERROR:CD Toc read error\n"); free(buff); return CMD_ERROR; } if(cdrom_read_toc(&gdtoc, 1) != CMD_OK) { ds_printf("DS_ERROR:GD Toc read error\n"); free(buff); return CMD_ERROR; } if(cdrom_read_sectors(buff,45150,16)) { ds_printf("DS_ERROR: IP.BIN read error\n"); free(buff); return CMD_ERROR; } strcpy(dst_file,"\0"); snprintf(dst_file,MAX_FN_LEN,"%s/IP.BIN",dst_folder); if ((gdfd=fs_open(dst_file,O_WRONLY | O_TRUNC | O_CREAT)) == FILEHND_INVALID) { ds_printf("DS_ERROR: Error open IP.BIN for write\n"); free(buff); return CMD_ERROR; } if (fs_write(gdfd,buff,32768) == -1) { ds_printf("DS_ERROR: Error write IP.BIN\n"); free(buff); fs_close(gdfd); return CMD_ERROR; } fs_close(gdfd); free(buff); ds_printf("IP.BIN succes dumped\n"); fs_chdir(dst_folder); strcpy(dst_file,"\0"); snprintf(dst_file,MAX_FN_LEN,"%s.gdi",text); if ((gdifd=fopen(dst_file,"w")) == NULL){ ds_printf("DS_ERROR: Error open %s.gdi for write\n",text); return CMD_ERROR; } int cdfirst = TOC_TRACK(cdtoc.first); int cdlast = TOC_TRACK(cdtoc.last); int gdfirst = TOC_TRACK(gdtoc.first); int gdlast = TOC_TRACK(gdtoc.last); fprintf(gdifd,"%d\n",gdlast); for (track=cdfirst; track <= cdlast; track++ ) { lba = TOC_LBA(cdtoc.entry[track-1]); lba -= 150; cdtype = TOC_CTRL(cdtoc.entry[track-1]); fprintf(gdifd, "%d %d %d %d track%02d.%s 0\n",track,lba,cdtype,(cdtype == 4 ? 2048 : 2352),track,(cdtype == 4 ? "iso" : "raw")); } for (track=gdfirst; track <= gdlast; track++ ) { lba = TOC_LBA(gdtoc.entry[track-1]); lba -= 150; gdtype = TOC_CTRL(gdtoc.entry[track-1]); fprintf(gdifd, "%d %d %d %d track%02d.%s 0\n",track,lba,gdtype,(gdtype == 4 ? 2048 : 2352),track,(gdtype == 4 ? "iso" : "raw")); } fclose(gdifd); fs_chdir("/"); ds_printf("%s.gdi succes writen\n",text); return CMD_OK; }