1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| PGDLLEXPORT
Datum vexplorateur(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vexplorateur);
PGDLLEXPORT
Datum
vexplorateur(PG_FUNCTION_ARGS)
{
int r;
char* t;
__int64 avlblspc, ttlspc, frspc;
TCHAR lpBuffer[500], * p;
int nligne;
FuncCallContext *funcctx;
int call_cntr;
int max_calls;
TupleDesc tupdesc;
AttInMetadata *attinmeta;
GetLogicalDriveStrings(sizeof(lpBuffer), lpBuffer);
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
nligne=0;
for(p = lpBuffer; *p != '\0'; p += 4)
{
if(GetDriveType(p)==3||GetDriveType(p)==2)
{
nligne=nligne+1;
}
}
funcctx->max_calls = nligne;
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("fonction renvoyant le type record appelée dans un contexte qui ne peut pas "
"accepter le type record")));
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
p = lpBuffer;
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
attinmeta = funcctx->attinmeta;
p += 4*call_cntr;
if (call_cntr < max_calls)
{
char **values;
HeapTuple tuple;
Datum result;
if(GetDriveType(p)==2||GetDriveType(p)==3)
{
values = (char **) palloc(3 * sizeof(char *));
values[0] = (char *) palloc(100 * sizeof(char));
values[1] = (char *) palloc(100 * sizeof(char));
values[2] = (char *) palloc(100 * sizeof(char));
SetErrorMode(SEM_FAILCRITICALERRORS);
r=GetDiskFreeSpaceEx(p, (PULARGE_INTEGER)&avlblspc, (PULARGE_INTEGER)&ttlspc, (PULARGE_INTEGER)&frspc);
SetErrorMode(0);
t=(char*)malloc(100*sizeof (char));
if(r!=0)
{ sprintf(t,"%d", (ttlspc-frspc)/1000000000);
strcat(t,"GO disponible");
}
else{strcat(t, "Non disponible");}
if(GetDriveType(p)==2)
{snprintf(values[0], 100, "%s", p);
snprintf(values[1], 100, "%s", "Disque amovible");
snprintf(values[2], 100, "%s", t);}
if(GetDriveType(p)==3)
{snprintf(values[0], 100, "%s", p);
snprintf(values[1], 100, "%s", "Disque local");
snprintf(values[2], 100, "%s", t);}
tuple = BuildTupleFromCStrings(attinmeta, values);
result = HeapTupleGetDatum(tuple);
pfree(values[0]);
pfree(values[1]);
pfree(values[2]);
pfree(values);
SRF_RETURN_NEXT(funcctx, result);
}
}
else
{
SRF_RETURN_DONE(funcctx);
}
} |