Detect if a text editors buffer file is open

I am trying to write a go program that will check if a file is being edited in a editor like atom with unsaved changes. I can’t figure out how to detect that. Any ideas?

Hey @kwtucker, I haven’t personally tried this before, but I’m thinking that you could loop on a specified interval of time and check if a file’s last access time is less than it’s last modified time, and if on any iteration it’s different, there’s probably unsaved changes.

I’m sure you could do other things along those lines but I would probably start trying something like that… Don’t know if there’s a much easier way since there probably is.

1 Like

I am currently reading the file, but I need to write to the file. If the file is being edited while I write to the file, and the unsaved changes are saved it undos what I just wrote. The unsaved changes are ahead of my write in a buffer. I want to check if that buffer exists and skip it until a on save event. Some how…
Thank you for replying so fast.

1 Like

Maybe you can check the above that I mentioned and if at a specific stage it’s safe to write, lock the file, see https://en.wikipedia.org/wiki/File_locking, then defer to unlock the file so the other process can write afterwards? Once again there is probably a better solution :stuck_out_tongue:, just thinking off the top of my head here.

Yeah, I thought about locking the file and unlocking it after. The program is really fast, so I don’t think it wouldn’t cause a long delay. I really want to find that hidden .swp buffer.

If you are working somewhere where there are .swp files, you could probably just find all files in the folder containing the file and check if a .swp file exists (in a loop every specified interval of time) and if not, lock the file, write, then continue. Probably don’t even need the locking.

https://golang.org/pkg/os/#FileMode
I looked at the os file stat function and it returns a interface of functions that you can use the Mode() and the return is a group of const that have a ModeTemporary? You think this is a good option? Never used stat before.

Sorry when I said stat I meant to list the files in the directory. Was typing faster than I was thinking. You could use something like ioutil.ReadDir: https://golang.org/pkg/io/ioutil/#ReadDir.

Edit: Although checking what you said from stat (ModeTemporary) could be actually useful too I’m sure.

I should really just mention here as well that I’m no expert so take anything I say with a grain of salt.

Some other people on this forum will probably give you much better and more concise answers than I’m giving you.

I’m just trying to help by thinking about what I would start off trying in your situation :slight_smile:

1 Like

I really appreciate you trying and helping, we are learning together. Thank you. I am going to try the things we talked about.

1 Like

I love trying to figure out things that I’ve never tried before and also love helping people out here when I’m able to, so anytime I can help you or somebody else I definitely will :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.