P
P
Pepsilike2015-03-30 22:42:17
JavaScript
Pepsilike, 2015-03-30 22:42:17

What is the algorithm of the module for logging file changes?

There is an interesting task: it is necessary to read the initial data from the A_file file in the A directory , save it to some local variable. Then hang some watcher on this file , which would monitor the change in this file, each time the file changes, you should read the data again and output only changes to the log compared to the last data.
What is the essence of the problem: first of all, I created a stream for reading using the 'fs.createReadStream' method , hung a watcher on the file using 'fs.watch' , then through another stream I write some data to this file (well, or just through notepad), so when the event ' change' I can't figure out how to reread the file, my previously created stream is supposedly already closed. I feel like I'm doing something fundamentally wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-03-31
@Pepsilike

And the stream is not needed here, do fs.readFile, then put fs.watch and when changing fs.readFile to another variable and compare, you just need to create another queue of comparisons, because the file can change more often than the previous comparison ends (but this is purely theoretical, if, according to the specifics of the problem, it is known that it changes with a small periodicity, then this can be omitted). The result of fs.readFile is a Buffer object, for comparing two buffers, most likely, if they contain text, they can be converted to .toString() strings and parsed. But to understand whether the buffers are identical, you can use Buffer.compare(buf1, buf2).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question