fseeko(this->f, offset, SEEK_SET);
while (bytes_read < total_bytes) {
- int tmp_bytes = fread(buf, 1, length, this->f);
+ int tmp_bytes = fread(buf, 1, length - bytes_read, this->f);
if (tmp_bytes == 0) {
break;
unsigned int iso_image::write(unsigned char * buf, off_t offset, int length) {
-
return 0;
}
port : 9502
-disks : puppy-hd puppy-iso
+disks : puppy-hdd puppy-iso ubuntu-hdd ubuntu-cd
puppy-iso.file : /opt/vmm-tools/isos/puppy.iso
puppy-iso.type : ISO
+
+puppy-hdd.file : /home/jarusl/guests/puppy-test1.hdd
+puppy-hdd.type : RAW
+
+ubuntu-hdd.file : /home/jarusl/guests/ubuntu.hdd
+ubuntu-hdd.type : RAW
+
+ubuntu-cd.file : /opt/vmm-tools/isos/ubuntu-7.10-desktop-i386.iso
+ubuntu-cd.type : ISO
off_t raw_disk::get_capacity() {
-
- return 0;
-}
+ struct stat f_stats;
+
+ stat(this->filename.c_str(), &f_stats);
+ return f_stats.st_size;
+}
unsigned int raw_disk::read(unsigned char * buf, off_t offset, int length) {
-
- return 0;
+ int total_bytes = length;
+ int bytes_read = 0;
+
+ fseeko(this->f, offset, SEEK_SET);
+
+ while (bytes_read < total_bytes) {
+ int tmp_bytes = fread(buf, 1, length - bytes_read, this->f);
+
+ if (tmp_bytes == 0) {
+ break;
+ }
+ bytes_read += tmp_bytes;
+ }
+
+ return bytes_read;
}
-
unsigned int raw_disk::write(unsigned char * buf, off_t offset, int length) {
-
- return 0;
+ int total_bytes = length;
+ int bytes_written = 0;
+
+ fseeko(this->f, offset, SEEK_SET);
+
+ while (bytes_written < total_bytes) {
+ int tmp_bytes = fwrite(buf, 1, length - bytes_written, this->f);
+
+ if (tmp_bytes == 0) {
+ break;
+ }
+ bytes_written += tmp_bytes;
+ }
+
+ return bytes_written;
}
// send :
// 1 bytes : status
int handle_write_request(SOCK conn, v3_disk * disk) {
- return -1;
+ off_t offset = 0;
+ unsigned int length = 0;
+ unsigned char * buf = NULL;
+ unsigned int ret_len = 0;
+ unsigned char status = NBD_STATUS_OK;
+
+ vtl_debug("Write Request\n");
+
+ if (Receive(conn, (char *)&offset, 8, true) <= 0) {
+ vtl_debug("Error receiving write offset\n");
+ return -1;
+ }
+
+ vtl_debug("Write Offset %d\n", offset);
+
+ if (Receive(conn, (char *)&length, 4, true) <= 0) {
+ vtl_debug("Error receiving write length\n");
+ return -1;
+ }
+
+ vtl_debug("Write length: %d\n", length);
+
+ buf = new unsigned char[length];
+
+ vtl_debug("Receiving Data\n");
+
+ if (Receive(conn, (char *)buf, length, true) <= 0) {
+ vtl_debug("Error receiving Write Data\n");
+ return -1;
+ }
+
+ vtl_debug("Wrote %d bytes to source disk\n", ret_len);
+
+ if (disk->write(buf, offset, length) != length) {
+ vtl_debug("Write Error\n");
+ status = NBD_STATUS_ERR;
+ }
+
+ vtl_debug("Sending Status byte (%d)\n", status);
+
+ if (Send(conn, (char *)&status, 1, true) <= 0) {
+ vtl_debug("Error Sending Wrte Status\n");
+ return -1;
+ }
+
+ vtl_debug("Write Complete\n");
+
+ delete buf;
+
+ return 0;
}