Class: Pacct::Log

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, mode) ⇒ Object



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
# File 'ext/pacct/pacct_c.c', line 140

static VALUE pacct_log_init(VALUE self, VALUE filename, VALUE mode) {
  PacctLog* log;
  FILE* acct;
  long length;
  char* c_filename = StringValueCStr(filename);
  size_t c_filename_len;
  const char* c_mode = "rb";
  
  if(mode != Qnil) {
    int isValidMode = 0;
    size_t i;
    c_mode = StringValueCStr(mode);
    for(i = 0; i < sizeof(validFileModes) / sizeof(char*); ++i) {
      if(strcmp(c_mode, validFileModes[i]) == 0) {
        isValidMode = 1;
        break;
      }
    }
    if(!isValidMode) {
      rb_raise(rb_eArgError, "Invalid mode for Pacct::File: '%s'", c_mode);
    }
  }
  
  acct = fopen(c_filename, c_mode);
  if(!acct) {
    rb_raise(rb_eIOError, "Unable to open file '%s'", c_filename);
  }
  
  Data_Get_Struct(self, PacctLog, log);
  
  log->file = acct;
  c_filename_len = strlen(c_filename);
  log->filename = malloc(c_filename_len + 1);
  ENSURE_ALLOCATED(log->filename);
  strncpy(log->filename, c_filename, c_filename_len);
  log->filename[c_filename_len] = '\0';
  
  CHECK_CALL(fseek(acct, 0, SEEK_END), 0);
  length = ftell(acct);
  rewind(acct);
  
  if(length % sizeof(struct acct_v3) != 0) {
    rb_raise(rb_eIOError, "Accounting file '%s' appears to be the wrong size.", c_filename);
  }
  
  log->num_entries = length / sizeof(struct acct_v3);
  
  return self;
}

Class Method Details

.new(filename) ⇒ Object

Creates a new Pacct::Log using the given accounting file



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/pacct/pacct_c.c', line 123

static VALUE pacct_log_new(int argc, VALUE* argv, VALUE class) {
  VALUE log;
  VALUE init_args[2];
  PacctLog* ptr;
  
  init_args[1] = Qnil;
  rb_scan_args(argc, argv, "11", init_args, init_args + 1);
  
  log = Data_Make_Struct(class, PacctLog, 0, pacct_log_free, ptr);
  
  ptr->file = NULL;
  ptr->num_entries = 0;
  
  rb_obj_call_init(log, 2, init_args);
  return log;
}

Instance Method Details

#closeObject

Closes the log file



199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/pacct/pacct_c.c', line 199

static VALUE pacct_log_close(VALUE self) {
  PacctLog* log;
  
  Data_Get_Struct(self, PacctLog, log);
  
  if(log->file) {
    fclose(log->file);
    log->file = NULL;
  }
  
  return Qnil;
}

#each_entry([start]) {|entry, index| ... } ⇒ Object

Yields each entry in the file to the given block

If start is given, iteration starts at the entry with that index.

Yields:

  • (entry, index)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/pacct/pacct_c.c', line 242

static VALUE each_entry(int argc, VALUE* argv, VALUE self) {
  PacctLog* log;
  VALUE start_value;
  long start = 0;
  int i = 0;
  
  rb_scan_args(argc, argv, "01", &start_value);
  if(argc && start_value != Qnil) {
    start = NUM2UINT(start_value);
  }
  
  Data_Get_Struct(self, PacctLog, log);
  
  pacct_log_check_closed(log);
  
  if(start > log->num_entries) {
    rb_raise(rb_eRangeError, "Index %li is out of range", start);
  }
  
  CHECK_CALL(fseek(log->file, start * sizeof(struct acct_v3), SEEK_SET), 0);
  
  for(i = start; i < log->num_entries; ++i) {
    VALUE entry = pacct_entry_new(log);
    rb_yield(entry);
  }

  return Qnil;
}

#last_entryObject

Returns the last entry in the file



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/pacct/pacct_c.c', line 274

static VALUE last_entry(VALUE self) {
  PacctLog* log;
  long pos;
  VALUE entry;
  
  Data_Get_Struct(self, PacctLog, log);
  
  pacct_log_check_closed(log);
  
  if(log->num_entries == 0) {
    return Qnil;
  }
  
  pos = ftell(log->file);
  CHECK_CALL(fseek(log->file, -sizeof(struct acct_v3), SEEK_END), 0);
  
  entry = pacct_entry_new(log);
  
  CHECK_CALL(fseek(log->file, pos, SEEK_SET), 0);
  
  return entry;
}

#num_entriesObject

Returns the number of entries in the file



300
301
302
303
304
305
306
# File 'ext/pacct/pacct_c.c', line 300

static VALUE get_num_entries(VALUE self) {
  PacctLog* log;
  
  Data_Get_Struct(self, PacctLog, log);
  
  return INT2NUM(log->num_entries);
}

#write_entry(entry) ⇒ Object

Appends the given entry to the file



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'ext/pacct/pacct_c.c', line 314

static VALUE write_entry(VALUE self, VALUE entry) {
  //To do consider: verification?
  PacctLog* log;
  long pos;
  struct acct_v3* acct;
  
  Data_Get_Struct(self, PacctLog, log);
  pacct_log_check_closed(log);
  Data_Get_Struct(entry, struct acct_v3, acct);
  
  pos = ftell(log->file);
  CHECK_CALL(fseek(log->file, 0, SEEK_END), 0);
  
  if(fwrite(acct, sizeof(struct acct_v3), 1, log->file) != 1) {
    rb_raise(rb_eIOError, "Unable to write to accounting file '%s'", log->filename);
  }
  
  ++(log->num_entries);
  
  CHECK_CALL(fseek(log->file, pos, SEEK_SET), 0);
  
  return Qnil;
}