Files
fstester/getter/os2_32/DIR.C

241 lines
9.3 KiB
C++
Raw Normal View History

/****************************************************************************
2020-03-01 16:56:27 +00:00
Aaru Data Preservation Suite
-----------------------------------------------------------------------------
Filename : dir.c
Author(s) : Natalia Portillo
Component : fstester.getter.os2
--[ Description ] -----------------------------------------------------------
Contains directory handlers
--[ License ] ---------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warraty of
MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------
2020-12-31 23:13:50 +00:00
Copyright (C) 2011-2021 Natalia Portillo
*****************************************************************************/
#define INCL_DOSFILEMGR // File Manager values
2019-01-27 20:33:59 +00:00
#define INCL_DOSERRORS // DOS error values
#include "dir.h"
#include "ea.h"
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
2019-01-27 20:33:59 +00:00
#include <string.h>
int GetAllInDir(PSZ path, ULONG *eaCounter)
{
HDIR hdirFindHandle;
FILEFINDBUF3 FindBuffer = {0}; // Returned from FindFirst/Next
ULONG ulResultBufLen;
ULONG ulFindCount;
APIRET rc; // Return code
PSZ pathWithWildcard;
PSZ fullPath;
ULONG flAttribute; // File attributes
2019-01-27 20:33:59 +00:00
int isDir = 0;
APIRET drc;
// Variables for EA saving
2019-01-27 20:33:59 +00:00
char * eaBuffer;
size_t eaSize;
char eaPath[8 + 1 + 3 + 1];
HFILE eaFileHandle; // Address of the handle for the file
2019-01-27 20:33:59 +00:00
ULONG ulAction =
0; // Address of the variable that receives the value that specifies the action taken by the DosOpen function
APIRET earc;
for(isDir = 0; isDir < 2; isDir++)
{
hdirFindHandle = HDIR_CREATE;
ulResultBufLen = sizeof(FILEFINDBUF3);
2019-01-27 20:33:59 +00:00
ulFindCount = 1; // Look for 1 file at a time
rc = NO_ERROR; // Return code
pathWithWildcard = malloc(strlen(path) + 5);
strcpy(pathWithWildcard, path);
2019-01-27 20:33:59 +00:00
strcat(pathWithWildcard, "\\*.*"); // Adds wildcard to passed path
flAttribute = FILE_ARCHIVED | FILE_SYSTEM | FILE_HIDDEN | FILE_READONLY; // All files
2019-01-27 20:33:59 +00:00
if(isDir) flAttribute |= MUST_HAVE_DIRECTORY; // Must be a directory
rc = DosFindFirst(pathWithWildcard, // File pattern
2019-01-27 20:33:59 +00:00
&hdirFindHandle, // Directory search handle
flAttribute, // Search attribute
&FindBuffer, // Result buffer
ulResultBufLen, // Result buffer length
&ulFindCount, // Number of entries to find
FIL_STANDARD); // Return Level 1 file info
if(rc != NO_ERROR)
{
free(pathWithWildcard);
2019-01-27 20:33:59 +00:00
if(rc == ERROR_NO_MORE_FILES && !isDir) continue;
printf("DosFindFirst error: return code = %u\n", rc);
return rc;
}
else
{
if(strcmp(FindBuffer.achName, ".") != 0 && strcmp(FindBuffer.achName, "..") != 0)
{
fullPath = malloc(strlen(path) + 2 + strlen(FindBuffer.achName) + 1);
2019-01-27 20:33:59 +00:00
fullPath = strcpy(fullPath, path); // Parent path
strcat(fullPath, "\\"); // Adds slashes
strcat(fullPath, FindBuffer.achName); // Adds filename
if(isDir)
printf("%s\\\n", fullPath); // Print directory name
else
printf("%s\n", fullPath); // Print file name
if(strcmp(FindBuffer.achName, ".") != 0 && strcmp(FindBuffer.achName, "..") != 0)
{
eaSize = 0;
GetEAs(fullPath, &eaBuffer, &eaSize);
if(eaSize != 0)
{
sprintf(&eaPath, "%08d.EA", *eaCounter);
2019-01-27 20:33:59 +00:00
earc = DosOpen(eaPath,
&eaFileHandle,
&ulAction,
0L,
FILE_NORMAL,
OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
2019-01-27 20:33:59 +00:00
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
NULL);
if(earc == NO_ERROR)
{
earc = DosWrite(eaFileHandle, eaBuffer, eaSize, &ulAction);
if(earc == NO_ERROR)
{
2019-01-27 20:33:59 +00:00
printf("\tSaved %ld bytes from %ld bytes of EAs saved to %08d.EA\n",
ulAction,
eaSize,
*eaCounter);
*eaCounter += 1;
}
DosClose(eaFileHandle);
}
free(eaBuffer);
}
if(isDir)
{
drc = GetAllInDir(fullPath, eaCounter);
if(drc != NO_ERROR && drc != ERROR_NO_MORE_FILES)
{
printf("GetAllInDir(%s) returned %u\n", fullPath, drc);
return drc;
}
}
}
free(fullPath);
}
}
while(rc != ERROR_NO_MORE_FILES)
{
ulFindCount = 1; // Reset find count
rc = DosFindNext(hdirFindHandle, // Directory handle
2019-01-27 20:33:59 +00:00
&FindBuffer, // Result buffer
ulResultBufLen, // Result buffer length
2019-01-27 20:33:59 +00:00
&ulFindCount); // Number of entries to find
if(rc != NO_ERROR && rc != ERROR_NO_MORE_FILES)
{
free(pathWithWildcard);
printf("DosFindNext error: return code = %u\n", rc);
return rc;
}
else
{
2019-01-27 20:33:59 +00:00
if(strcmp(FindBuffer.achName, ".") == 0 || strcmp(FindBuffer.achName, "..") == 0) continue;
fullPath = malloc(strlen(path) + 2 + strlen(FindBuffer.achName) + 1);
2019-01-27 20:33:59 +00:00
fullPath = strcpy(fullPath, path); // Parent path
strcat(fullPath, "\\"); // Adds slashes
strcat(fullPath, FindBuffer.achName); // Adds filename
if(isDir)
printf("%s\\\n", fullPath); // Print directory name
else
printf("%s\n", fullPath); // Print file name
eaSize = 0;
GetEAs(fullPath, &eaBuffer, &eaSize);
if(eaSize != 0)
{
sprintf(&eaPath, "%08d.EA", *eaCounter);
2019-01-27 20:33:59 +00:00
earc = DosOpen(eaPath,
&eaFileHandle,
&ulAction,
0L,
FILE_NORMAL,
OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
2019-01-27 20:33:59 +00:00
OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
NULL);
if(earc == NO_ERROR)
{
earc = DosWrite(eaFileHandle, eaBuffer, eaSize, &ulAction);
if(earc == NO_ERROR)
{
2019-01-27 20:33:59 +00:00
printf("\tSaved %ld bytes from %ld bytes of EAs saved to %08d.EA\n",
ulAction,
eaSize,
*eaCounter);
*eaCounter += 1;
}
DosClose(eaFileHandle);
}
else
printf("Error %d calling DosOpen\n", earc);
free(eaBuffer);
}
if(isDir)
{
drc = GetAllInDir(fullPath, eaCounter);
if(drc != NO_ERROR && drc != ERROR_NO_MORE_FILES)
{
printf("GetAllInDir(%s) returned %u\n", fullPath, drc);
return drc;
}
}
free(fullPath);
} /* endif */
2019-01-27 20:33:59 +00:00
} /* endwhile */
free(pathWithWildcard);
rc = DosFindClose(hdirFindHandle); // Close our directory handle
if(rc != NO_ERROR)
{
printf("DosFindClose error: return code = %u\n", rc);
return rc;
}
}
return ERROR_NO_MORE_FILES;
}