Solved

using lseek( )

Posted on 2004-04-05
7
210 Views
Last Modified: 2010-04-21
how can i use a for loop to traverse bit_map[i] array and finds the next available free bitmap position using lseek( )?
Note: i use man lseek page but the concept is still not very clear to me can an expert help out?
0
Comment
Question by:achille67
7 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 10762113
lseek() is only used to position the read/write pointer to a file at a speciffied offset. It doesn't operate on arrays.
0
 

Author Comment

by:achille67
ID: 10762219
Oh ok, so how do i use position a read/write pointer?
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 250 total points
ID: 10762503
You first need to open a file, then you call lseek to position the read/write pointer (which one it is depends on wether you've opened the file in read or write mode):


    int fd;

    fd = open("/path/to/file", O_RDONLY);
    lseek(fd, 1000, SEEK_SET);

This sets the read pointer to the position 1000 in the file. You can find out more about these calls in th eman pages:

man 2 lseek
man 2 open


... but, this has very likely nothing to do with your problem ... What exactly are you trying to accomplish? Do you already have code for your bit_map array? If so, please show it to illustrate your problem.

0
 

Author Comment

by:achille67
ID: 10762880
Thanks khkremer,

The following code is simply importing linux_file from my linux file system to
to u_file residing in my own file system i just create by running the command:
./userfs -reformat 800000 file.system
At some point of the implementation of of u_import, i want to find
next free available free bitmap in bit_map[i] array  and position lseek to that place with  lseek(virtual_disk,BLOCK_SIZE_BYTES,SEEK_SET);
NOTE: u_import is a function of an entire file system homework(you may take a look at it at:  http://www.clarkson.edu/~jnm/os/homework/userfs/fs.htm)

int u_import(char* linux_file, char* u_file)
{
        int free_space;
        int num_block;
        struct stat st;
        int file_size=0;
        int remainder=0;
        int i;
        int j;

        free_space = u_quota();

        handle = open(linux_file,O_RDONLY);
        if ( -1 == handle ) {
                printf("error, reading file %s\n",linux_file);
                return 0;
        }

        fstat(handle,&st);
        file_size=(int)st.st_size;
        num_block=(int)file_size/BLOCK_SIZE_BYTES;
        if((num_block*BLOCK_SIZE_BYTES)!=file_size)
          {
            num_block++;
            remainder=file_size-(num_block*BLOCK_SIZE_BYTES);
          }
     for(i=0;i<num_block;i++)
          {
            lseek(handle,BLOCK_SIZE_BYTES*i,SEEK_SET);
             read(handle,&buffer,BLOCK_SIZE_BYTES);


             //      crash_write(virtual_disk, bit_map, sizeof(BIT)*BIT_MAP_SIZE );

             // bit_map[i] array and finds the next available free bitmap
             //position lseek to that place with
                     for (j=0; j< BIT_MAP_SIZE; j++){
             assert( free_space >= 1);
               allocate_block(i);

                 lseek(virtual_disk,BLOCK_SIZE_BYTES,SEEK_SET);

                     }
                     .
                     .
                     .
                      return 1;
}
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10764031
Hi achille67,
I see that you use int for calculating offsets. That will very likely cause problems, as offsets should be 64 bits. Define an unsigned type which you use for FS block/seek oprations, like

typedef uint32_t fs_block_t; /* For storing block numbers */
typedef uint64_t fs_seek_t;  /* For seeking in files      */

The system offers the off_t type for seeks which you can also use.

Cheers,
Stefan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now