Hello, All!

I had PostgreSQL 7.0.3 (7.1 now) and one nice day I've noticed that much
number of my BLOBs are broken! Although they seems to be with good content
in file system (xinv[0-9]+ files) I was not able to get them via
lo_export... After spending some time trying to fix it, I decided to write
my own xinv2plainfile converter. I hope if someone has same troubles this
converter will help him.
Just compile it, put it in the dir with your xinv[0-9]+ files and run.
It will create new files with name eq to BLOB id from apropriate xinv.
----------------------------xinv2palinfile.c-------------------------------
#include "sys/types.h"
#include "dirent.h"
#include "stdio.h"
#include "string.h"
 
#define BLCKSIZE 8192
#define HPT_LEN 40
 
#define DEBUG
//#undef DEBUG
 
typedef unsigned short uint16;
typedef unsigned int uint32;
 
typedef struct ItemIdData
{
  unsigned lp_off:15,
    lp_flags:2,
    lp_len:15;
} ItemIdData;

typedef struct PageHeaderData
{
  uint16 pd_lower;
  uint16 pd_upper;
  uint16 pd_special;
  uint16 pd_opaque; //page size
  ItemIdData pd_linp[1];
} PageHeaderData;
 
int
extract(const char * filename)
{
  FILE * infile;
  FILE * outfile;
  ItemIdData linp;
  PageHeaderData* pg_head;
  char buff[BLCKSIZE];
  char data[BLCKSIZE];
  int tuple_no;

  //opening outpur file, if it is already presents, overwrite it!
  if ((outfile = fopen(filename + 4, "w")) == NULL)
    return -1;
 
  //opening input file
  if ((infile = fopen(filename, "r")) == NULL)
    return -1;
 
  while (fread(&buff, BLCKSIZE, 1, infile))
    {
      pg_head = (PageHeaderData*)buff;
#ifdef DEBUG
      printf("Page data: pd_lower=%d, pd_upper=%d, pd_special=%d, 
pd_opaque=%d\
             pg_head->pd_lower, pg_head->pd_upper, pg_head->pd_special,
pg_head
#endif

      for(tuple_no = 0; pg_head->pd_linp[tuple_no].lp_len; ++tuple_no)
        {
          linp = pg_head->pd_linp[tuple_no];
          memcpy(data, buff + linp.lp_off + HPT_LEN, linp.lp_len -
HPT_LEN);
          data[linp.lp_len - HPT_LEN] = 0;
          //    }
#ifdef DEBUG
          printf("Tuple %d: off=%d,\tflags=%d,\tlen=%d\n",\
                 tuple_no, linp.lp_off, linp.lp_flags, linp.lp_len);
          printf("Data:\n%s\n----------\n", data);
#endif
          fprintf(outfile, "%s", data);
        }
    }
  fclose(infile);
  fclose(outfile);
  return 0;
}

int
main(void)
{
  DIR * curdir;
  struct dirent * curdirentry;
 
  //open current directory
  curdir = opendir(".");
  if (curdir == NULL)
    {
      printf("Cannot open curdir!!!\n");
      return -1;
    }

  //search through curdir for files 'xinv[0-9]+'
  while ((curdirentry = readdir(curdir)) != NULL)
  {
    if (strstr(curdirentry->d_name, "xinv") != curdirentry->d_name)
      continue;
    //found entry with name begining with xinv.
    //let's hope this is what we are looking for :)
    printf("Trying to extract file '%s'... ", curdirentry->d_name);
    if (extract(curdirentry->d_name))
      printf("failed\n");
    else
      printf("successed\n");
  }
 
  return 0;
}

---------------------------------------------------------------------------
With Best Regards,
  Maks N. Polunin.
Brainbench: http://www.brainbench.com/transcript.jsp?pid=111472
ICQ#:18265775


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to