开发者

Is a file opened in another application?

Is there a way for me to tell if a particular file is op开发者_高级运维en in another application?

I have a feature I'd like to provide before opening a file in my application, i want to know whether the file is already opened or not.

I want to put the functionality that if its already opened in another application then my application restrict the file to open it again to save the loss of information.

thanks in advance...


There is file locking for this kind of tasks:

http://linux.die.net/man/2/flock


On Linux, you can use "lsof" to see if the file is open.

http://linux.about.com/library/cmd/blcmdl8_lsof.htm


You must have implemented some solution by now but, the way i understand your design, I suggest against your it.

You should allow the user to open the file and check the for time stamps to find out if its been modified before allowing user to save the file (may be ask user whether to overwrite the file or save it as new file etc.).

Look at stat() API.

-Before you open the file note down the timestamp (st_mtime, see structure below)

-Let users modify the file.

-Before user saves the file check the timestamp of the file again st_mtime and if it differs then ask questions to the user.

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜