Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening Files

Status
Not open for further replies.

areric

Programmer
Jul 13, 2003
47
0
0
US
Ive tried asking this stuff before but unfortunatly i havent quite found a solution yet so i figured id post a bit of an update. Im using MFC to scan a LARGE network storage device, talking 3TB highly nested. I recently changed my scanning algorithm to this.

void scanPath(string path, WIN32_FIND_DATA &fileData)
{
vector <string> dirs;
string dirName;
string pathToScan;
HANDLE searchHandle;
string acctName;
string filename;

pathToScan = combinePath(path, "*");
//cout<<path<<endl;


searchHandle = FindFirstFile(pathToScan.c_str(), &fileData);
//cout<<"after search handle"<<endl;
while (FindNextFile(searchHandle, &fileData))
{
if (strcmpi (fileData.cFileName, ".") != 0 &&
strcmpi (fileData.cFileName, "..") != 0)
{
//cout<<"In While"<<endl;
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
dirs.push_back(fileData.cFileName);
}
filename = combinePath(path, fileData.cFileName);
getOwnerName(filename.c_str(), acctName);
cout<<path<<"\\"<<fileData.cFileName<<","<<acctName<<endl;
}
}
FindClose(searchHandle);

for (int i=0; i<dirs.size(); i++)
{
//cout<<"recursing"<<endl;
dirName = dirs;
scanPath(path+"\\"+dirName, fileData);
}
}

The algorithm is leaving the files marked as open. Two questions

1) Can i get summary file information (file name, size, write and access dates, owner) Without leaving the file open?

2) If not how can i close the file right after i get that information?

Thank you everyone
 
So i realized after i submitted this that its a terribly name for the post. Unbeliveable undescriptive. Sorry everyone.

For those of you that are interested though my GetOwnerName function is off MSDN and looks like this:

int getOwnerName(const char* filename, string &rAcctName){
DWORD dwRtnCode = 0;
PSID pSidOwner;
BOOL bRtnBool = TRUE;
LPTSTR AcctName, DomainName;
DWORD dwAcctName = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
HANDLE hFile;
PSECURITY_DESCRIPTOR pSD;

// Get the handle of the file object.
hFile = CreateFile(
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("CreateFile error = %d-%s\n"), dwErrorCode, filename);
rAcctName="unknown";
return -1;
}

// Allocate memory for the SID structure.
pSidOwner = (PSID)GlobalAlloc(
GMEM_FIXED,
sizeof(PSID));

// Allocate memory for the security descriptor structure.
pSD = (PSECURITY_DESCRIPTOR)GlobalAlloc(
GMEM_FIXED,
sizeof(PSECURITY_DESCRIPTOR));

// Get the owner SID of the file.
dwRtnCode = GetSecurityInfo(
hFile,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSidOwner,
NULL,
NULL,
NULL,
&pSD);

// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("GetSecurityInfo error = %d\n"), dwErrorCode);
rAcctName="unknown";
return -1;
}

// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid(
NULL, // local computer
pSidOwner,
AcctName,
(LPDWORD)&dwAcctName,
DomainName,
(LPDWORD)&dwDomainName,
&eUse);

// Reallocate memory for the buffers.
AcctName = (char *)GlobalAlloc(
GMEM_FIXED,
dwAcctName);

// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
rAcctName="unknown";
return -1;
}

DomainName = (char *)GlobalAlloc(
GMEM_FIXED,
dwDomainName);

// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();
//_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
rAcctName="unknown";
return -1;

}

// Second call to LookupAccountSid to get the account name.
bRtnBool = LookupAccountSid(
NULL, // name of local or remote computer
pSidOwner, // security identifier
AcctName, // account name buffer
(LPDWORD)&dwAcctName, // size of account name buffer
DomainName, // domain name
(LPDWORD)&dwDomainName, // size of domain name buffer
&eUse); // SID type

// Check GetLastError for LookupAccountSid error condition.
if (bRtnBool == FALSE) {
DWORD dwErrorCode = 0;

dwErrorCode = GetLastError();

if (dwErrorCode == ERROR_NONE_MAPPED) {
// _tprintf(TEXT("Account owner not found for specified SID.\n"));
}
else {
// _tprintf(TEXT("Error in LookupAccountSid.\n"));
}
rAcctName = "unknown";
return -1;

}
else if (bRtnBool == TRUE) {
// Print the account name.
//_tprintf(TEXT("Account owner = %s\n"), AcctName);
rAcctName = AcctName;
return 1;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top