/* OCaml-Win32
 * win32_disk.c
 * Copyright (c) 2002-2004 by Harry Chomsky
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "win32.h"

/********************************************************************/
/* System Services: Files and I/O: SDK Documentation: Storage: Storage Reference: Disk Management Reference */

CAMLprim value get_disk_free_space(value path)
{
    CAMLparam1(path);
    char* p;
    DWORD sectors, bytes, free, total;
    BOOL result;
    CAMLlocal2(val, v);

    p = new_stringoption(path);
    enter_blocking_section();
    result = GetDiskFreeSpace(p, &sectors, &bytes, &free, &total);
    leave_blocking_section();
    free_stringoption(p);
    if (!result)
        raise_last_error();
    val = alloc_tuple(4);
    Store_field(val, 0, Val_int(sectors));
    Store_field(val, 1, Val_int(bytes));
    v = copy_int32(free);
    Store_field(val, 2, v);
    v = copy_int32(total);
    Store_field(val, 3, v);
    CAMLreturn(val);
}

CAMLprim value get_disk_free_space_ex(value path)
{
    CAMLparam1(path);
    char* p;
    ULARGE_INTEGER avail, total, free;
    BOOL result;
    CAMLlocal2(val, v);

    p = new_stringoption(path);
    enter_blocking_section();
    result = GetDiskFreeSpaceEx(p, &avail, &total, &free);
    leave_blocking_section();
    free_stringoption(p);
    if (!result)
        raise_last_error();
    val = alloc_tuple(3);
    v = copy_int64(avail.QuadPart);
    Store_field(val, 0, v);
    v = copy_int64(total.QuadPart);
    Store_field(val, 1, v);
    v = copy_int64(free.QuadPart);
    Store_field(val, 2, v);
    CAMLreturn(val);
}
