Get permissions of a file or folder - c++

Iam trying to get permissions on file/folder for current user. I found nice article about it here. I have tried to run this program but I got few erros and I dont know where I can find solutions to them. I tried to make my own version. Unfortunately when i try:
LPWSTR lpszPrimaryDC = NULL;
NetGetDCName(NULL, L"A", (LPBYTE *)&lpszPrimaryDC);
I got error: NERR_DCNotFound. How I can solve this problem?

The documentation says that error is returned when it "Could not find the domain controller for the domain specified in the domainname parameter." Do you have a domain called "A"? If not, the function is right to fail (and you need to rethink why/how you are calling it).

The only way the code didn't crash and gave me correct answer to question: is file or folder readable?
`
FILE *myFile = fopen(dirPath, "r");
if (myFile == 0) {
// "File or Dir is not readable
}
`
Hope this helps. You can use the same for writing test with "w".

Related

FMX 3D and fopen() function

New to C++ Builder 10.4. Migrating from XE4.
A line of code fopen() that works fine in the old environment
InputFileHandle = fopen(FileName, "rb");
does not return a valid pointer in the FMX 3D environment for a file in the same directory that the project is running.
I'd appreciate if you can give me a solution for that.
Try to for check which error occur
#include <errno.h>
....
FILE *InputFileHandle;
if ((InputFileHandle = fopen(FileName, "rb")) == NULL)
printf(" %s \n", strerror(errno));
I can see only two possible trouble:
Permission error, you'll have to fix the file permission.
file does not exist meaning your new framework change the current directory.
Thank you PHE for the answer and the clue you posted for my question. I checked the permissions on the file and they are accessible as needed. The issue was the "change of current directory" and that was causing the file not to be recognized. One solid answer is to specify the path to the file from the root directory( this could be cumbersome for long file paths!). For windows path to be recognized you shall specify forward-slash twice in the related string to be decoded properly, like this "C://dir1//dir2//....//filename". Regards
Thank you PHE for the answer and the clue you posted for my question. I checked the permissions on the file and they are accessible as needed. The issue was the "change of current directory" and that was causing the file not to be recognized. One solid answer is to specify the path to the file from the root directory( this could be cumbersome for long file paths!). For the windows path to be recognized you shall specify backward-slash twice in the related string to be decoded properly, like this "C:\dir1\dir2\....\filename". Regards

Reading a saved EVTX file in the given path

I am currently working on a project where I need to read windows events .
I am using OpenEventLog() and ReadEventLog() from Win API. I can read the events from system using the typename of the event.
But I need to specify the file name or file path of the .evtx file that I have saved from the EventViewer.I tried the below code,
HANDLE logHandle = OpenEventLog(NULL, "C:\\Users\\MyAccount\\Documents\\myevents.evtx");
DWORD status = GetLastError();
if(logHandle == NULL){
cerr<<"NO HANDLE GENERATED!!!"<<endl;
}else if(status == ERROR_INVALID_HANDLE){
cerr<<"INVALID HANDLE!!!"<<endl;
}else if(status!=0){
cout<<"OPENEVENTLOG ERROR STATUS::>"<<status<<endl;
}
But it does not find the specified file and switches over to default Application Events. Can anyone please tell me what the problem could be? or if there is anything to be changed in the code?
PS: I even tried placing the file in project folder and specifying just the filename(myevents.evtx) , but still doesn't work.I also tried reading the evtx directly as shown in "Reading .evt/.evtx files directly" , but later I found this can't be done. Apparently there is no way to read them directly without win API or without writing a whole bunch of parser code.
Thanks in advance.
Well, it turns out OpenEventLog() is not meant for opening saved .evtx files.
I should've been using OpenBackupEventLog() for that.

C++ - What permissions does stat() need?

I've got some problems using the stat() function.
What I'm doing is very simple, and I remember it worked on another machine.
I have a directory structure like this:
/home/bernd/dir
/home/bernd/dir/file.ext
/home/bernd/dir/anotherDir
and so on...
What I want to do is to distinguish between files and directories with this source code:
DIR *dir = opendir("/home/bernd/dir");
struct dirent *pent;
while(pent = readdir(dir))
{
if((strcmp(pent->d_name,".") == 0) || (strcmp(pent->d_name,"..") == 0)
continue;
struct stat st;
string tmp = "/home/bernd/dir/" + pent->d_name;
if(stat(tmp.c_str(),&st) == -1)
cout<<strerror(errno);
else
//would be happy to get here
}
As you can see I'm simply walking through the directory and calling stat on the current element, but the stat call always returns Permission Denied. I thought I was messing with relative paths at first, or I was calling stat on the wrong path, which is kept in the string tmp, but I checked them and everything was fine.
The next thing was of course to change the permissions of the files and directories, so anyone can read and write, but the result did not change.
I really hope you guys can help me in any way and your help is highly appreciated!
Thank you in advance!
Do you have execute permission to /home/bernd/dir? Read permission only allows you to list the directory without necessarily being able to access any of its contents.
(On the other hand, execute permission without read permission lets you access the contents but makes the directory unlistable (readdir would fail).)

C++/Linux How can I get read permission stat for other users?

I'm writing a simple C++ webserver assignment and I want to check if the file is readable by other users. If not, the server will send back 403 Forbidden.
I already have a statbuf.st_mode using stat(FILE,&statbuf) but I don't know how to retrieve the read permission for other users. I know there is "S_IROTH" but I don't how to use it. I tried to print it to terminal using cout<<S_IROTH<<endl; and it was 4 but "FILE" has a permission of 0440 so I guess I was not printing the S_IROTH of "FILE".
So my question is: How to get read permission stat for other users?
Or am I making any mistake in concept here?
Thank you.
You need to mask the mode of the file against S_IROTH. Also, you're passing statbuf incorrectly (and you should be getting a warning for it). The correct code should look like:
int result = stat(path, &statbuf);
if (result != 0) {
return NOT_FOUND;
}
if (!(statbuf.st_mode & S_IROTH)) {
return FORBIDDEN;
}
... success, continue ...

Error loading cr2 with edsdk

I am trying to read cr2 images using canon sdk (canon_edsdk-2.12).
I seem to be loading the dll correctly, but when I try to get the actual image, I get an error.
I tried to run the sample program to see how that is different than mine, but the same thing happens.
Trying to look for the issue on the web, I found the actual source code of the sample: http://read.pudn.com/downloads107/sourcecode/graph/texture_mapping/440409/RAWDevelop/RAWDevelopDlg.cpp__.htm
My error, on the given source, is in the void CRAWDevelopDlg::LoadImage() function -
err = EdsGetImage( m_ImageRef , source , kEdsTargetImageType_RGB , rect , size , DstStreamRef );
if( err == EDS_ERR_OK ) {...}
else
{
AfxMessageBox("The error occurred with the EdsGetImage function.");
}
The above (on line 481 on the page) is the same method that I use, and i get the same error - with error code 35 (instead of 0).
The error seems to be
#define EDS_ERR_FILE_OPEN_ERROR 0x00000023L
So... could there be something wrong with the files ? I experimented with files taken by different versions, including the newest cameras... The files open in Photoshop... And the demo does show header information, as it gives the error. So it can see something.
Am I missing anything ?
All the required dll's used are on the system path...
Thank you.
Old question, still, might help someone:
To open a raw file with the SDK you need to call these functions (you should check for errors, of course):
EdsStreamRef stream = NULL;
EdsImageRef imgRef = NULL;
EdsCreateFileStream("filename", kEdsFile_OpenExisting, kEdsAccess_Read, &stream);
EdsCreateImageRef(stream, &imgRef);
EdsRelease(stream);
Then you can set and get properties with the imgRef.
To save the image as jpg/tiff/RGB image use EdsSaveImage function.