0

I am making a service that watches a file's readout changes using a stable RecursiveFileObserver , this file observer will is assigned to observe the file Modifications (FileObserver.MODIFY) ... What I want this observer to do onEvent is to change the file's value to what it was before it is changed .. So the cycle is :

Service Starts and observer startsWatching --> File is changed by X --> My Observer changes this to whatever it was before being changed by X ...

So all i need is a way to read what the file's value was before being changed by X .

So can anybody help me in this , and is it even possible ?

This is a snippet of what I am trying to do ...

    @Override
    public void onEvent(int event, String path) {
        // TODO Auto-generated method stub
        super.onEvent(event, path);

        if(event != MODIFY){
            return;
        }

        if (CoresManagement.isAccessAllowed()){ //If the access is allowed , doesn't do anything ...        
            return;
        }

        //Here is where I want to do what I said above

            if (path == "le Wild Location"){
              modifyFile("le Wild Location" , "Here is the value I want to know , the old value " );
            }

please if you find anything unclear i can clarify !

Seaskyways
  • 3,630
  • 3
  • 26
  • 43

1 Answers1

0

what you can do it to implementa a FileObserver

fileObserver = new FileObserver(pathOfFile) {

 @Override
 public void onEvent(int event, String file) {
    // on any change in the file you can replace the old file. 
    //basically keep the backup file some where or in the buffer and replace with this file if there is any change. 
 }
 };
 fileObserver.startWatching(); 

to replace file.

FileChannel origFile = null;
FileChannel modFile = null;
long cnt = 0;
long size = 0;

try {
    origFile = new FileInputStream(sourceFile).getChannel();
    modFile = new FileOutputStream(destFile).getChannel();

    size = origFile.size();              
    while((cnt += modFile.transferFrom(origFile, cnt, size-cnt))<size);
}
finally {
    if(origFile != null) {
        origFile.close();
    }
    if(modFile != null) {
        modFile.close();
    }
}
blganesh101
  • 3,647
  • 1
  • 24
  • 44
  • Thats what I am doing , but the whole purpose of this is that I want my app to change this file while restricting others ,including system, from changing it ... And this procedure is for the restriction part of the process , because of that you see up `if (CoresManagement.isAccessAllowed()`) , access is allowed once my app tries to change the file , after its done , it disallows it ... and by my means disallow , I mean to change the file to what it was before being changed by anyone , and for that , I need to get somehow the file's read out before being changed – Seaskyways Jun 25 '13 at 11:50
  • Can you create a private file? you could use openFileOutput("filename", MODE_PRIVATE) method – blganesh101 Jun 25 '13 at 11:53
  • the file I am changing is withing the /sys directory , and only root and system can change it , thus , for changing the file I am using classic shell commands ( su , echo , cat .... ) ... So no – Seaskyways Jun 25 '13 at 11:57
  • onEvent is called once the file has been changed, so we cannot restrict some process commiting changes at this point. – blganesh101 Jun 25 '13 at 12:07
  • Sometimes onEvent is called with an event MODIFY without the file's readout being changed, like if the file was changed from "hey" to "hey", onEvent is called with event MODIFY, so how can i catch if the file is actually modified or not? – Seaskyways Jun 25 '13 at 13:16