/* OCaml-Win32
 * win32_directory.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: Directory Management Reference */

CAMLprim value create_directory(value name, value security)
{
    CAMLparam2(name, security);
    char* n;
    SECURITY_ATTRIBUTES sa;
    SECURITY_ATTRIBUTES* sap;
    BOOL result;

    n = new_string(name);
    if (Is_some(security)) {
        securityattributes_val(Value_of(security), &sa);
        sap = &sa;
    } else
        sap = NULL;
    enter_blocking_section();
    result = CreateDirectory(n, sap);
    leave_blocking_section();
    free_string(n);
    if (!result)
        raise_last_error();
    CAMLreturn(Val_unit);
}

CAMLprim value create_directory_ex(value templ, value name, value security)
{
    CAMLparam3(templ, name, security);
    char* tmpl;
    char* n;
    SECURITY_ATTRIBUTES sa;
    SECURITY_ATTRIBUTES* sap;
    BOOL result;

    tmpl = new_string(templ);
    n = new_string(name);
    if (Is_some(security)) {
        securityattributes_val(Value_of(security), &sa);
        sap = &sa;
    } else
        sap = NULL;
    enter_blocking_section();
    result = CreateDirectoryEx(tmpl, n, sap);
    leave_blocking_section();
    free_string(tmpl);
    free_string(n);
    if (!result)
        raise_last_error();
    CAMLreturn(Val_unit);
}

CAMLprim value find_close_change_notification(value handle)
{
    if (!FindCloseChangeNotification(Handle_val(handle)))
        raise_last_error();
    return Val_unit;
}

CAMLprim value find_first_change_notification(value path, value watch_subtree, value filter)
{
    HANDLE h;

    h = FindFirstChangeNotification(String_val(path), Bool_val(watch_subtree), flags_val(filter, file_change_types));
    if (h == INVALID_HANDLE_VALUE)
        raise_last_error();
    return alloc_handle(h);
}

CAMLprim value find_next_change_notification(value handle)
{
    if (!FindNextChangeNotification(Handle_val(handle)))
        raise_last_error();
    return Val_unit;
}

CAMLprim value get_current_directory(value unit)
{
    CAMLparam1(unit);
    int len;
    char* buf;
    CAMLlocal1(val);

    enter_blocking_section();
    len = GetCurrentDirectory(0, NULL);
    leave_blocking_section();
    if (len == 0)
        raise_last_error();
    buf = malloc(len);
    enter_blocking_section();
    len = GetCurrentDirectory(len, buf);
    leave_blocking_section();
    if (len == 0) {
        free(buf);
        raise_last_error();
    }
    val = copy_string(buf);
    free(buf);
    CAMLreturn(val);
}

CAMLprim value remove_directory(value name)
{
    CAMLparam1(name);
    char* n;
    BOOL result;

    n = new_string(name);
    enter_blocking_section();
    result = RemoveDirectory(n);
    leave_blocking_section();
    free_string(n);
    if (!result)
        raise_last_error();
    CAMLreturn(Val_unit);
}

CAMLprim value set_current_directory(value name)
{
    CAMLparam1(name);
    char* n;
    BOOL result;

    n = new_string(name);
    enter_blocking_section();
    result = SetCurrentDirectory(n);
    leave_blocking_section();
    free_string(n);
    if (!result)
        raise_last_error();
    CAMLreturn(Val_unit);
}
