EMILE/libemile/emile_first_set_param_scsi.c

94 lines
2.2 KiB
C
Raw Normal View History

2004-12-09 22:57:09 +00:00
static __attribute__((used)) char* rcsid = "$CVSHeader$";
/*
*
* (c) 2004 Laurent Vivier <Laurent@lvivier.info>
2004-12-09 22:57:09 +00:00
*
*/
2004-12-13 02:01:43 +00:00
#include <stdio.h>
2004-12-09 22:57:09 +00:00
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "libemile.h"
2005-11-30 00:04:02 +00:00
#define BLOCK_SIZE 512 /* FIXME: should ask the disk driver */
2004-12-09 22:57:09 +00:00
int emile_first_set_param_scsi(int fd, char *second_name)
{
int ret;
int fd_second;
char first[1024];
int i;
int current;
struct emile_container *container;
unsigned short max_blocks;
unsigned short *first_max_blocks = (unsigned short*)&first[1022];
unsigned long *second_size = (unsigned long*)&first[1018];
unsigned short *unit_id = (unsigned short*)&first[1016];
unsigned short *block_size = (unsigned short*)&first[1014];
unsigned short *count;
unsigned long *offset;
off_t location;
location = lseek(fd, 0, SEEK_CUR);
if (location == -1)
return EEMILE_CANNOT_READ_FIRST;
2004-12-09 22:57:09 +00:00
ret = read(fd, first, 1024);
if (ret == -1)
2004-12-12 22:59:38 +00:00
return EEMILE_CANNOT_READ_FIRST;
2004-12-09 22:57:09 +00:00
max_blocks = *first_max_blocks / 6;
container = (struct emile_container*)
malloc(sizeof(struct emile_container)
+ max_blocks * sizeof(struct emile_block));
if (container == NULL)
2004-12-12 22:59:38 +00:00
return EEMILE_MALLOC_ERROR;
2004-12-09 22:57:09 +00:00
fd_second = open(second_name, O_RDONLY);
if (fd_second == -1)
2004-12-12 22:59:38 +00:00
return EEMILE_CANNOT_OPEN_FILE;
2004-12-09 22:57:09 +00:00
ret = emile_scsi_create_container(fd_second, unit_id, container, max_blocks);
2004-12-09 22:57:09 +00:00
if (ret != 0)
2004-12-12 22:59:38 +00:00
return ret;
2004-12-09 22:57:09 +00:00
close(fd_second);
2005-11-30 00:04:02 +00:00
*block_size = BLOCK_SIZE;
2004-12-09 22:57:09 +00:00
*second_size = 0;
current = 1014;
for(i = 0; i < max_blocks - 1; i++)
{
current -= 2;
count = (unsigned short*)(&first[current]);
2004-12-09 22:57:09 +00:00
*count = container->blocks[i].count;
if (container->blocks[i].count == 0)
break;
current -= 4;
offset = (unsigned long*)(&first[current]);
2004-12-09 22:57:09 +00:00
*offset = container->blocks[i].offset;
(*second_size) += container->blocks[i].count;
}
/* mark end of blocks list */
current -= 2;
count = (unsigned short*)(&first[current]);
2004-12-09 22:57:09 +00:00
*count = 0;
/* set second level size */
2005-11-30 00:04:02 +00:00
(*second_size) *= BLOCK_SIZE;
2004-12-09 22:57:09 +00:00
ret = lseek(fd, location, SEEK_SET);
2004-12-09 22:57:09 +00:00
if (ret != 0)
2004-12-12 22:59:38 +00:00
return EEMILE_CANNOT_WRITE_FIRST;
2004-12-09 22:57:09 +00:00
ret = write(fd, first, 1024);
if (ret == -1)
2004-12-12 22:59:38 +00:00
return EEMILE_CANNOT_WRITE_FIRST;
2004-12-09 22:57:09 +00:00
2004-12-12 22:59:38 +00:00
return 0;
2004-12-09 22:57:09 +00:00
}