0

I'm trying to implement an script that reads the content(files and folders) of a certain directory and writes it in a database. My goal is create an software that allows me to organize those files and folders relating description and tags to them, without affecting the correspondig physical files in the disk. But for now I'm facing a logical problem: How do I make a direct connection between that physical file and the database register? I want that, even if the physical file, for some reason, is edited or moved to another folder inside the root directory, the software is still able to relate that file with its original register in the database.

My first idea was to use a checksum hash to identify every file but, I'm guessing that if the file is edited, so does the hash, doesn't it? Besides that, I also think that a folder itself can't be checked that way.

Another solution that came up to my mind was applying a unique key in the beginning of every file and folder name in the directory. That may work, but it seems to me like an improvised solution and, therefore, I'mhoping that there may be another way to do it that I haven't considered yet. Does anyone have an advice on that?

jonathan.hepp
  • 1,603
  • 3
  • 15
  • 21
  • 1
    how about `http://stackoverflow.com/questions/511463/monitor-directory-for-changes` , Assuming you are on "*nix" – sakhunzai Apr 02 '12 at 14:12
  • Thank you very much @sakhunzai. And yes, I should have informed that in fact the directory is in a local Linux server that shares its content through Samba to Windows. – jonathan.hepp Apr 02 '12 at 14:19

2 Answers2

1

Under Linux, you can track files by inode, which won't change if the file gets moved or renamed. You can even get notified when a file changes.

have you considered storing the file in the database?

dj_segfault
  • 11,957
  • 4
  • 29
  • 37
  • Thank you! I didn't know about inotify. I'm reading about it right now and it seems good. – jonathan.hepp Apr 02 '12 at 14:21
  • Ok. After making some tests the inode number fits all my needs. When a file or folder is created, the inode number is created and it's only deleted when the referred file or folder is deleted. It remains the same after moving or editing the file, as well. – jonathan.hepp Apr 02 '12 at 17:00
0

You can't.

It looks that there is no way to identify the file: neither by content nor by pathname.

One workaround might be: use path as id (and use them as reference in the DB) and do not use system tools (like mv) to move files but your own script which updates the file system and the database.

Andreas Florath
  • 4,418
  • 22
  • 32