obsidian-api icon indicating copy to clipboard operation
obsidian-api copied to clipboard

any way to change mtime only?

Open fyears opened this issue 3 years ago • 10 comments

Hi,

Is there any way to change mtime only? (which is similar to Linux's touch command).

I found there are modify() and modifyBinary(). But the content (data) must be provided, which means that though I could change the mtime by writing same content, it's very inefficient. I also tried to pass undefined to data but received no luck.

Another related issue is that the api also lacks the support of appending file instead of rewriting file. Is it also possible to do that?

Thank you so much!

fyears avatar Oct 19 '21 02:10 fyears

btw, i am developing plugins for mobile obsidian, so nodejs fs module is not available……

fyears avatar Oct 19 '21 03:10 fyears

I currently don't see a way to set mtime directly. What's your use case?

As for append, I'll look into it.

lishid avatar Oct 19 '21 15:10 lishid

Hi,

I am developing a plugin about synchronizing notes (definitely NOT a replacement for the official solution but something meeting my personal needs).

The algorithm might involve comparing and/or setting the mtime of files and folders. (BTW, if possible, the ability of "appending file" besides "rewriting" might be extremely helpful.) But I dig into the current api, the only possible way is rewriting the file to reset the mtime.

Thank you for your reply. And Obsidian (mobile) is really great! 👍

fyears avatar Oct 19 '21 16:10 fyears

Why do you need to write the mtime separately from writing the file? With Obsidian Sync, we store the mtime when a file syncs down with the write() function. I'm not sure I understand in what case you would perform just a change to the mtime without also wanting to set the contents. (I guess if the file has "changed" but the content remained identical?)

Also what do you need append for in a synchronization situation?

lishid avatar Oct 19 '21 16:10 lishid

For example, we want that a file has the same mtime in local and on remote. But after it is uploaded (synced) to the remote, the mtime of file on the remote are different from that in local (because uploading also takes time). We need to set the local mtime the same as the remote one. So we can identify the files that are absolutely different if their mtimes are different.

fyears avatar Oct 19 '21 17:10 fyears

appending might be needed while something is being logged (or cached), which is usually appended only for most of the time

fyears avatar Oct 19 '21 17:10 fyears

I will add append for the next insider release.

For example, we want that a file has the same mtime in local and on remote. But after it is uploaded (synced) to the remote, the mtime of file on the remote are different from that in local (because uploading also takes time). We need to set the local mtime the same as the remote one. So we can identify the files that are absolutely different if their mtimes are different.

I don't think this is a good way to solve the problem because it's a workaround for something else with possible consequences. Your remote host should be able to store mtimes based on the uploaded file. Your problem here is that if your sync solution wasn't running for a while, the moment it runs will change the modified time of any files that was edited during the downtime, which IMO isn't desired behavior - if I disconnect from sync for a year, then every note I modify during that year will have their mtime updated to now() when I reconnect to sync.

Obsidian Sync stores an additional "mtime" (and ctime) field in the server for the file. I'm guessing you can't because you're storing it directly in the file system - I'd propose figuring out a way to set the mtime on your remote server so the times match, instead of back-propagate the mtime from the server after upload.

lishid avatar Oct 19 '21 17:10 lishid

if I disconnect from sync for a year, then every note I modify during that year will have their mtime updated to now() when I reconnect to sync.

In this situation, the local file is newer (by comparing mtimes) and should be synced to the remote.

I'm guessing you can't because you're storing it directly in the file system

Haha y are right! Something like FTP/webdav. I try to figure out a “clean” way now.

fyears avatar Oct 19 '21 18:10 fyears

Yeah so if I understand it correctly it will end up with something like this:

  • 2020-01-01 Disconnect sync
  • 2020-06-01 Modify my file "note.md"
  • 2020-12-01 Reconnect sync
  • Uploads note.md
  • Remote now says note.md modified on 2020-12-01 (because it was just uploaded)
  • Update the local mtime to 2020-12-01 (because the times don't match)
  • Original mtime is now lost

Is that correct?

lishid avatar Oct 19 '21 18:10 lishid

Yes u are correct.

fyears avatar Oct 19 '21 18:10 fyears

This should now be achiveable with append - just append nothing, and set the write options to overwrite the mtime.

lishid avatar Sep 23 '22 00:09 lishid