Example #1
1
static BOOL
register_monitoring_entry(WDM_PEntry entry)
{
    BOOL success;
    DWORD bytes;
    bytes = 0; // Not used because the process callback gets passed the written bytes

    success = ReadDirectoryChangesW(
        entry->dir_handle,                  // handle to directory
        entry->buffer,                      // read results buffer
        WDM_BUFFER_SIZE,                    // length of buffer
        entry->user_data->watch_childeren,  // monitoring option
        entry->user_data->flags,            // filter conditions
        &bytes,                             // bytes returned
        &entry->event_container,            // overlapped buffer
        &handle_entry_change                // process callback
    );

    if ( ! success ) {
        WDM_DEBUG("ReadDirectoryChangesW failed with error (%d): %s", GetLastError(), rb_w32_strerror(GetLastError()));
        return FALSE;
    }

    return TRUE;
}
Example #2
0
// TODO:
//   1. this function uses a lot of 'alloca' calls, which AFAIK is not recommeneded! Can this be avoided?
//   2. all wcscat calls can be done faster with memcpy, but is it worth sacrificing the readability?
static VALUE
extract_absolute_path_from_notification(const LPWSTR base_dir, const PFILE_NOTIFY_INFORMATION info) {
    LPWSTR buffer, absolute_filepath;
    WCHAR file[_MAX_FNAME], ext[_MAX_EXT], filename[WDM_MAX_FILENAME];
    DWORD filename_len, absolute_filepath_len;
    LPSTR multibyte_filepath;
    int multibyte_filepath_buffer_size;
    VALUE path;

    filename_len = info->FileNameLength/sizeof(WCHAR);

    // The file in the 'info' struct is NOT null-terminated, so add 1 extra char to the allocation
    buffer = ALLOCA_N(WCHAR, filename_len + 1);

    memcpy(buffer, info->FileName, info->FileNameLength);

    // Null-terminate the string
    buffer[filename_len] = L'\0';

    WDM_WDEBUG("change in: '%s'", buffer);

    absolute_filepath_len = wcslen(base_dir) + filename_len;
    absolute_filepath = ALLOCA_N(WCHAR, absolute_filepath_len + 1); // 1 for NULL
    absolute_filepath[0] = L'\0';

    wcscat(absolute_filepath, base_dir);
    wcscat(absolute_filepath, buffer);

    WDM_WDEBUG("absolute path is: '%s'", absolute_filepath);

    _wsplitpath(buffer, NULL, NULL, file, ext);

    // TODO: Extracting the file name from 'buffer' is only needed when watching sub-dirs
    filename[0] = L'\0';
    if ( file[0] != L'\0' ) wcscat(filename, file);
    if ( ext[0]  != L'\0' ) wcscat(filename, ext);

    WDM_WDEBUG("filename: '%s'", filename);

    filename_len = wcslen(filename);

    // The maximum length of an 8.3 filename is twelve, including the dot.
    if (filename_len <= 12 && wcschr(filename, L'~'))
    {
        LPWSTR unicode_absolute_filepath;
        WCHAR absolute_long_filepath[WDM_MAX_WCHAR_LONG_PATH];
        BOOL is_unc_path;

        is_unc_path = wdm_utils_is_unc_path(absolute_filepath);

        unicode_absolute_filepath = ALLOCA_N(WCHAR, absolute_filepath_len + (is_unc_path ? 8 : 4) + 1); // 8 for "\\?\UNC\" or 4 for "\\?\", and 1 for \0

        unicode_absolute_filepath[0] = L'\0';
        wcscat(unicode_absolute_filepath, L"\\\\?\\");

        if ( is_unc_path ) {
            wcscat(unicode_absolute_filepath, L"UNC\\");
            wcscat(unicode_absolute_filepath, absolute_filepath + 2); // +2 to skip the begin of a UNC path
        }
        else {
            wcscat(unicode_absolute_filepath, absolute_filepath);
        }

        // Convert to the long filename form. Unfortunately, this
        // does not work for deletions, so it's an imperfect fix.
        if (GetLongPathNameW(unicode_absolute_filepath, absolute_long_filepath, WDM_MAX_WCHAR_LONG_PATH) != 0) {
            absolute_filepath = absolute_long_filepath + 4; // Skip first 4 pointers of "\\?\"
            absolute_filepath_len = wcslen(absolute_filepath);
            WDM_WDEBUG("Short path converted to long: '%s'", absolute_filepath);
        }
        else {
            WDM_DEBUG("Can't convert short path to long: '%s'", rb_w32_strerror(GetLastError()));
        }
    }

    // The convention in Ruby is to use forward-slashes to seprarate dirs on all platforms.
    wdm_utils_convert_back_to_forward_slashes(absolute_filepath, absolute_filepath_len + 1);

    // Convert the path from WCHAR to multibyte CHAR to use it in a ruby string
    multibyte_filepath_buffer_size =
        WideCharToMultiByte(CP_UTF8, 0, absolute_filepath, absolute_filepath_len + 1, NULL, 0, NULL, NULL);

    multibyte_filepath = ALLOCA_N(CHAR, multibyte_filepath_buffer_size);

    if ( 0 == WideCharToMultiByte(CP_UTF8, 0, absolute_filepath, absolute_filepath_len + 1,
            multibyte_filepath, multibyte_filepath_buffer_size, NULL, NULL) ) {
        rb_raise(eWDM_Error, "Failed to add the change file path to the event!");
    }

    WDM_DEBUG("will report change in: '%s'",  multibyte_filepath);

    path = rb_enc_str_new(multibyte_filepath,
        multibyte_filepath_buffer_size - 1, // -1 because this func takes the chars count, not bytes count
        wdm_rb_enc_utf8);

    OBJ_TAINT(path);

    return path;
}