Load a string object from an RDB file according to flags: * * RDB_LOAD_NONE (no flags): load an RDB object, unencoded. * RDB_LOAD_ENC: If the returned type is a Redis object, try to * encode it in a special way to be more memory * efficient. When this flag is passed the function * no longer guarantees that obj->ptr is an SDS string. * RDB_LOAD_PLAIN
| 506 | * On I/O error NULL is returned. |
| 507 | */ |
| 508 | void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) { |
| 509 | int encode = flags & RDB_LOAD_ENC; |
| 510 | int plain = flags & RDB_LOAD_PLAIN; |
| 511 | int sds = flags & RDB_LOAD_SDS; |
| 512 | int isencoded; |
| 513 | unsigned long long len; |
| 514 | |
| 515 | len = rdbLoadLen(rdb,&isencoded); |
| 516 | if (len == RDB_LENERR) return NULL; |
| 517 | |
| 518 | if (isencoded) { |
| 519 | switch(len) { |
| 520 | case RDB_ENC_INT8: |
| 521 | case RDB_ENC_INT16: |
| 522 | case RDB_ENC_INT32: |
| 523 | return rdbLoadIntegerObject(rdb,len,flags,lenptr); |
| 524 | case RDB_ENC_LZF: |
| 525 | return rdbLoadLzfStringObject(rdb,flags,lenptr); |
| 526 | default: |
| 527 | rdbReportCorruptRDB("Unknown RDB string encoding type %llu",len); |
| 528 | return NULL; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (plain || sds) { |
| 533 | void *buf = plain ? ztrymalloc(len) : sdstrynewlen(SDS_NOINIT,len); |
| 534 | if (!buf) { |
| 535 | serverLog(server.loading? LL_WARNING: LL_VERBOSE, "rdbGenericLoadStringObject failed allocating %llu bytes", len); |
| 536 | return NULL; |
| 537 | } |
| 538 | if (lenptr) *lenptr = len; |
| 539 | if (len && rioRead(rdb,buf,len) == 0) { |
| 540 | if (plain) |
| 541 | zfree(buf); |
| 542 | else |
| 543 | sdsfree(buf); |
| 544 | return NULL; |
| 545 | } |
| 546 | return buf; |
| 547 | } else { |
| 548 | robj *o = encode ? tryCreateStringObject(SDS_NOINIT,len) : |
| 549 | tryCreateRawStringObject(SDS_NOINIT,len); |
| 550 | if (!o) { |
| 551 | serverLog(server.loading? LL_WARNING: LL_VERBOSE, "rdbGenericLoadStringObject failed allocating %llu bytes", len); |
| 552 | return NULL; |
| 553 | } |
| 554 | if (len && rioRead(rdb,o->ptr,len) == 0) { |
| 555 | decrRefCount(o); |
| 556 | return NULL; |
| 557 | } |
| 558 | return o; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | robj *rdbLoadStringObject(rio *rdb) { |
| 563 | return rdbGenericLoadStringObject(rdb,RDB_LOAD_NONE,NULL); |
no test coverage detected