Class: LargeFileLinux
- Inherits:
-
Object
- Object
- LargeFileLinux
- Defined in:
- lib/large_file_linux/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.size(fname) ⇒ Object
The “size” class method.
Instance Method Summary collapse
-
#close ⇒ Object
The “close” instance method.
-
#initialize(fname, rflags) ⇒ Object
constructor
The “initialize” method.
-
#read(bytes) ⇒ Object
The “read” instance method.
-
#seek(offset, whence) ⇒ Object
The “seek” instance method.
-
#size ⇒ Object
The “size” instance method.
-
#write(buf, bytes) ⇒ Object
The “write” instance method.
Constructor Details
#initialize(fname, rflags) ⇒ Object
The “initialize” method.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'ext/large_file_linux/large_file_linux.c', line 63
static VALUE
lf_init(VALUE self, VALUE fname, VALUE rflags) {
char *fnp, *rffp;
int fd, *fdp, flags;
/*
* Get the C-language representations of the Ruby
* objects passed in.
*/
fnp = RSTRING_PTR(StringValue(fname));
rffp = RSTRING_PTR(StringValue(rflags));
# ifdef DEBUG
printf("LargeFileLinux[0x%lx] (new): file = %s, flags = %s\n", self, fnp, rffp);
# endif
/*
* Convert Ruby form of "open" flags to open(2) bit flags.
*/
flags = O_LARGEFILE;
switch(*rffp) {
case 'r':
if (*++rffp == '+') flags |= O_RDWR; // Read/write: "r+"
else flags |= O_RDONLY; // Read-only: "r"
break;
case 'w':
flags |= O_CREAT | O_TRUNC;
if (*++rffp == '+') flags |= O_RDWR; // Read/write, trincate or create: "w+"
else flags |= O_WRONLY; // Write-only, truncate or create: "w"
break;
case 'a':
flags |= O_CREAT | O_APPEND;
if (*++rffp == '+') flags |= O_RDWR; // Read/write, append or create: "a+"
else flags |= O_WRONLY; // Write-only, append or create: "a"
break;
case 'b': // Binary mode, no-op on linux
break;
default:
rb_raise(rb_eArgError,
"%s::new on file: %s - unrecognized flag value: %c\n",
class_name, fnp, *rffp);
}
/*
* Open the file.
*/
if ((fd = open(fnp, flags)) < 0) {
rb_raise(rb_eSystemCallError,
"%s::new - open failed for file: %s, %s\n",
class_name, fnp, strerror(errno));
}
/*
* Save the file name and file descriptor in instance variables.
*/
rb_iv_set(self, "@fileName", rb_str_new2(fnp));
/*
* Set the file descriptor in private data space.
*/
Data_Get_Struct(self, int, fdp);
*fdp = fd;
# ifdef DEBUG
printf("LargeFileLinux[0x%lx] (init): fd = %d\n", self, *fdp);
# endif
return self;
}
|
Class Method Details
.size(fname) ⇒ Object
The “size” class method.
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 409 410 |
# File 'ext/large_file_linux/large_file_linux.c', line 383
static VALUE
lf_s_size(VALUE self, VALUE fname) {
char *fnp;
struct stat64 st;
/*
* Get the C-language representations of the Ruby
* objects passed in.
*/
fnp = RSTRING_PTR(StringValue(fname));
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (s_size): file = %s\n", self, *fnp);
# endif
/*
* stat the file to get its size.
*/
if (stat64(fnp, &st) < 0) {
rb_raise(rb_eSystemCallError,
"%s::s_size - stat failed on file: %s, %s\n",
class_name,
fnp,
strerror(errno));
}
return OFFT2NUM(st.st_size);
}
|
Instance Method Details
#close ⇒ Object
The “close” instance method.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'ext/large_file_linux/large_file_linux.c', line 415
static VALUE
lf_close(VALUE self) {
int *fdp;
/*
* Get the file descriptor from private data space.
*/
Data_Get_Struct(self, int, fdp);
# ifdef DEBUG
do {
VALUE v = rb_iv_get(self, "@fileName");
printf("LargeFileLinux [0x%lx] (close): fd = %d, File = %s\n", self, *fdp, RSTRING_PTR(StringValue(v)));
} while(0);
# endif
if (*fdp != CLOSED_FD) {
close(*fdp);
*fdp = CLOSED_FD;
}
return Qnil;
}
|
#read(bytes) ⇒ Object
The “read” instance method.
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 |
# File 'ext/large_file_linux/large_file_linux.c', line 140
static VALUE
lf_read(VALUE self, VALUE bytes) {
VALUE rb;
int *fdp;
char *buf;
long len;
int n = NUM2INT(bytes); // the number of bytes to read
/*
* Get the file descriptor from private data space.
*/
Data_Get_Struct(self, int, fdp);
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (read): bytes = %d, fd = %d\n", self, n, *fdp);
# endif
/*
* Make sure we don't try to read a file we've closed.
*/
if (*fdp == CLOSED_FD) {
VALUE v = rb_iv_get(self, "@fileName");
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (read): failed on file (fd = %d): %s, attempted read after close\n",
self,
*fdp,
RSTRING_PTR(StringValue(v)));
# endif
rb_raise(rb_eSystemCallError,
"%s::read - read failed on file: %s, attempted read after close\n",
class_name,
RSTRING_PTR(StringValue(v)));
}
/*
* Allocate a temp read buffer.
*/
if ((buf = malloc(n)) == NULL) {
VALUE v = rb_iv_get(self, "@fileName");
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (read): on file (fd = %d): %s - could not allocate memory for read buffer\n",
self,
*fdp,
RSTRING_PTR(StringValue(v)));
# endif
rb_raise(rb_eNoMemError,
"%s::read on file: %s - could not allocate memory for read buffer\n",
class_name,
RSTRING_PTR(StringValue(v)));
}
/*
* Read data into the temp buffer.
*/
if ((len = read(*fdp, buf, n)) < 0) {
VALUE v = rb_iv_get(self, "@fileName");
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (read): failed on file (fd = %d): %s, %s\n",
self,
*fdp,
RSTRING_PTR(StringValue(v)),
strerror(errno));
# endif
rb_raise(rb_eSystemCallError,
"%s::read - read failed on file: %s, %s\n",
class_name,
RSTRING_PTR(StringValue(v)),
strerror(errno));
}
/*
* Create a Ruby string and initialize it with the
* contents of the temp buffer.
*/
rb = rb_str_new(buf, len);
// we no longer need the temp buffer.
free(buf);
// return the ruby string.
return rb;
}
|
#seek(offset, whence) ⇒ Object
The “seek” instance method.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'ext/large_file_linux/large_file_linux.c', line 285
static VALUE
lf_seek(VALUE self, VALUE offset, VALUE whence) {
int *fdp;
long long o;
/*
* Get the file descriptor from private data space.
*/
Data_Get_Struct(self, int, fdp);
/*
* The offset value passed in is either a Fixnum or Bignum,
* convert accordingly.
*/
if (FIXNUM_P(offset)) {
o = (long long)NUM2INT(offset);
}
else {
o = rb_big2ll(offset);
}
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (seek): offset = %lld, whence = %d, fd = %d\n",
self, o, NUM2INT(whence), *fdp);
# endif
/*
* Make sure we don't try to seek on a file we've closed.
*/
if (*fdp == CLOSED_FD) {
VALUE v = rb_iv_get(self, "@fileName");
rb_raise(rb_eSystemCallError,
"%s::seek - seek failed on file: %s, attempted seek after close\n",
class_name,
RSTRING_PTR(StringValue(v)));
}
/*
* Perform seek operation on the file.
*
* This code assumes the values of the Ruby IO::SEEK_* constants
* and their corresponding C-library equivalents, are the same.
*/
if (lseek64(*fdp, o, NUM2INT(whence)) < 0) {
VALUE v = rb_iv_get(self, "@fileName");
rb_raise(rb_eSystemCallError,
"%s::seek - seek failed on file: %s, %s\n",
class_name,
RSTRING_PTR(StringValue(v)),
strerror(errno));
}
return INT2NUM(0);
}
|
#size ⇒ Object
The “size” instance method.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'ext/large_file_linux/large_file_linux.c', line 343
static VALUE
lf_size(VALUE self) {
struct stat64 st;
int *fdp;
/*
* Get the file descriptor from private data space.
*/
Data_Get_Struct(self, int, fdp);
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (size): fd = %d\n", self, *fdp);
# endif
if (*fdp == CLOSED_FD) {
VALUE v = rb_iv_get(self, "@fileName");
rb_raise(rb_eSystemCallError,
"%s::size - failed on file: %s, file is not open\n",
class_name,
RSTRING_PTR(StringValue(v)));
}
/*
* stat the file to get its size.
*/
if (fstat64(*fdp, &st) < 0) {
VALUE v = rb_iv_get(self, "@fileName");
rb_raise(rb_eSystemCallError,
"%s::size - stat failed on file: %s, %s\n",
class_name,
RSTRING_PTR(StringValue(v)),
strerror(errno));
}
return OFFT2NUM(st.st_size);
}
|
#write(buf, bytes) ⇒ Object
The “write” instance method.
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'ext/large_file_linux/large_file_linux.c', line 224
static VALUE
lf_write(VALUE self, VALUE buf, VALUE bytes) {
int *fdp;
char *bufp;
long len;
int n = NUM2INT(bytes); // the number of bytes to write
/*
* Get the file descriptor from private data space.
*/
Data_Get_Struct(self, int, fdp);
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (write): bytes = %d, fd = %d\n", self, n, *fdp);
# endif
/*
* Make sure we don't try to write a file we've closed.
*/
if (*fdp == CLOSED_FD) {
VALUE v = rb_iv_get(self, "@fileName");
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (write): write failed on file (fd = %d): %s, attempted read after close\n",
self,
*fdp,
RSTRING_PTR(StringValue(v)));
# endif
rb_raise(rb_eSystemCallError,
"%s::write - write failed on file: %s, attempted read after close\n",
class_name,
RSTRING_PTR(StringValue(v)));
}
bufp = RSTRING_PTR(StringValue(buf));
/*
* Write data to file.
*/
if ((len = write(*fdp, bufp, n)) < 0) {
VALUE v = rb_iv_get(self, "@fileName");
# ifdef DEBUG
printf("LargeFileLinux [0x%lx] (write): write failed on file (fd = %d): %s, %s\n",
self,
*fdp,
RSTRING_PTR(StringValue(v)),
strerror(errno));
# endif
rb_raise(rb_eSystemCallError,
"%s::write - write failed on file (fd = %d): %s, %s\n",
class_name,
*fdp,
RSTRING_PTR(StringValue(v)),
strerror(errno));
}
return INT2NUM(len);
}
|