Class: Warts::File

Inherits:
Object
  • Object
show all
Defined in:
ext/scfile.c

Constant Summary collapse

TRACEROUTE =
rb_str_new2("traceroute")
ARTS =
rb_str_new2("arts")
WARTS =
rb_str_new2("warts")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

  • Opens either a path or a fd, depending on the type of the first argument.

  • A file descriptor can be given as either a FIXNUM or as an instance of

  • IO.

*

  • NOTE: It is the responsibility of the caller to (eventually) close any

  • passed in file descriptor. See comments below for the reason.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ext/scfile.c', line 120

static VALUE scfile_init(int argc, VALUE *argv, VALUE self)
{
  VALUE path_or_fd, mode, type;
  VALUE path = Qnil;
  int fd = -1, fd_dup = -1;
  char mode_char = 'r';
  char *type_str = NULL;
  scamper_file_t *file = NULL;
  file_data_t *data = NULL;

  rb_scan_args(argc, argv, "12", &path_or_fd, &mode, &type);
  if (NIL_P(path_or_fd)) {
    rb_raise(rb_eArgError, "missing path or fd");
  }

  if (TYPE(path_or_fd) == T_FIXNUM || TYPE(path_or_fd) == T_BIGNUM) {
    fd = NUM2INT(path_or_fd);
  }
  else if (RTEST(rb_funcall(path_or_fd, meth_kind_of, 1, rb_cIO))) {
    VALUE fileno = rb_funcall(path_or_fd, meth_fileno, 0);
    fd = FIX2INT(fileno);
  }
  else {
    path = path_or_fd;
    SafeStringValue(path);
  }

  if (NIL_P(mode)) {
    mode_char = 'r';
  }
  else {
    /* Since scamper_file_open() doesn't return any distinguishable error for
       an invalid mode, we check for validity here. */
    const char *mode_str;

    StringValue(mode);
    mode_str = RSTRING(mode)->ptr;
    if (strlen(mode_str) != 1 || !index("rwa", mode_str[0])) {
      rb_raise(rb_eArgError, "invalid open mode");
    }
    mode_char = mode_str[0];
  }

  rb_ivar_set(self, iv_readable, (mode_char == 'r' ? Qtrue : Qfalse));

  if (NIL_P(type)) {
    if (fd >= 0) {
      type_str = "warts";
    }
    else {
      if (mode_char == 'w' || mode_char == 'a') {
	type_str = "warts";
      }
      /* else let scamper_file_open do file type detection */
    }
  }
  else {
    StringValue(type);
    type_str = RSTRING(type)->ptr;

    /* Since scamper_file_open() doesn't return any distinguishable error for
       an invalid type, we check for validity here. */
    if (strcmp(type_str, "warts") && strcmp(type_str, "arts")
	&& strcmp(type_str, "traceroute")) {
      rb_raise(rb_eArgError, "invalid file type");
    }
  }

  if (type_str) {
    if ((mode_char == 'w' || mode_char == 'a') && strcmp(type_str, "arts")==0){
      rb_raise(rb_eArgError,"writing or appending to arts files not supported");
    }

    if (mode_char == 'r' && strcmp(type_str, "traceroute") == 0) {
      rb_raise(rb_eArgError, "reading from traceroute files not supported");
    }
  }

  data = ALLOC(file_data_t);
  data->file = NULL;
  data->filters = NULL;
  DATA_PTR(self) = data;

  /*
  ** NOTE: We must be careful when calling scamper_file_openfd().  We
  **       should NOT simply pass in the file descriptor supplied to
  **       scfile_init because the policy of scamper_file_openfd is to
  **       take ownership of the passed-in file descriptor.  This means
  **       that, eventually, scamper_file_close closes the file
  **       descriptor.  Normally, this "takes-ownership-of-argument"
  **       policy is what you want, but when interfacing the scamper warts
  **       library to Ruby, this policy can cause problems.
  **
  **       An open file in Ruby is more than simply a file
  **       descriptor--there's an allocated object associated with the
  **       file descriptor.  Hence, we need to free the object in addition
  **       to closing the file descriptor when closing an open file in
  **       Ruby.  So it causes problems when scamper_file_close does half
  **       of this work (by closing the file descriptor) without getting
  **       the Ruby object involved.  This results in close() being called
  **       twice on the file descriptor.  The first time, the call closes
  **       the actual file; the second time, it closes any unreleated open
  **       file that happens to be re-using the same file descriptor
  **       number (which can be particularly insidious if the extra
  **       close() is delayed till the next garbage collection).
  **
  **       When piping in the output of 'gzip -dc' (in order to
  **       transparently read a compressed warts file), simply closing the
  **       file descriptor isn't sufficient to free up the resource.  At
  **       some point after closing the file descriptor, we have to call
  **       waitpid().  Thus, in this case, the takes-ownership policy of
  **       scamper_file_openfd is problematic--the Ruby object needs to
  **       do the resource deallocation itself.
  **
  **       Fortunately, there's an easy solution to the mismatch.  We
  **       simply dup the file descriptor and pass the dup to
  **       scamper_file_openfd (and let it take ownership and do what it
  **       will of it).
  */
  if (NIL_P(path)) {
    if ((fd_dup = dup(fd)) < 0) {
      rb_raise(rb_eRuntimeError, "couldn't duplicate file descriptor");
    }
    file = scamper_file_openfd(fd_dup, NULL, mode_char, type_str);
  }
  else {
    file = scamper_file_open(RSTRING(path)->ptr, mode_char, type_str);
  }

  if (file) {
    data->file = file;
    return self;
  }
  else {
    if (fd_dup >= 0) {
      close(fd_dup);
    }
    return Qnil;
  }
}

Class Method Details

.open(*args) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
# File 'ext/scfile.c', line 312

static VALUE scfile_s_open(int argc, VALUE *argv, VALUE klass)
{
  VALUE obj = Data_Wrap_Struct(klass, 0, sf_free, 0);

  if (NIL_P(scfile_init(argc, argv, obj))) {
    return Qnil;
  }

  return (rb_block_given_p() ? rb_ensure(rb_yield, obj, scfile_close, obj)
	  : obj);
}

Instance Method Details

#add_all_filtersObject



578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'ext/scfile.c', line 578

static VALUE scfile_add_all_filters(VALUE self)
{
  VALUE filters;
  int i;

  filters = get_iv_filters(self);

  for (i = 0; i < num_sctypes; i++) {
    rb_ary_store(filters, all_sctypes[i].value, Qtrue);
  }

  rb_ivar_set(self, iv_filters_changed, Qtrue);
  return self;
}

#add_filters(*args) ⇒ Object



594
595
596
597
# File 'ext/scfile.c', line 594

static VALUE scfile_add_filters(int argc, VALUE *argv, VALUE self)
{
  return scfile_set_filters(argc, argv, self, Qtrue);
}

#clear_all_filtersObject



600
601
602
603
604
605
# File 'ext/scfile.c', line 600

static VALUE scfile_clear_all_filters(VALUE self)
{
  rb_ivar_set(self, iv_filters, Qnil);
  rb_ivar_set(self, iv_filters_changed, Qtrue);
  return self;
}

#clear_filters(*args) ⇒ Object



608
609
610
611
# File 'ext/scfile.c', line 608

static VALUE scfile_clear_filters(int argc, VALUE *argv, VALUE self)
{
  return scfile_set_filters(argc, argv, self, Qnil);
}

#closeObject



287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'ext/scfile.c', line 287

static VALUE scfile_close(VALUE self)
{
  file_data_t *data = NULL;

  Data_Get_Struct(self, file_data_t, data);

  if (data->file) {
    scamper_file_close(data->file);
    data->file = NULL;
  }

  return self;
}

#closed?Boolean

Returns:

  • (Boolean)


302
303
304
305
306
307
308
309
# File 'ext/scfile.c', line 302

static VALUE scfile_closed(VALUE self)
{
  file_data_t *data = NULL;

  Data_Get_Struct(self, file_data_t, data);

  return (data->file ? Qfalse : Qtrue);
}

#filtersObject



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'ext/scfile.c', line 614

static VALUE scfile_get_filters(VALUE self)
{
  VALUE retval, filters;
  int i;

  retval = rb_ary_new();

  filters = rb_ivar_get(self, iv_filters);
  if (!NIL_P(filters)) {
    for (i = 0; i < RARRAY(filters)->len; i++) {
      if (RTEST(rb_ary_entry(filters, i))) {
	rb_ary_push(retval, INT2FIX(i));
      }
    }
  }
  return retval;
}

#pathObject



275
276
277
278
279
280
281
282
283
284
# File 'ext/scfile.c', line 275

static VALUE scfile_path(VALUE self)
{
  file_data_t *data = NULL;

  Data_Get_Struct(self, file_data_t, data);
  if (data->file) {
    return rb_str_new2(scamper_file_getfilename(data->file));
  }
  return Qnil;
}

#readObject Also known as: each



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'ext/scfile.c', line 365

static VALUE scfile_read(VALUE self)
{
  file_data_t *data = NULL;
  uint16_t obj_type = 0;
  void *obj_data = NULL;
  VALUE obj;
  int result;

  Data_Get_Struct(self, file_data_t, data);
  if (!data->file) {
    return Qnil;
  }
  CHECK_READABLE(self)

  update_filters(self);

 loop:
  result = scamper_file_read(data->file, data->filters, &obj_type, &obj_data);
  if (result == -1) {
    rb_raise(rb_eIOError, "couldn't read warts file data");
  }
  if (!obj_data) {
    return Qnil;  /* EOF */
  }

  switch (obj_type) {
  case SCAMPER_FILE_OBJ_LIST:
    RETURN(sclist_create((scamper_list_t *)obj_data));

  case SCAMPER_FILE_OBJ_CYCLE_START:
  case SCAMPER_FILE_OBJ_CYCLE_DEF:
  case SCAMPER_FILE_OBJ_CYCLE_STOP:
    RETURN(sccycle_create((scamper_cycle_t *)obj_data, obj_type));

  case SCAMPER_FILE_OBJ_ADDR:
    RETURN(scaddr_create((scamper_addr_t *)obj_data));

  case SCAMPER_FILE_OBJ_TRACE:
    RETURN(sctrace_create((scamper_trace_t *)obj_data));

  default:
    rb_notimplement();
  }
}

#typeObject



262
263
264
265
266
267
268
269
270
271
272
# File 'ext/scfile.c', line 262

static VALUE scfile_type(VALUE self)
{
  file_data_t *data = NULL;

  Data_Get_Struct(self, file_data_t, data);
  if (data->file) {
    char buf [128];
    return rb_str_new2(scamper_file_type_tostr(data->file, buf, 128));
  }
  return Qnil;
}

#write(obj) ⇒ Object

  • This method initiates multiple dispatch with obj. Obj.write_to(file)

  • should turn around and invoke a suitable write_xxx method; e.g., a trace

  • object should call file.write_trace(self), and a ping object should call

  • file.write_ping(self).



419
420
421
422
# File 'ext/scfile.c', line 419

static VALUE scfile_write(VALUE self, VALUE obj)
{
  rb_funcall(obj, meth_write_to, 1, self);
}

#write_cycle_start(obj) ⇒ Object

  • NOTE: We could have gone with a single scfile_write_cycle() function

  • that determines what scamper function to call based on the type

  • of the cycle object. However, such a design might prove

  • inconvenient for the user since cycle objects are immutable

  • in this extension. If there were only a single function, then

  • users would have to be careful to hang onto the START/STOP

  • cycle objects when sorting traces into files.



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'ext/scfile.c', line 485

static VALUE scfile_write_cycle_start(VALUE self, VALUE obj)
{
  file_data_t *data = NULL;
  scamper_cycle_t *cycle = NULL;
  int result;

  if (TYPE(obj) != T_DATA || !RTEST(rb_obj_is_instance_of(obj, cCycle))) {
    rb_raise(rb_eArgError, "argument must be instance of Warts::Cycle");
  }

  Data_Get_Struct(self, file_data_t, data);
  if (!data->file) {
    return Qnil;
  }
  CHECK_WRITABLE(self)

  Data_Get_Struct(obj, scamper_cycle_t, cycle);
  warts_list_write_bug_workaround(cycle->list);

  result = scamper_file_write_cycle_start(data->file, cycle);
  if (result == -1) {
    rb_raise(rb_eIOError, "couldn't write warts file data");
  }
  return self;
}

#write_cycle_stop(obj) ⇒ Object



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
# File 'ext/scfile.c', line 512

static VALUE scfile_write_cycle_stop(VALUE self, VALUE obj)
{
  file_data_t *data = NULL;
  scamper_cycle_t *cycle = NULL;
  int result;

  if (TYPE(obj) != T_DATA || !RTEST(rb_obj_is_instance_of(obj, cCycle))) {
    rb_raise(rb_eArgError, "argument must be instance of Warts::Cycle");
  }

  Data_Get_Struct(self, file_data_t, data);
  if (!data->file) {
    return Qnil;
  }
  CHECK_WRITABLE(self)

  Data_Get_Struct(obj, scamper_cycle_t, cycle);
  warts_list_write_bug_workaround(cycle->list);

  result = scamper_file_write_cycle_stop(data->file, cycle);
  if (result == -1) {
    rb_raise(rb_eIOError, "couldn't write warts file data");
  }
  return self;
}

#write_trace(obj) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'ext/scfile.c', line 446

static VALUE scfile_write_trace(VALUE self, VALUE obj)
{
  file_data_t *data = NULL;
  scamper_trace_t *trace = NULL;
  int result;

  if (TYPE(obj) != T_DATA || !RTEST(rb_obj_is_instance_of(obj, cTrace))) {
    rb_raise(rb_eArgError, "argument must be instance of Warts::Trace");
  }

  Data_Get_Struct(self, file_data_t, data);
  if (!data->file) {
    return Qnil;
  }
  CHECK_WRITABLE(self)

  Data_Get_Struct(obj, scamper_trace_t, trace);
  warts_list_write_bug_workaround(trace->list);
  if (trace->cycle) {
    warts_list_write_bug_workaround(trace->cycle->list);
  }

  result = scamper_file_write_trace(data->file, trace);
  if (result == -1) {
    rb_raise(rb_eIOError, "couldn't write warts file data");
  }
  return self;
}