Module: RubyWatchman

Defined in:
lib/ruby-watchman/version.rb,
ext/ruby-watchman/watchman.c

Overview

Copyright © 2014-present Facebook. All Rights Reserved.

Constant Summary collapse

VERSION =
'0.0.2'

Class Method Summary collapse

Class Method Details

.dump(serializable) ⇒ Object

RubyWatchman.dump(serializable)

Converts the Ruby object, ‘serializable`, into a binary string in the Watchman binary protocol format.

Examples of serializable objects include arrays, hashes, strings, numbers (integers, floats), booleans, and nil.



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'ext/ruby-watchman/watchman.c', line 548

VALUE RubyWatchman_dump(VALUE self, VALUE serializable) {
    watchman_t *w = watchman_init();
    watchman_dump(w, serializable);

    // update header with final length information
    uint64_t *len =
        (uint64_t *)(w->data + sizeof(WATCHMAN_HEADER) - sizeof(uint64_t) - 1);
    *len = w->len - sizeof(WATCHMAN_HEADER) + 1;

    // prepare final return value
    VALUE serialized = rb_str_buf_new(w->len);
    rb_str_buf_cat(serialized, (const char*)w->data, w->len);
    watchman_free(w);
    return serialized;
}

.load(serialized) ⇒ Object

RubyWatchman.load(serialized)

Converts the binary object, ‘serialized`, from the Watchman binary protocol format into a normal Ruby object.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'ext/ruby-watchman/watchman.c', line 492

VALUE RubyWatchman_load(VALUE self, VALUE serialized) {
    serialized = StringValue(serialized);
    long len = RSTRING_LEN(serialized);
    char *ptr = RSTRING_PTR(serialized);
    char *end = ptr + len;

    // expect at least the binary marker and a int8_t length counter
    if ((size_t)len < sizeof(WATCHMAN_BINARY_MARKER) - 1 + sizeof(int8_t) * 2) {
        rb_raise(rb_eArgError, "undersized header");
    }

    int mismatched =
        memcmp(ptr, WATCHMAN_BINARY_MARKER, sizeof(WATCHMAN_BINARY_MARKER) - 1);
    if (mismatched) {
        rb_raise(rb_eArgError, "missing binary marker");
    }

    // get size marker
    ptr += sizeof(WATCHMAN_BINARY_MARKER) - 1;
    uint64_t payload_size = watchman_load_int(&ptr, end);
    if (!payload_size) {
        rb_raise(rb_eArgError, "empty payload");
    }

    // sanity check length
    if (ptr + payload_size != end) {
        rb_raise(
            rb_eArgError,
            "payload size mismatch (%lu)",
            (unsigned long)(end - (ptr + payload_size))
        );
    }

    VALUE loaded = watchman_load(&ptr, end);

    // one more sanity check
    if (ptr != end) {
        rb_raise(
            rb_eArgError,
            "payload termination mismatch (%lu)",
            (unsigned long)(end - ptr)
        );
    }

    return loaded;
}

.query(query, socket) ⇒ Object

RubyWatchman.query(query, socket)

Converts ‘query`, a Watchman query comprising Ruby objects, into the Watchman binary protocol format, transmits it over socket, and unserializes and returns the result.



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'ext/ruby-watchman/watchman.c', line 581

VALUE RubyWatchman_query(VALUE self, VALUE query, VALUE socket) {
    VALUE error = Qnil;
    VALUE errorClass = Qnil;
    VALUE loaded = Qnil;
    void *buffer = NULL;
    int fileno = NUM2INT(rb_funcall(socket, rb_intern("fileno"), 0));

    // do blocking I/O to simplify the following logic
    int flags = fcntl(fileno, F_GETFL);
    if (
        !(flags & O_NONBLOCK) &&
        fcntl(fileno, F_SETFL, flags & ~O_NONBLOCK) == -1
    ) {
        error = rb_str_new2("unable to clear O_NONBLOCK flag");
        goto cleanup;
    }

    // send the message
    VALUE serialized = RubyWatchman_dump(self, query);
    long query_len = RSTRING_LEN(serialized);
    ssize_t sent = send(fileno, RSTRING_PTR(serialized), query_len, 0);
    if (sent == -1) {
        goto system_call_fail;
    } else if (sent != query_len) {
        error = rb_str_new2("sent byte count mismatch");
        goto cleanup;
    }

    // sniff to see how large the header is
    int8_t peek[WATCHMAN_PEEK_BUFFER_SIZE];
    ssize_t received =
        recv(fileno, peek, WATCHMAN_SNIFF_BUFFER_SIZE, MSG_PEEK | MSG_WAITALL);
    if (received == -1) {
        goto system_call_fail;
    } else if (received != WATCHMAN_SNIFF_BUFFER_SIZE) {
        error = rb_str_new2("failed to sniff PDU header");
        goto cleanup;
    }

    // peek at size of PDU
    int8_t sizes[] = { 0, 0, 0, 1, 2, 4, 8 };
    int8_t sizes_idx = peek[sizeof(WATCHMAN_BINARY_MARKER) - 1];
    if (sizes_idx < WATCHMAN_INT8_MARKER || sizes_idx > WATCHMAN_INT64_MARKER) {
        error = rb_str_new2("bad PDU size marker");
        goto cleanup;
    }
    ssize_t peek_size = sizeof(WATCHMAN_BINARY_MARKER) - 1 + sizeof(int8_t) +
        sizes[sizes_idx];

    received = recv(fileno, peek, peek_size, MSG_PEEK);
    if (received == -1) {
        goto system_call_fail;
    } else if (received != peek_size) {
        error = rb_str_new2("failed to peek at PDU header");
        goto cleanup;
    }
    int8_t *pdu_size_ptr =
        peek + sizeof(WATCHMAN_BINARY_MARKER) - sizeof(int8_t);
    int64_t payload_size =
        peek_size +
        watchman_load_int((char **)&pdu_size_ptr, (char *)peek + peek_size);

    // actually read the PDU
    buffer = xmalloc(payload_size);
    if (!buffer) {
        errorClass = rb_eNoMemError;
        error = rb_str_new2("failed to allocate");
        goto cleanup;
    }
    received = recv(fileno, buffer, payload_size, MSG_WAITALL);
    if (received == -1) {
        goto system_call_fail;
    } else if (received != payload_size) {
        error = rb_str_new2("failed to load PDU");
        goto cleanup;
    }

    if (!(flags & O_NONBLOCK) && fcntl(fileno, F_SETFL, flags) == -1) {
        error = rb_str_new2("unable to restore fnctl flags");
        goto cleanup;
    }

    char *payload = buffer + peek_size;
    loaded = watchman_load(&payload, payload + payload_size);
    goto cleanup;

system_call_fail:
    errorClass = rb_eSystemCallError;
    error = INT2FIX(errno);

cleanup:
    if (buffer) {
        xfree(buffer);
    }

    if (!(flags & O_NONBLOCK) && fcntl(fileno, F_SETFL, flags) == -1) {
        rb_raise(rb_eRuntimeError, "unable to restore fnctl flags");
    }

    if (NIL_P(errorClass)) {
        errorClass = rb_eRuntimeError;
    }

    if (!NIL_P(error)) {
        rb_exc_raise(rb_class_new_instance(1, &error, errorClass));
    }

    return loaded;
}