Knowing when a different program modifies a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Knowing when a different program modifies a file
# 15  
Old 04-18-2013
I have to admit that Hanson44 is right - some editors reuse inodes (which, BTW, was not the topic of this thread).
I'm using joe; its reusing inodes as well, which I never noticed before. But, this guy is smart: if you edit a file, and another process adds/edits as well, it tells you before allowing to edit:
Code:
Notice: File on disk changed! (hit ^C to continue)

and, when trying to finally write the file, it issues:
Code:
File on disk is newer. Overwrite (y,n,^C)?

# 16  
Old 04-18-2013
Quote:
Originally Posted by hanson44
I would say it's as clear as mud at this point. I examined the inode. It did not change.
Quote:
Originally Posted by hanson44
I am willing to believe the inode could change when vi writes out the file. Can someone provide an example of that happening?
Code:
$ echo foo > file
$ cat file
foo
$ ls -i file
472445 file
$ # vim 7.0 opens file in a different terminal, showing just one line, "foo"
$ echo bar >> file
$ cat file
foo
bar
$ ls -i file
472445 file
$ # vim :wq in the other  (after it warns that the file has changed)
$ cat file
foo
$ ls -i file
472449 file

A syscall trace of vim 7.0 reveals the source of the new file with the new inode number:
Code:
rename("file", "file~") = 0
open("file", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 3
write(3, "foo\n", 4)
...
close(3)
...
unlink("file~") = 0

Before writing, this version of vim is using the rename system call to move the original file to file~. It then uses the open syscall with the O_CREAT flag to create a different file. It then writes the contents of the buffer to this new file before unlinking the backup copy (which is the original file and points to the original inode).

Quote:
Originally Posted by hanson44
The previous post from RudiC said "Nothing to do with vi nor syslog; it's just the way the file system works." Nobody contradicted that. My example seems to contradict the assertion.
Your example does not contradict anything. It merely demonstrates that a different sequence of calls is being made.

Regards,
Alister
# 17  
Old 04-18-2013
Thank you for verifying with a different editor. It was obvious from the tests I did that the same file was being written to, regardless of what some man page or specification might suggest.

Just to make sure, I did the same test on syslog. If you edit syslog (normally not a good idea), wait a while for something to be appended, write and exit vi, the newly added content is not there. The original poster thought syslog stopped working only because the newly added content was overwritten with the original file when vi saved/exited.
# 18  
Old 04-18-2013
Quote:
Originally Posted by hanson44
Just to make sure, I did the same test on syslog. If you edit syslog (normally not a good idea), wait a while for something to be appended, write and exit vi, the newly added content is not there. The original poster thought syslog stopped working only because the newly added content was overwritten with the original file when vi saved/exited.
This could be insidious, actually. If the old file is deleted and a new one created, syslog will apparently stop working, since the old, deleted file will remain on disk -- and keep getting written to -- until syslog rotates the file itself for some reason or the machine restarts.

What puzzles me is why anything under /var/log was being edited at all. Smilie
# 19  
Old 04-18-2013
Quote:
This could be insidious. What puzzles me is
why anything under /var/log was being edited at all.
In my posts, including the last one, I have repeatedly said it is NOT a good idea to edit syslog (or other system log file). So I am in 100% agreement. I just did this as a one-time operation (a great sacrifice Smilie) to test the assertion that syslog would continue to be written due (and the content preserved), even if vi edited and saved the file. There was no other way to test it.
# 20  
Old 04-22-2013
Quote:
Originally Posted by hanson44
Just to make sure, I did the same test on syslog. If you edit syslog (normally not a good idea), wait a while for something to be appended, write and exit vi, the newly added content is not there. The original poster thought syslog stopped working only because the newly added content was overwritten with the original file when vi saved/exited.
If by "overwritten" you mean that the editor changed the file's contents, then you are wrong. If by "overwritten" you mean that the editor created a completely different file, then you are correct.

If an editor simply overwrites the file's contents, after the edit, syslog's new messages would continue to appear in the file.

If the messages cease to reach the named file, then the original file has been unlinked and replaced by a new file (a different inode) with the same name. Meanwhile, syslog is still happily logging to the original log file (now with no name).

The OP stated having to restart to resume logging. That would not be necessary if the file's inode had not changed.

Regards,
Alister
# 21  
Old 04-22-2013
Quote:
The OP stated having to restart to resume logging
I can't seen the OP's machine to know what really happened. If restart was really needed, then I agree. On my test, restart was not needed. Anyway, based on our tests (see below), it all makes sense.

----------------------------
Code:
Your test:
$ echo foo > file
$ cat file
foo
$ ls -i file
472445 file
$ # vim 7.0 opens file in a different terminal, showing just one line, "foo"
$ echo bar >> file
$ cat file
foo
bar
$ ls -i file
472445 file
$ # vim :wq in the other  (after it warns that the file has changed)
$ cat file
foo
$ ls -i file
472449 file

I just saw this previous post (above). It was at the same time I posted, so got missed. I re-ran the exact sequence of operations (below), using vim 7.3.547 and linux, as a reality check, and the inode does not change:
Code:
My re-test:
$ echo foo > file
$ cat file
foo
$ ls -i file
786454 file
$ # vim 7.3 opens file in a different terminal, showing just one line, "foo"
$ cat file
foo
$ echo bar >> file
$ cat file
foo
bar
$ ls -i file
786454 file
$ # vim :wq in the other  (after it warns that the file has changed)
$ cat file
foo
$ ls -i file
786454 file

It seems you have shown that vim can be configured to create a new file with a new inode. As you explained, it has something to do with the backup file. My vim happens not to be configured that way (I don't think I would want it configured that way). The OP could well have had their vi configured in that way, so that would explain what happened. But that wasn't the question. The previous contention was that vim had to create a new file, that the inode had to change, due to something more fundamental going on. It was the believable idea Nothing to do with vi nor syslog; it's just the way the file system works that nobody else objected to, that I decided to investigate. I just wanted to know the answer. My test shows that shows vim does not have to create a new file. Your test shows vim can be configured to create a new file.

Quote:
If by "overwritten" you mean that the editor changed the
file's contents, then you are wrong. If by "overwritten" you
mean that the editor created a completely different file,
then you are correct.
If the inode does not change, isn't it still the original file? The inode does not change in my test, as shown above. Your test shows that vim can be configured to create a new file under the test conditions, not that the new file creation is inevitable or required. I appreciate that you posted the test, and helped clarify this.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl to download file from subdivx.com after following location without knowing the file name/extens

This question could be specific to the site subdivx.com In the past, I've been able to download a file following location using cURL but there is something about subdivx.com that's different and can't figure out how to get it to work. I tried the following directly in the terminal with no... (5 Replies)
Discussion started by: MoonD
5 Replies

2. Solaris

Passwd -l or -u modifies lastchg field in /etc/shadow file

Hi, I have a Solaris 10 box where password aging is not functioning properly. Using the passwd command with the -l or -u options causes the lastchg field in the /etc/shadow file to be modified. Therefore, if a user's password is set to expire in 90 days and they are 1 day away, all they have... (4 Replies)
Discussion started by: cschar
4 Replies

3. UNIX Desktop Questions & Answers

Knowing the size and location of variables in a C program

So I need some help with this. Pardon me if I'm posting in the wrong forum, after some googling for my answer and finding nothing I found this forum. It seemed appropriate for what I was seeking. I just didnt find a forum that concerned the use of GDB. I'm learning to use the C language and GDB.... (2 Replies)
Discussion started by: Cambria
2 Replies

4. Emergency UNIX and Linux Support

Is there any way to set the files modified date and stamp to last modifies time?

Actually i did modification in a file on server by mistake, now its showing current time stamp, is there any way to set the files modified date and stamp to last modifies time. Please advice here.Thanks in advance.:b: (7 Replies)
Discussion started by: saluja.deepak
7 Replies

5. Shell Programming and Scripting

how to use the filehandle stored in a variable without knowing its file association

how to use the filehandle stored in a variable without knowing its file association i.e. the filename code my $logFH = $connObj->get('logFH'); infoPrint("Variable is of type IO \n") if(UNIVERSAL::isa($logFH, 'IO')); infoPrint("$logFH\n"); output == INFO :: Variable is of type... (0 Replies)
Discussion started by: rrd1986
0 Replies

6. Shell Programming and Scripting

How Can I get terminal no when someone modifies CRONTAB file

Hi All I have a script which drops a mail with the changes done on crontab file for every 1 hour Can anyone pls tell me how can I modify the script so that it should display the terminal from which the crontab file hasbeen modified. Quick responce much appriciated !! Many thanks in... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

7. Shell Programming and Scripting

Knowing whether the file has completely SFTP ed

Hi.. Can Anyone out there help me? I need to write a script to convert a file in EDCIDC format to CSV The files will be transfered through sftp to the box. Is there a way to check the file has finished being transfered or still transfering. so that my conversion task will be performed after... (3 Replies)
Discussion started by: ramukandada
3 Replies

8. UNIX for Dummies Questions & Answers

Rename file knowing the first 7 carachters

Hi, people. I need some help with this: i have this file " PROVEDP_???_yyyymmdd " , and i want to rename to this " IN_PROV_yyyy???.dat " . The " ??? " is the Month , but the file could be created on April, but the name coulb be March, for example.So i need to grab the 3 caracters ... (2 Replies)
Discussion started by: osramos
2 Replies

9. UNIX for Advanced & Expert Users

knowing progress while reading a file

Hi, I am parsing a very big file say 10 MB. It 'll take more than an hour ..I want to know the progress say in % .Is there any way to do that??? or (Is there any way to know which line of the file I am in ) (2 Replies)
Discussion started by: sakthi.abdullah
2 Replies

10. Shell Programming and Scripting

Script that modifies root level files

I run a decent size Solaris 8 network where we use host files and no DNS servers. I have a master host file to push out to all the machines (also would like to do system and services too) but, the only way I've ever been able to do it is buy telneting into the machine and ftping the file into place... (10 Replies)
Discussion started by: turbo90awd
10 Replies
Login or Register to Ask a Question