Find the answer to your Linux question:
Page 3 of 5 FirstFirst 1 2 3 4 5 LastLast
Results 21 to 30 of 45
Originally Posted by bitzsk One thing i cant understand is that when i open a file and delete same charactors, then save the file. i guess the block number should ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #21
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758

    Post


    Quote Originally Posted by bitzsk View Post
    One thing i cant understand is that when i open a file and delete same charactors, then save the file. i guess the block number should not changed, because i not add any thing to the file and the old block should have enough space to save the file to the same block.

    so what i think is add something to a file, it's block will change.

    delete something from a file, it's block will not change.

    but it's not true.

    The starting block of the file will likely not change, though I am not certain of that.
    Rubberman,Your uncertainity is correct. New block may or mayn't allocated ,it's based on the application which uses the files.

    We have file's content in buffer,while modifying kernel takes the next available block number from its free list and stores the content in to it,If the application asks the kernel to do so.

    You can check that with simple example :

    Original file st1 - note block number(5939201) and inode

    [root@ctsingtofed08 tmp]# filefrag -v st1
    Checking st1
    Filesystem type is: ef53
    Filesystem cylinder groups is approximately 303
    Blocksize of file st1 is 4096
    File size of st1 is 512 (1 blocks)
    First block: 5939201
    Last block: 5939201
    st1: 1 extent found

    [root@ctsingtofed08 tmp]# ls -i st1
    5925319 st1
    Now i'm adding some content using cat command
    [root@ctsingtofed08 tmp]# cat >> st1
    sdfs
    Inode remains the same.
    [root@ctsingtofed08 tmp]# ls -i st1
    5925319 st1

    and the block number too.
    [root@ctsingtofed08 tmp]# filefrag -v st1
    Checking st1
    Filesystem type is: ef53
    Filesystem cylinder groups is approximately 303
    Blocksize of file st1 is 4096
    File size of st1 is 517 (1 blocks)
    First block: 5939201
    Last block: 5939201
    st1: 1 extent found
    [root@ctsingtofed08 tmp]#
    Now i added some content using vi

    #vi st1

    added some value and closed it.


    Now Inode values remains the same ,but block number is changed.
    [
    root@ctsingtofed08 tmp]# filefrag -v st1
    Checking st1
    Filesystem type is: ef53
    Filesystem cylinder groups is approximately 303
    Blocksize of file st1 is 4096
    File size of st1 is 529 (1 blocks)
    First block: 6063105
    Last block: 6063105
    st1: 1 extent found
    So it's purely based on the application,(vi or cat)the way how they open the file.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  2. #22
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    Quote Originally Posted by Lakshmipathi View Post
    Rubberman,Your uncertainity is correct. New block may or mayn't allocated ,it's based on the application which uses the files.

    We have file's content in buffer,while modifying kernel takes the next available block number from its free list and stores the content in to it,If the application asks the kernel to do so.

    You can check that with simple example :

    Original file st1 - note block number(5939201) and inode



    Now i'm adding some content using cat command

    Inode remains the same.


    Now i added some content using vi

    #vi st1

    added some value and closed it.


    Now Inode values remains the same ,but block number is changed.
    [

    So it's purely based on the application,(vi or cat)the way how they open the file.
    Well, that's because vi, and a lot of other editors, save the old version of the file, and when you save the changes, write the entire data set to a new file. So, this is expected. As you say, this is up to the application. However, that is not due to behavior of the OS, but solely due to behavior of the application. When you modify a file with VI or nedit, you have the old copy, and it is almost certain that it occupies the same location you found it at originally, before the changes.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #23
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758

    Exclamation

    Quote Originally Posted by Rubberman View Post
    Well, that's because vi, and a lot of other editors, save the old version of the file, and when you save the changes, write the entire data set to a new file. So, this is expected. As you say, this is up to the application. However, that is not due to behavior of the OS, but solely due to behavior of the application. When you modify a file with VI or nedit, you have the old copy, and it is almost certain that it occupies the same location you found it at originally, before the changes.
    Thansks for more insight on this issue
    I need to understand how the application can change a block numbers.

    I created a file
    touch it.txt
    then wrote messy problem to append data to it
    [root@ctsingtofed08 tmp]# cat test.c
    main(){
    int fp;
    char x='x';
    fp=open("it.txt",2);
    lseek(fp,0,2);
    write(fp,&x,1);
    close(fp);
    }
    Both inode and block numbers are same,every time i added a character to file.
    So above one is similar to cat command.

    I thought it was related OS functionality , (like an application opening a file with special argument) will write the file in another location. -I realized that's not the case.

    Can you suggest me example program or pseudo codes for another case?
    (may be comparing source code vi and cat might be the best option - i guess)
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  4. #24
    Just Joined! bitzsk's Avatar
    Join Date
    Apr 2009
    Location
    Far, Far, Away on the earth
    Posts
    25
    Thank you. even the file has one block, it will change when add something. as follows:

    zsk@zsk-laptop:~$ sudo filefrag -v a
    Checking a
    Filesystem type is: ef53
    Filesystem cylinder groups is approximately 104
    Blocksize of file a is 4096
    File size of a is 13 (1 blocks)
    First block: 3013938
    Last block: 3013938
    a: 1 extent found
    zsk@zsk-laptop:~$ cat a
    hello,world!
    zsk@zsk-laptop:~$ vim a
    zsk@zsk-laptop:~$ cat a
    hi,hello,world!
    zsk@zsk-laptop:~$ sudo filefrag -v a
    Checking a
    Filesystem type is: ef53
    Filesystem cylinder groups is approximately 104
    Blocksize of file a is 4096
    File size of a is 16 (1 blocks)
    First block: 3013924
    Last block: 3013924
    a: 1 extent found

  5. #25
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758
    Hi bitzsk,
    try using
    cat >> a
    I guess,Now the block won't change.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  6. #26
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,533
    When you edit a file in VI, changes are recorded to .filename.swp. When you save the file, the original file, with the changes, are saved to a new file, and the old one is deleted. This is why you will find that the block number of the file has changed. The original file is not acutally modified. VI keeps all data in core memory. Other editors act differently, but with the same effect. The nedit editor renames the original file as a backup, and saves the changed data to a new file with the same name as the original. Each application can behave in a somewhat different manner, so the fact that the block numbers have changed really means very little.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #27
    Just Joined! bitzsk's Avatar
    Join Date
    Apr 2009
    Location
    Far, Far, Away on the earth
    Posts
    25
    yeah, cat >> a will not change the inode and block number. but the different behavire of cat and vi command confused me. i don't know the mechanism of these commands.

  8. #28
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758

    Smile

    Quote Originally Posted by Rubberman View Post
    When you edit a file in VI, changes are recorded to .filename.swp. When you save the file, the original file, with the changes, are saved to a new file, and the old one is deleted. This is why you will find that the block number of the file has changed. The original file is not acutally modified. VI keeps all data in core memory. Other editors act differently, but with the same effect. The nedit editor renames the original file as a backup, and saves the changed data to a new file with the same name as the original. Each application can behave in a somewhat different manner, so the fact that the block numbers have changed really means very little.
    Thanks Rubberman,So it's a way how they application _handles_ a file rather than how it opens a file.

    that's why we have VI sometimes show a warning message about swap file content is older/newer or something...
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  9. #29
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758

    Smile

    Quote Originally Posted by bitzsk View Post
    yeah, cat >> a will not change the inode and block number. but the different behavire of cat and vi command confused me. i don't know the mechanism of these commands.
    Go through the above replies from Rubberman and me discussing about vi and cat. You will get better understanding of it
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  10. #30
    Just Joined! bitzsk's Avatar
    Join Date
    Apr 2009
    Location
    Far, Far, Away on the earth
    Posts
    25
    Quote Originally Posted by Rubberman View Post
    so the fact that the block numbers have changed really means very little.
    hi, Rubberman

    i am doing a project which is related to security, in the block-level to protect a file against for written even if you have the ROOT privilege. i save the block number of a file first, so when you try to edit the file , i will get the blocks what you are writing for, and compare the blocks with which i saved before,if there are same, the written will be stopped,but if you don't send me the old block number but a new one, i can't control the write behavior any more.

    and if you use cat command to change the file ,i can control

    and if you use vim command to change the file, i cant control.

Page 3 of 5 FirstFirst 1 2 3 4 5 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •