CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2003
    Location
    Bulgaria
    Posts
    56

    Question How can I detect file change?!

    Hi everybody,

    I want to develop module that detects file's change. I mean than your application will be notified through that module when somebody changes file outside. I found some convinient ways to do this job in Win32. I'm using FindFirstChangeNotification, FindNextChangeNotification and ReadDirectoryChangesW functions. But after using them I must look for changed file because these function doesn't give me information about which one is the changed file. Does anybody knows how to do this task? The simplest way is to check date of modification of file. But I wonder myself whether there is any more direct way.

    Also does anybody knows how to do this detection on Unix/Linux?!

    Regards, Alex

  2. #2
    Join Date
    Jan 2003
    Location
    Bulgaria
    Posts
    56

    Exclamation ReadDirectoryChangesW is not available on Win9x

    I saw that ReadDirectoryChangesW function returns FILE_NOTIFY_INFORMATION structure but this function is not supported on Windows 9x according to the MSDN. This makes me to use FindFirstChangeNotification, FindNextChangeNotification functions on Windows 9x. So anybody who has some advise for the last is welcome.

    Regards, Alex

  3. #3
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    well to see if there has been a change to a file you can run it against a CRC32 value you know to represent the file prior to modification.

    to actually FIND each change in the file though would be lots harder and i'm not familiar with how to do that. there are programs around though *some ship w/ windows* that do things like that and you could take those and view their import tables to get an idea of how it's doing it (that's a very handy way to get a GENERAL idea)

    search the forum for CRC and you'll find a ton of stuff

  4. #4
    Join Date
    Jan 2003
    Location
    Bulgaria
    Posts
    56

    Exclamation Why CRC???

    I'm not looking for the change inside the file. There are a lot of tool that do this job very pretty (like diff).
    I'm looking for notification which file was changed outside my aplication. I have some ideas about FindFirstChangeNotification&FindNextChangeNotification. I can wait on event handle for each one of directories that I want to observe with WaitForSingleObjectEx function. In this way I can detect exact directory in which change occured. If somebody has better idea let me know.


    Regards, Alex

    PS: What about Unix/Linux?! I'll be very surprised if these OS doesn't support such kind of feature like this one supported in Windows.

  5. #5
    Join Date
    May 2002
    Posts
    20
    I only know of how to do this in unix, and in C. I don't think there is anything in C++, but you could easily make a class from this.

    You are looking for the library <sys/stat.h>.

    Function: int stat(char *path, struct stat *buf).

    what struct stat is: (i put comments by what you will need)

    struct stat {
    mode_t st_mode;
    ino_t st_ino;
    dev_t st_dev;
    dev_t st_rdev;
    nlink_t st_nlink;
    uid_t st_uid;
    gid_t st_gid;
    off_t st_size; // file size
    time_t st_atime; // time file was last accessed
    time_t st_mtime; // time of last modification
    time_t st_ctime; // time of last file status change...
    // this one is like ctime in that it is in seconds from
    // jan 1, 1970...
    long st_blksize;
    blkcnt_t st_blocks;
    }

    This is just very basic, you'll probably need to look up more on the net, but this should point you in the right direction. One thing your obviously going to have to do is save all the data somewhere, save the last size, last changed time, etc. Then when you check again, see if it changed.

    -Avantos
    "People demand freedom of speech to make up for the freedom of thought which
    they avoid".

  6. #6
    Join Date
    Dec 2002
    Posts
    287
    1) stat (in fact _stat) is also available in Windows but it is just a wrapper around other OS primitives:

    http://msdn.microsoft.com/library/de...._wstati64.asp


    2) You can try fcntl with F_NOTIFY (on Linux at least):

    File and directory change notification
    F_NOTIFY
    (Linux 2.4 onwards) Provide notification when the directory referred to by fd or any of the files that it contains is changed. The events to be notified are specified in arg, which is a bit mask specified by ORing together zero or more of the following bits:
    Bit Description (event in directory)

    DN_ACCESS
    DN_MODIFY A file was modified (write, pwrite,

    writev, truncate, ftruncate)

    DN_CREATE A file was created (open, creat, mknod,

    mkdir, link, symlink, rename)

    DN_DELETE A file was unlinked (unlink, rename to

    another directory, rmdir)

    DN_RENAME A file was renamed within this

    directory (rename)

    DN_ATTRIB The attributes of a file were changed

    (chown, chmod, utime[s])

    Here is a link to the fcntl man pages:
    http://www.item.ntnu.no/cgi-bin/man2html?fcntl+2

    Dan

  7. #7
    Join Date
    Jan 2003
    Location
    Bulgaria
    Posts
    56

    Thumbs up

    Thank you Dan,
    Yesterday I found such as article in Internet - http://nst.dsi.a-star.edu.sg/mcsa/lx...on/dnotify.txt

    The idea is the same with yours. I test it on linux core 2.4.19 onward and it works. But for my sorrow I didn't find something similar on Unix (Solaris). Some gues told me that there isn't such as. This makes me to think that Win is better. But this is another theme which place is not here.

    Regards, Alex

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured