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
# 8  
Old 04-18-2013
Code:
When saving, vim unlinks the original file and creates a new one.

I'm trying to understand what you are saying. I think my assertion is very simple:
Code:
$ date > date.txt
$ vi date.txt # different window, shows one line
$ date >> date.txt
$ cat date.txt
Thu Apr 18 11:46:30 PDT 2013
Thu Apr 18 11:47:57 PDT 2013

Save and exit vi

$ cat date.txt
Thu Apr 18 11:46:30 PDT 2013

Code:
When you saved the log file from within vi, 
you wiped out the changes that syslog had made.

Is this incorrect because of something about vi, or something about syslog?
# 9  
Old 04-18-2013
Nothing to do with vi nor syslog; it's just the way the file system works.
If an editor opens a file, it reads its momentary contents, which you then can edit. During that time, another process can modify (= add to) the original file, of which the editor is not aware. When saving your edits, a NEW file is created, and the old one unlinked, which would normally mean: deleted. Compare inode numbers before and after with e.g. ls -i.
Should that other process have the old file open at that time, the link between file name and inode will be lost, but the file still exists, and can be seen by e.g. lsof. linux's debugfs has a command: undel, to recreate the link between inode and file name.
# 10  
Old 04-18-2013
Quote:
Originally Posted by alister
That is incorrect. Whatever changes syslog had made are still there. When saving, vim unlinks the original file and creates a new one. syslog is still working with the original, as RudiC points out.

While the original file is no longer reachable through the filesystem, any process with an open descriptor to the original's contents can still read/write from/to it. Only when the last of those descriptor's is closed will the kernel remove the unreachable file.

From the POSIX unlink(2) manual:


To demonstrate this, let's use sed to delete all empty lines from a file, without using a temp file (-i, even when available, uses a temp file):
Code:
{ rm file; sed '/./!d' > file; } < file

1) { ... } < file opens a descriptor to the original file contents. As long as this descriptor is open, the original file's contents are accessible.
2) rm file unlinks the file. At this point, the file is no longer reachable through the filesystem hierarchy.
3) The redirection in sed ... > file creates a new file and redirects stdout to it. sed inherits its stdin descriptor from the parent sh, through which it has access to the original file's content.

Such "cleverness" is usually a very bad idea. Not creating the temp file means that, should the system fail at just the right time, you could be left without a reachable version of the data. And even though a temp file isn't created, the amount of storage required is the same (the original version of the file and the version without empty lines will coexist for some finite amount of time).

If instead an editor which does not unlink the original file were used, e.g. ed, there would then be the problem of multiple unsynchronized writers. The resulting file's contents will be some indeterminate, interleaved melange of data written by multiple processes.

Regards,
Alister
The description of how unlink() works on a file is correct. The statement that vim unlink()s the file is an optimization used by vim under certain circumstances. It isn't clear to me that the standards allow this optimization in the case being discussed in this thread. The standard clearly does not allow this behavior when there are multiple links to the file being edited and vim doesn't unlink() (or more likely rename(tmp_file, original_file)) the original file when there are multiple links.

To verify this, execute the following commands:
Code:
$ touch x1 x2
$ ln x2 x3
$ ls -li x[1-3]

You will note at this point that there are two links to x2 and x3 and that they have the same i-node number (in POSIX referred to as the file serial number).
If you then edit x1 with vim to add some text and save the file before exiting vim and rerun the ls, you will indeed see that the i-node number of x1 has changed.

However, if you edit x2 with vim, add some text, save the file before exiting vim, and rerun the ls; you will see that x2 and x3 both still have the same (unchanged) i-node number but the added text is now in this file and visible not matter which link is used to access the file. So, vim only performs this optimization when there is only one link to the file. In my personal opinion, that fact that vim ever does this is a bug.

The standards have a detailed description of how file handles have to be shared between and within processes to get the desired results; and when two processes are updating a file at the same time (as is being described in this thread) the results are undefined. For those of you with access to the 2008 Issue of the POSIX Standards (or the 2013 edition including technical corrigendum #1 to be released tomorrow) this text appears in the System Interfaces volume of the standard in subclause 2.5.1 titled Interaction of File Descriptors and Standard I/O Streams.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 04-18-2013
Quote:
Originally Posted by SkySmart
for instance, if i have a file called /home/skysmart/boldness.txt. I want to know whenever this file is "vi'ed" by a real life user or if someone other than a specific process adds contents to it, as in ">>" /">" .
Quote:
Originally Posted by SkySmart
this happened on linux red hat 6.2
On linux, if your kernel supports inotify, you can use inotifywait to watch a file. Upon notification of a process opening the file, you use lsof to see who came calling. There's a race, though. It's possible that the process may have called close() by the time lsof gets around to looking for it.

If that's unacceptable, you can almost certainly accomplish this task with selinux (something I know almost nothing about).

Regards,
Alister
# 12  
Old 04-18-2013
Quote:
When saving your edits, a NEW file is created.
Compare inode numbers before and after
Code:
$ date > date.txt
$ cat date.txt
Thu Apr 18 12:49:28 PDT 2013
$ ls -i date.txt
786451 date.txt

$ vi date.txt # different window, shows one line

$ date >> date.txt
$ cat date.txt
Thu Apr 18 12:49:28 PDT 2013
Thu Apr 18 12:50:31 PDT 2013

Save and exit vi

$ cat date.txt
Thu Apr 18 12:49:28 PDT 2013
$ ls -i date.txt
786451 date.txt

The inode did not change. Whenever I read one thing in a man page, and see another thing in practice, I tend to believe what I see. But I'm willing to be educated.
# 13  
Old 04-18-2013
Don,

Thank you very much for elaborating on vim's behavior and thanks also for the POSIX update information.

Regards,
Alister

---------- Post updated at 04:03 PM ---------- Previous update was at 04:01 PM ----------

Quote:
Originally Posted by hanson44
The inode did not change. Whenever I read one thing in a man page, and see another thing in practice, I tend to believe what I see. But I'm willing to be educated.
There are many vi implementations. Yours may not have changed the inode, but at least one very popular implementation will under certain circumstances (as detailed in Don Cragun's post).

Regards,
Alister
# 14  
Old 04-18-2013
Quote:
There are many vi implementations.
Yours may not have changed the inode, but at
least one very popular implementation will under
certain circumstances (as detailed in Don Cragun's post).
I would say it's as clear as mud at this point. I examined the inode. It did not change. It doesn't help much to cite long-winded official explanations. I am willing to believe the inode could change when vi writes out the file. Can someone provide an example of that happening?

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.

FWIW, I'm using vim on linux.
 
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