⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 load.c

📁 linux subdivision ying gai ke yi le ba
💻 C
📖 第 1 页 / 共 3 页
字号:
/* load.c --- parsing a 'dumpfile'-formatted stream.
 *
 * ====================================================================
 * Copyright (c) 2000-2004 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 * ====================================================================
 */


#include "svn_private_config.h"
#include "svn_pools.h"
#include "svn_error.h"
#include "svn_fs.h"
#include "svn_repos.h"
#include "svn_string.h"
#include "svn_hash.h"
#include "svn_path.h"

#include <apr_lib.h>



/*----------------------------------------------------------------------*/

/** Batons used herein **/

struct parse_baton
{
  svn_repos_t *repos;
  svn_fs_t *fs;

  svn_boolean_t use_history;
  svn_stream_t *outstream;
  enum svn_repos_load_uuid uuid_action;
  const char *parent_dir;
  apr_pool_t *pool;
  apr_hash_t *rev_map;
};

struct revision_baton
{
  svn_revnum_t rev;

  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;

  const svn_string_t *datestamp;

  apr_int32_t rev_offset;

  struct parse_baton *pb;
  apr_pool_t *pool;
};

struct node_baton
{
  const char *path;
  svn_node_kind_t kind;
  enum svn_node_action action;
  const char *md5_checksum;     /* null, if not available */

  svn_revnum_t copyfrom_rev;
  const char *copyfrom_path;

  struct revision_baton *rb;
  apr_pool_t *pool;
};



/*----------------------------------------------------------------------*/

/** A conversion function between the two vtable types. **/
static
svn_repos_parser_fns2_t *fns2_from_fns (const svn_repos_parser_fns_t *fns,
                                        apr_pool_t *pool)
{
  svn_repos_parser_fns2_t *fns2;

  fns2 = apr_palloc (pool, sizeof (*fns2));
  fns2->new_revision_record = fns->new_revision_record;
  fns2->uuid_record = fns->uuid_record;
  fns2->new_node_record = fns->new_node_record;
  fns2->set_revision_property = fns->set_revision_property;
  fns2->set_node_property = fns->set_node_property;
  fns2->remove_node_props = fns->remove_node_props;
  fns2->set_fulltext = fns->set_fulltext;
  fns2->close_node = fns->close_node;
  fns2->close_revision = fns->close_revision;
  fns2->delete_node_property = NULL;
  fns2->apply_textdelta = NULL;
  return fns2;
}


/*----------------------------------------------------------------------*/

/** The parser and related helper funcs **/


/* Allocate a new hash *HEADERS in POOL, and read a series of
   RFC822-style headers from STREAM.  Duplicate each header's name and
   value into POOL and store in hash as a const char * ==> const char *.

   The headers are assumed to be terminated by a single blank line,
   which will be permanently sucked from the stream and tossed.

   If the caller has already read in the first header line, it should
   be passed in as FIRST_HEADER.  If not, pass NULL instead.
 */
static svn_error_t *
read_header_block (svn_stream_t *stream,
                   svn_stringbuf_t *first_header,
                   apr_hash_t **headers,
                   apr_pool_t *pool)
{
  *headers = apr_hash_make (pool);  

  while (1)
    {
      svn_stringbuf_t *header_str;
      const char *name, *value; 
      svn_boolean_t eof;
      apr_size_t i = 0;

      if (first_header != NULL)
        {
          header_str = first_header;
          first_header = NULL;  /* so we never visit this block again. */
          eof = FALSE;
        }

      else
        /* Read the next line into a stringbuf. */
        SVN_ERR (svn_stream_readline (stream, &header_str, "\n", &eof, pool));
      
      if (eof || svn_stringbuf_isempty(header_str))
        break;    /* end of header block */

      /* Find the next colon in the stringbuf. */
      while (header_str->data[i] != ':')
        {
          if (header_str->data[i] == '\0')
            return svn_error_create (SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                                     "Found malformed header block "
                                     "in dumpfile stream");
          i++;
        }
      /* Create a 'name' string and point to it. */
      header_str->data[i] = '\0';
      name = header_str->data;

      /* Skip over the NULL byte and the space following it.  */
      i += 2;
      if (i > header_str->len)
        return svn_error_create (SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                                 "Found malformed header block "
                                 "in dumpfile stream");

      /* Point to the 'value' string. */
      value = header_str->data + i;
      
      /* Store name/value in hash. */
      apr_hash_set (*headers, name, APR_HASH_KEY_STRING, value);
    }

  return SVN_NO_ERROR;
}


static svn_error_t *
stream_ran_dry (void)
{
  return svn_error_create (SVN_ERR_INCOMPLETE_DATA, NULL,
                           "Premature end of content data in dumpstream");
}

static svn_error_t *
stream_malformed (void)
{
  return svn_error_create (SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                           "Dumpstream data appears to be malformed");
}

/* Read CONTENT_LENGTH bytes from STREAM, parsing the bytes as an
   encoded Subversion properties hash, and making multiple calls to
   PARSE_FNS->set_*_property on RECORD_BATON (depending on the value
   of IS_NODE.)

   Use POOL for all allocations.  */
static svn_error_t *
parse_property_block (svn_stream_t *stream,
                      svn_filesize_t content_length,
                      const svn_repos_parser_fns2_t *parse_fns,
                      void *record_baton,
                      svn_boolean_t is_node,
                      apr_pool_t *pool)
{
  svn_stringbuf_t *strbuf;

  while (content_length)
    {
      char *buf;  /* a pointer into the stringbuf's data */
      svn_boolean_t eof;

      /* Read a key length line.  (Actually, it might be PROPS_END). */
      SVN_ERR (svn_stream_readline (stream, &strbuf, "\n", &eof, pool));

      if (eof)
        {
          /* We could just use stream_ran_dry() or stream_malformed(),
             but better to give a non-generic property block error. */ 
          return svn_error_create
            (SVN_ERR_STREAM_MALFORMED_DATA, NULL,
             "Incomplete or unterminated property block");
        }

      content_length -= (strbuf->len + 1); /* +1 because we read a \n too. */
      buf = strbuf->data;

      if (! strcmp (buf, "PROPS-END"))
        break; /* no more properties. */

      else if ((buf[0] == 'K') && (buf[1] == ' '))
        {
          apr_size_t numread;
          char *keybuf;
          char c;
          
          /* Get the length of the key */
          apr_size_t keylen = (apr_size_t) atoi (buf + 2);

          /* Now read that much into a buffer, + 1 byte for null terminator */
          keybuf = apr_pcalloc (pool, keylen + 1);
          numread = keylen;
          SVN_ERR (svn_stream_read (stream, keybuf, &numread));
          content_length -= numread;
          if (numread != keylen)
            return stream_ran_dry ();
          keybuf[keylen] = '\0';

          /* Suck up extra newline after key data */
          numread = 1;
          SVN_ERR (svn_stream_read (stream, &c, &numread));
          content_length -= numread;
          if (numread != 1)
            return stream_ran_dry ();
          if (c != '\n') 
            return stream_malformed ();

          /* Read a val length line */
          SVN_ERR (svn_stream_readline (stream, &strbuf, "\n", &eof, pool));
          content_length -= (strbuf->len + 1); /* +1 because we read \n too */
          buf = strbuf->data;

          if ((buf[0] == 'V') && (buf[1] == ' '))
            {
              svn_string_t propstring;

              /* Get the length of the value */
              apr_size_t vallen = atoi (buf + 2);

              /* Again, 1 extra byte for the null termination. */
              char *valbuf = apr_palloc (pool, vallen + 1);
              numread = vallen;
              SVN_ERR (svn_stream_read (stream, valbuf, &numread));
              content_length -= numread;
              if (numread != vallen)
                return stream_ran_dry ();
              ((char *) valbuf)[vallen] = '\0';

              /* Suck up extra newline after val data */
              numread = 1;
              SVN_ERR (svn_stream_read (stream, &c, &numread));
              content_length -= numread;
              if (numread != 1)
                return stream_ran_dry ();
              if (c != '\n') 
                return stream_malformed ();

              /* Create final value string */
              propstring.data = valbuf;
              propstring.len = vallen;

              /* Now, send the property pair to the vtable! */
              if (is_node)
                SVN_ERR (parse_fns->set_node_property (record_baton,
                                                       keybuf,
                                                       &propstring));
              else
                SVN_ERR (parse_fns->set_revision_property (record_baton,
                                                           keybuf,
                                                           &propstring));
            }
          else
            return stream_malformed (); /* didn't find expected 'V' line */
        }
      else if ((buf[0] == 'D') && (buf[1] == ' '))
        {
          apr_size_t numread;
          char *keybuf;
          char c;
          
          /* Get the length of the key */
          apr_size_t keylen = (apr_size_t) atoi (buf + 2);

          /* Now read that much into a buffer, + 1 byte for null terminator */
          keybuf = apr_pcalloc (pool, keylen + 1);
          numread = keylen;
          SVN_ERR (svn_stream_read (stream, keybuf, &numread));
          content_length -= numread;
          if (numread != keylen)
            return stream_ran_dry ();
          keybuf[keylen] = '\0';

          /* Suck up extra newline after key data */
          numread = 1;
          SVN_ERR (svn_stream_read (stream, &c, &numread));
          content_length -= numread;
          if (numread != 1)
            return stream_ran_dry ();
          if (c != '\n') 
            return stream_malformed ();

          /* We don't expect these in revision properties, and if we see
             one when we don't have a delete_node_property callback,
             then we're seeing a v3 feature in a v2 dump. */
          if (!is_node || !parse_fns->delete_node_property)
            return stream_malformed ();

          SVN_ERR (parse_fns->delete_node_property (record_baton, keybuf));
        }
      else
        return stream_malformed (); /* didn't find expected 'K' line */
      
    } /* while (1) */

  return SVN_NO_ERROR;
}                  


/* Read CONTENT_LENGTH bytes from STREAM, and use
   PARSE_FNS->set_fulltext to push those bytes as replace fulltext for
   a node.  Use BUFFER/BUFLEN to push the fulltext in "chunks".

   Use POOL for all allocations.  */
static svn_error_t *
parse_text_block (svn_stream_t *stream,
                  svn_filesize_t content_length,
                  svn_boolean_t is_delta,
                  const svn_repos_parser_fns2_t *parse_fns,
                  void *record_baton,
                  char *buffer,
                  apr_size_t buflen,
                  apr_pool_t *pool)
{
  svn_stream_t *text_stream = NULL;
  apr_size_t num_to_read, rlen, wlen;

  if (is_delta)
    {
      svn_txdelta_window_handler_t wh;
      void *whb;

      SVN_ERR (parse_fns->apply_textdelta (&wh, &whb, record_baton));
      if (wh)
        text_stream = svn_txdelta_parse_svndiff (wh, whb, TRUE, pool);
    }
  else
    {
      /* Get a stream to which we can push the data. */
      SVN_ERR (parse_fns->set_fulltext (&text_stream, record_baton));
    }

  /* If there are no contents to read, just write an empty buffer
     through our callback. */
  if (content_length == 0)
    {
      wlen = 0;
      if (text_stream)
        SVN_ERR (svn_stream_write (text_stream, "", &wlen));
    }

  /* Regardless of whether or not we have a sink for our data, we
     need to read it. */
  while (content_length)
    {
      if (content_length >= buflen)
        rlen = buflen;
      else
        rlen = (apr_size_t) content_length;
      
      num_to_read = rlen;
      SVN_ERR (svn_stream_read (stream, buffer, &rlen));
      content_length -= rlen;
      if (rlen != num_to_read)
        return stream_ran_dry ();
      
      if (text_stream)
        {
          /* write however many bytes you read. */
          wlen = rlen;
          SVN_ERR (svn_stream_write (text_stream, buffer, &wlen));
          if (wlen != rlen)
            {
              /* Uh oh, didn't write as many bytes as we read. */
              return svn_error_create (SVN_ERR_STREAM_UNEXPECTED_EOF, NULL,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -