FindFirstChangeNotification API - file

I am using FindFirstChangeNotification API to monitor the changes happening in a particular folder.But how to exclude a particular file(present in the watching folder) change notification Only.

It works at the directory level, if you want to exclude a specific file then just ignore any notifications about it in you application logic.

Use ReadDirectoryChanges(), it monitors files in a directory tree. ReadDirectoryChanges is basically doing the same thing as FindFirstChangeNotification, FindNextChangeNotification. ReadDirectoryChanges is just more powerful because if you provide the optional callback function to ReadDirectoryChangesW(), you can see which file changed, and why it changed, and then filter in your application logic without overhead of any system call(s) to find which file changed, ...you get this array of structures.
typedef struct _FILE_NOTIFY_INFORMATION {
DWORD NextEntryOffset;
DWORD Action; // <- reason for the change
DWORD FileNameLength;
WCHAR FileName[1];
} FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;
FindNextChangeNotification is more like a sledgehammer, you still need to check the folder to see what exactly changed, but it easier to use if you already know which file to hunt for. Findfirst/Next also slightly easier to use in terms of thread waiting/IO completion logic.

Related

Can I access a folder right after it's created

This maybe because I'm used to backend and async in js but if I type this in rust..
fs::create_dir("dir1")
Then use
fs::File::create("dir1/file1.txt")
To create a file in the new folder right after,
is it bound to go wrong, should the program sleep for a second to make sure the directory has already been created on the environment?
To answer this, No it is not bound to go wrong.
And also to add in general, sleeping to ensure that something is being done is not that of a good idea.
In the case you provided, fs::create_dir is sync and it returns a Result type. So before you create file inside dir1 make sure you check the result is ok. And also check the result for creating file is ok.
fn create_file(){
if fs::create_dir("dir1").is_ok() {
if fs::File::create("dir1/file1.txt").is_ok() {
println!("File created inside dir1");
}
}
}
Also, while inside rust you can take the gurantee that once directory is created it stays there but this won't always be case. For example, a system might have a background job that keeps deleting the directory inside some place. Hopefully, you don't always have to worry about all these extreme cases but just so you know ;)

How can I load a memory stream into LibVLC?

I want to play a media file from a memory stream using LibVLC like so:
//Ideally it would go like this:
LibVLC.MediaFromStream = new MemoryStream(File.ReadAllBytes(File_Path));
Of course this is a very oversimplified version of what I want but hopefully it conveys what I am looking for.
The reason being that I want there to be a good amount of portability for what I'm doing without having to track file locations and such. I'd rather have a massive clump of data in a single file that can be read from than have to track the locations of one or many more files.
I know this has something to do with the LibVLC IMEM Access module. However, looking at what information I've been able to find on that, I feel like I've been tossed from a plane and have just a few minutes to learn how to fly before I hit the ground.
See my answer to a similar question here:
https://stackoverflow.com/a/31316867/2202445
In summary, the API:
libvlc_media_t* libvlc_media_new_callbacks (libvlc_instance_t * instance,
libvlc_media_open_cb open_cb,
libvlc_media_read_cb read_cb,
libvlc_media_seek_cb seek_cb,
libvlc_media_close_cb close_cb,
void * opaque)
allows just this. The four callbacks must be implemented, although the documentation states the seek callback is not always necessary, see the libVlc documentation. I give an example of a partial implementation in the above answer.
There is no LibVLC API for imem, at least not presently.
You can however still use imem in your LibVLC application, but it's not straightforward...
If you do vlc -H | grep imem you will see something like this (this is just some of the options, there are others too):
--imem-get <string> Get function
--imem-release <string> Release function
--imem-cookie <string> Callback cookie string
--imem-data <string> Callback data
You can pass values for these switches either when you create your libvlc instance via libvlc_new(), or when you prepare media via libvlc_media_add_option().
Getting the needed values for these switches is a bit trickier, since you need to pass the actual in-memory address (pointer) to the callback functions you declare in your own application. You end up passing something like "--imem-get 812911313", for example.
There are downsides to doing it this way, e.g. you may not be able to seek backwards/forwards in the stream.
I've done this successfully in Java, but not C# (never tried).
An alternative to consider if you want to play the media data stored in a file, is to store your media in a zip or rar since vlc has plugins to play media from directly inside such archives.

Node.JS: How does "fs.watchFile" work?

According to the API docs for Node 0.4.3, the fs.watchFile(filename, [options], listener) function starts a routine that will
Watch for changes on filename. The callback listener will be called each time the file is accessed.
It also says
The options if provided should be an object containing two members a boolean, persistent, and interval, a polling value in milliseconds
Which indicates that it will check every so often based on what is in interval. But it also says
The default is { persistent: true, interval: 0 }.
So does that mean it will check every millisecond to see if the file time changed? Does it listen for OS level events? How does that work?
Yes, cpedros is correct, this does seem to be a duplicate. I think I can shed some more light on this though.
Each OS has its own file change event that gets fired. On Linux, it is inotify (used to be dnotify), on Mac it is fsevents, and on Windows it is FileSystemWatcher. I'm not sure if the underlying code handles each case, but that's the general Idea.
If you just want to watch a file on Linux, I recommend node-inotify-plus-plus. If you want to watch a directory, use inotify-plus-plus with node-walk. I've implemented this and it worked like a charm.
I can post some code if you're interested. The beauty behind node-inotify-plus-plus is that it abstracts much of the nastiness of inotify and gives an intuitive API for listening to specific events on a file.
EDIT: This shouldn't be used to watch tons of files. On my system, the max is 8192. Your max can be found by using this command cat /proc/sys/fs/inotify/max_user_watches. This could be used to just watch directories for changes and then figure out the individual files from there. A modified event will fire if a file directly under that directory is modified.
EDIT: Thanks #guiomie for pointing out that watching files is now fully supported on Windows. I assume this is with the v0.6.x release.
To extend on tjameson's fantastic answer, you could use watchr to normalise the API between the node versiosn and OS watching differences. It also provides events for unlink and new instead of just change, as well as adds support for directory tree watching.

calling custom actions from File instead of Binary

I've found lots of examples of calling custom actions in WiX using Binary element, but none examples where a File element was used. Can anyone give me an example?
Not strictly true about needing to run the action deferred! You can use the InstallExecute action to run all the spooled actions up to that point, including, for example file installation. After that you could schedule an immediate action which depends on the newly-installed file, which at this point will be present.
That said, if the file is going to make any changes to the machine state, then the CA really needs to be deferred in system context, so InstallExecute doesn't really buy you anything.

How to reset an object's security descriptor to the default?

As part of a testing utility I am creating some registry keys and applying a specific security descriptor to them. Later on I want to reset it to the "default" security descriptor (i.e. inherited from the parent). What is the proper way to do this?
I can't save and restore the original security descriptor because this utility may be run multiple times before the tester will want to reset it. I guess I could save it to a temp file or registry value, but I would prefer a more elegant solution.
So, do I have to do something with the parent's security descriptor or what? I'm having a hard time figuring out what to do.
Almost forgot to mention I'm doing this in C.
UPDATE: I should have added that I'll also be doing this with files (and possibly other securable objects), so it would be nice if there were a generic way to work with security descriptors themselves instead of using object-specific things like RegSaveKey. I imagine it would require working with the security descriptor of the parent, so it would be great if I could do something like the following:
BOOL WINAPI GetDefaultChildSecurityDescriptorFromParent(LPSECURITY_DESCRIPTOR Parent, LPSECURITY_DESCRIPTOR* Child);
I'm just not sure how to do it programmatically. You can accomplish this in the security descriptor editor by using the check box to inherit entries from the parent, so obviously it is possible somehow.
I recommend saving keys to a file using RegSavekey. To restore the key use RegLoadKey.
The easiest way I can think to do this would be to read in the structure that needs to be defaulted... then delete it and recreate it - passing NULL to force the defaults.
I hate to answer my own question, but I found a snippet of documentation on the matter (the DACL is really the only thing I am concerned with). Looks like I have to get the DACL of the parent and create a new DACL that includes all the inheritable ACEs in it. I was hoping it would be simpler than that, but it's not too bad.

Resources