Ejemplo n.º 1
0
/* Check that reading a line from HUNK equals what's inside EXPECTED.
 * If ORIGINAL is TRUE, read the original hunk text; else, read the
 * modified hunk text. */
static svn_error_t *
check_content(svn_diff_hunk_t *hunk, svn_boolean_t original,
              const char *expected, apr_pool_t *pool)
{
  svn_stream_t *exp;
  svn_stringbuf_t *exp_buf;
  svn_stringbuf_t *hunk_buf;
  svn_boolean_t exp_eof;
  svn_boolean_t hunk_eof;

  exp = svn_stream_from_string(svn_string_create(expected, pool),
                               pool);

  while (TRUE)
  {
    SVN_ERR(svn_stream_readline(exp, &exp_buf, NL, &exp_eof, pool));
    if (original)
      SVN_ERR(svn_diff_hunk_readline_original_text(hunk, &hunk_buf, NULL,
                                                   &hunk_eof, pool, pool));
    else
      SVN_ERR(svn_diff_hunk_readline_modified_text(hunk, &hunk_buf, NULL,
                                                   &hunk_eof, pool, pool));

    SVN_TEST_ASSERT(exp_eof == hunk_eof);
    if (exp_eof)
      break;
    SVN_TEST_STRING_ASSERT(exp_buf->data, hunk_buf->data);
  }

  if (!hunk_eof)
    SVN_TEST_ASSERT(hunk_buf->len == 0);

  return SVN_NO_ERROR;
}
Ejemplo n.º 2
0
/* Given a revision file FILE that has been pre-positioned at the
   beginning of a Node-Rev header block, read in that header block and
   store it in the apr_hash_t HEADERS.  All allocations will be from
   RESULT_POOL. */
static svn_error_t *
read_header_block(apr_hash_t **headers,
                  svn_stream_t *stream,
                  apr_pool_t *result_pool)
{
  *headers = svn_hash__make(result_pool);

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

      SVN_ERR(svn_stream_readline(stream, &header_str, "\n", &eof,
                                  result_pool));

      if (eof || header_str->len == 0)
        break; /* end of header block */

      while (header_str->data[i] != ':')
        {
          if (header_str->data[i] == '\0')
            return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
                                     _("Found malformed header '%s' in "
                                       "revision file"),
                                     header_str->data);
          i++;
        }

      /* Create a 'name' string and point to it. */
      header_str->data[i] = '\0';
      name = header_str->data;
      name_len = i;

      /* Check if we have enough data to parse. */
      if (i + 2 > header_str->len)
        {
          /* Restore the original line for the error. */
          header_str->data[i] = ':';
          return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
                                   _("Found malformed header '%s' in "
                                     "revision file"),
                                   header_str->data);
        }

      /* Skip over the NULL byte and the space following it. */
      i += 2;

      value = header_str->data + i;

      /* header_str is safely in our pool, so we can use bits of it as
         key and value. */
      apr_hash_set(*headers, name, name_len, value);
    }

  return SVN_NO_ERROR;
}
Ejemplo n.º 3
0
svn_error_t *
svn_fs_fs__read_rep_header(svn_fs_fs__rep_header_t **header,
                           svn_stream_t *stream,
                           apr_pool_t *result_pool,
                           apr_pool_t *scratch_pool)
{
  svn_stringbuf_t *buffer;
  char *str, *last_str;
  apr_int64_t val;
  svn_boolean_t eol = FALSE;

  SVN_ERR(svn_stream_readline(stream, &buffer, "\n", &eol, scratch_pool));

  *header = apr_pcalloc(result_pool, sizeof(**header));
  (*header)->header_size = buffer->len + 1;
  if (strcmp(buffer->data, REP_PLAIN) == 0)
    {
      (*header)->type = svn_fs_fs__rep_plain;
      return SVN_NO_ERROR;
    }

  if (strcmp(buffer->data, REP_DELTA) == 0)
    {
      /* This is a delta against the empty stream. */
      (*header)->type = svn_fs_fs__rep_self_delta;
      return SVN_NO_ERROR;
    }

  (*header)->type = svn_fs_fs__rep_delta;

  /* We have hopefully a DELTA vs. a non-empty base revision. */
  last_str = buffer->data;
  str = svn_cstring_tokenize(" ", &last_str);
  if (! str || (strcmp(str, REP_DELTA) != 0))
    goto error;

  SVN_ERR(parse_revnum(&(*header)->base_revision, (const char **)&last_str));

  str = svn_cstring_tokenize(" ", &last_str);
  if (! str)
    goto error;
  SVN_ERR(svn_cstring_atoi64(&val, str));
  (*header)->base_item_index = (apr_off_t)val;

  str = svn_cstring_tokenize(" ", &last_str);
  if (! str)
    goto error;
  SVN_ERR(svn_cstring_atoi64(&val, str));
  (*header)->base_length = (svn_filesize_t)val;

  return SVN_NO_ERROR;

 error:
  return svn_error_createf(SVN_ERR_FS_CORRUPT, NULL,
                           _("Malformed representation header"));
}
Ejemplo n.º 4
0
/* 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 (svn_stringbuf_isempty(header_str))
            break;    /* end of header block */
        else if (eof)
            return stream_ran_dry();

        /* Find the next colon in the stringbuf. */
        while (header_str->data[i] != ':')
        {
            if (header_str->data[i] == '\0')
                return svn_error_createf(SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                                         _("Dump stream contains a malformed "
                                           "header (with no ':') at '%.20s'"),
                                         header_str->data);
            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_createf(SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                                     _("Dump stream contains a malformed "
                                       "header (with no value) at '%.20s'"),
                                     header_str->data);

        /* Point to the 'value' string. */
        value = header_str->data + i;

        /* Store name/value in hash. */
        svn_hash_sets(*headers, name, value);
    }

    return SVN_NO_ERROR;
}
Ejemplo n.º 5
0
svn_error_t *
svn_repos_parse_dumpstream3(svn_stream_t *stream,
                            const svn_repos_parse_fns3_t *parse_fns,
                            void *parse_baton,
                            svn_boolean_t deltas_are_text,
                            svn_cancel_func_t cancel_func,
                            void *cancel_baton,
                            apr_pool_t *pool)
{
    svn_boolean_t eof;
    svn_stringbuf_t *linebuf;
    void *rev_baton = NULL;
    char *buffer = apr_palloc(pool, SVN__STREAM_CHUNK_SIZE);
    apr_size_t buflen = SVN__STREAM_CHUNK_SIZE;
    apr_pool_t *linepool = svn_pool_create(pool);
    apr_pool_t *revpool = svn_pool_create(pool);
    apr_pool_t *nodepool = svn_pool_create(pool);
    int version;

    SVN_ERR(svn_stream_readline(stream, &linebuf, "\n", &eof, linepool));
    if (eof)
        return stream_ran_dry();

    /* The first two lines of the stream are the dumpfile-format version
       number, and a blank line.  To preserve backward compatibility,
       don't assume the existence of newer parser-vtable functions. */
    SVN_ERR(parse_format_version(&version, linebuf->data));
    if (parse_fns->magic_header_record != NULL)
        SVN_ERR(parse_fns->magic_header_record(version, parse_baton, pool));

    /* A dumpfile "record" is defined to be a header-block of
       rfc822-style headers, possibly followed by a content-block.

         - A header-block is always terminated by a single blank line (\n\n)

         - We know whether the record has a content-block by looking for
           a 'Content-length:' header.  The content-block will always be
           of a specific length, plus an extra newline.

       Once a record is fully sucked from the stream, an indeterminate
       number of blank lines (or lines that begin with whitespace) may
       follow before the next record (or the end of the stream.)
    */

    while (1)
    {
        apr_hash_t *headers;
        void *node_baton;
        svn_boolean_t found_node = FALSE;
        svn_boolean_t old_v1_with_cl = FALSE;
        const char *content_length;
        const char *prop_cl;
        const char *text_cl;
        const char *value;
        svn_filesize_t actual_prop_length;

        /* Clear our per-line pool. */
        svn_pool_clear(linepool);

        /* Check for cancellation. */
        if (cancel_func)
            SVN_ERR(cancel_func(cancel_baton));

        /* Keep reading blank lines until we discover a new record, or until
           the stream runs out. */
        SVN_ERR(svn_stream_readline(stream, &linebuf, "\n", &eof, linepool));

        if (eof)
        {
            if (svn_stringbuf_isempty(linebuf))
                break;   /* end of stream, go home. */
            else
                return stream_ran_dry();
        }

        if ((linebuf->len == 0) || (svn_ctype_isspace(linebuf->data[0])))
            continue; /* empty line ... loop */

        /*** Found the beginning of a new record. ***/

        /* The last line we read better be a header of some sort.
           Read the whole header-block into a hash. */
        SVN_ERR(read_header_block(stream, linebuf, &headers, linepool));

        /*** Handle the various header blocks. ***/

        /* Is this a revision record? */
        if (svn_hash_gets(headers, SVN_REPOS_DUMPFILE_REVISION_NUMBER))
        {
            /* If we already have a rev_baton open, we need to close it
               and clear the per-revision subpool. */
            if (rev_baton != NULL)
            {
                SVN_ERR(parse_fns->close_revision(rev_baton));
                svn_pool_clear(revpool);
            }

            SVN_ERR(parse_fns->new_revision_record(&rev_baton,
                                                   headers, parse_baton,
                                                   revpool));
        }
        /* Or is this, perhaps, a node record? */
        else if (svn_hash_gets(headers, SVN_REPOS_DUMPFILE_NODE_PATH))
        {
            SVN_ERR(parse_fns->new_node_record(&node_baton,
                                               headers,
                                               rev_baton,
                                               nodepool));
            found_node = TRUE;
        }
        /* Or is this the repos UUID? */
        else if ((value = svn_hash_gets(headers, SVN_REPOS_DUMPFILE_UUID)))
        {
            SVN_ERR(parse_fns->uuid_record(value, parse_baton, pool));
        }
        /* Or perhaps a dumpfile format? */
        /* ### TODO: use parse_format_version */
        else if ((value = svn_hash_gets(headers,
                                        SVN_REPOS_DUMPFILE_MAGIC_HEADER)))
        {
            /* ### someday, switch modes of operation here. */
            SVN_ERR(svn_cstring_atoi(&version, value));
        }
        /* Or is this bogosity?! */
        else
        {
            /* What the heck is this record?!? */
            return svn_error_create(SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                                    _("Unrecognized record type in stream"));
        }

        /* Need 3 values below to determine v1 dump type

           Old (pre 0.14?) v1 dumps don't have Prop-content-length
           and Text-content-length fields, but always have a properties
           block in a block with Content-Length > 0 */

        content_length = svn_hash_gets(headers,
                                       SVN_REPOS_DUMPFILE_CONTENT_LENGTH);
        prop_cl = svn_hash_gets(headers, SVN_REPOS_DUMPFILE_PROP_CONTENT_LENGTH);
        text_cl = svn_hash_gets(headers, SVN_REPOS_DUMPFILE_TEXT_CONTENT_LENGTH);
        old_v1_with_cl =
            version == 1 && content_length && ! prop_cl && ! text_cl;

        /* Is there a props content-block to parse? */
        if (prop_cl || old_v1_with_cl)
        {
            const char *delta = svn_hash_gets(headers,
                                              SVN_REPOS_DUMPFILE_PROP_DELTA);
            svn_boolean_t is_delta = (delta && strcmp(delta, "true") == 0);

            /* First, remove all node properties, unless this is a delta
               property block. */
            if (found_node && !is_delta)
                SVN_ERR(parse_fns->remove_node_props(node_baton));

            SVN_ERR(parse_property_block
                    (stream,
                     svn__atoui64(prop_cl ? prop_cl : content_length),
                     parse_fns,
                     found_node ? node_baton : rev_baton,
                     parse_baton,
                     found_node,
                     &actual_prop_length,
                     found_node ? nodepool : revpool));
        }

        /* Is there a text content-block to parse? */
        if (text_cl)
        {
            const char *delta = svn_hash_gets(headers,
                                              SVN_REPOS_DUMPFILE_TEXT_DELTA);
            svn_boolean_t is_delta = FALSE;
            if (! deltas_are_text)
                is_delta = (delta && strcmp(delta, "true") == 0);

            SVN_ERR(parse_text_block(stream,
                                     svn__atoui64(text_cl),
                                     is_delta,
                                     parse_fns,
                                     found_node ? node_baton : rev_baton,
                                     buffer,
                                     buflen,
                                     found_node ? nodepool : revpool));
        }
        else if (old_v1_with_cl)
        {
            /* An old-v1 block with a Content-length might have a text block.
               If the property block did not consume all the bytes of the
               Content-length, then it clearly does have a text block.
               If not, then we must deduce whether we have an *empty* text
               block or an *absent* text block.  The rules are:
               - "Node-kind: file" blocks have an empty (i.e. present, but
                 zero-length) text block, since they represent a file
                 modification.  Note that file-copied-text-unmodified blocks
                 have no Content-length - even if they should have contained
                 a modified property block, the pre-0.14 dumper forgets to
                 dump the modified properties.
               - If it is not a file node, then it is a revision or directory,
                 and so has an absent text block.
            */
            const char *node_kind;
            svn_filesize_t cl_value = svn__atoui64(content_length)
                                      - actual_prop_length;

            if (cl_value ||
                    ((node_kind = svn_hash_gets(headers,
                                                SVN_REPOS_DUMPFILE_NODE_KIND))
                     && strcmp(node_kind, "file") == 0)
               )
                SVN_ERR(parse_text_block(stream,
                                         cl_value,
                                         FALSE,
                                         parse_fns,
                                         found_node ? node_baton : rev_baton,
                                         buffer,
                                         buflen,
                                         found_node ? nodepool : revpool));
        }

        /* if we have a content-length header, did we read all of it?
           in case of an old v1, we *always* read all of it, because
           text-content-length == content-length - prop-content-length
        */
        if (content_length && ! old_v1_with_cl)
        {
            apr_size_t rlen, num_to_read;
            svn_filesize_t remaining =
                svn__atoui64(content_length) -
                (prop_cl ? svn__atoui64(prop_cl) : 0) -
                (text_cl ? svn__atoui64(text_cl) : 0);


            if (remaining < 0)
                return svn_error_create(SVN_ERR_STREAM_MALFORMED_DATA, NULL,
                                        _("Sum of subblock sizes larger than "
                                          "total block content length"));

            /* Consume remaining bytes in this content block */
            while (remaining > 0)
            {
                if (remaining >= (svn_filesize_t)buflen)
                    rlen = buflen;
                else
                    rlen = (apr_size_t) remaining;

                num_to_read = rlen;
                SVN_ERR(svn_stream_read(stream, buffer, &rlen));
                remaining -= rlen;
                if (rlen != num_to_read)
                    return stream_ran_dry();
            }
        }

        /* If we just finished processing a node record, we need to
           close the node record and clear the per-node subpool. */
        if (found_node)
        {
            SVN_ERR(parse_fns->close_node(node_baton));
            svn_pool_clear(nodepool);
        }

        /*** End of processing for one record. ***/

    } /* end of stream */

    /* Close out whatever revision we're in. */
    if (rev_baton != NULL)
        SVN_ERR(parse_fns->close_revision(rev_baton));

    svn_pool_destroy(linepool);
    svn_pool_destroy(revpool);
    svn_pool_destroy(nodepool);
    return SVN_NO_ERROR;
}
Ejemplo n.º 6
0
/* 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.)

   Set *ACTUAL_LENGTH to the number of bytes consumed from STREAM.
   If an error is returned, the value of *ACTUAL_LENGTH is undefined.

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

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

        svn_pool_clear(proppool);

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

        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"));
        }

        *actual_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] == ' '))
        {
            char *keybuf;
            apr_uint64_t len;

            SVN_ERR(svn_cstring_strtoui64(&len, buf + 2, 0, APR_SIZE_MAX, 10));
            SVN_ERR(read_key_or_val(&keybuf, actual_length,
                                    stream, (apr_size_t)len, proppool));

            /* Read a val length line */
            SVN_ERR(svn_stream_readline(stream, &strbuf, "\n", &eof, proppool));
            if (eof)
                return stream_ran_dry();

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

            if ((buf[0] == 'V') && (buf[1] == ' '))
            {
                svn_string_t propstring;
                char *valbuf;
                apr_int64_t val;

                SVN_ERR(svn_cstring_atoi64(&val, buf + 2));
                propstring.len = (apr_size_t)val;
                SVN_ERR(read_key_or_val(&valbuf, actual_length,
                                        stream, propstring.len, proppool));
                propstring.data = valbuf;

                /* 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] == ' '))
        {
            char *keybuf;
            apr_uint64_t len;

            SVN_ERR(svn_cstring_strtoui64(&len, buf + 2, 0, APR_SIZE_MAX, 10));
            SVN_ERR(read_key_or_val(&keybuf, actual_length,
                                    stream, (apr_size_t)len, proppool));

            /* 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) */

    svn_pool_destroy(proppool);
    return SVN_NO_ERROR;
}
Ejemplo n.º 7
0
static svn_error_t *
test_stream_seek_file(apr_pool_t *pool)
{
  static const char *file_data[2] = {"One", "Two"};
  svn_stream_t *stream;
  svn_stringbuf_t *line;
  svn_boolean_t eof;
  apr_file_t *f;
  static const char *fname = "test_stream_seek.txt";
  int j;
  apr_status_t status;
  static const char *NL = APR_EOL_STR;
  svn_stream_mark_t *mark;

  status = apr_file_open(&f, fname, (APR_READ | APR_WRITE | APR_CREATE |
                         APR_TRUNCATE | APR_DELONCLOSE), APR_OS_DEFAULT, pool);
  if (status != APR_SUCCESS)
    return svn_error_createf(SVN_ERR_TEST_FAILED, NULL, "Cannot open '%s'",
                             fname);

  /* Create the file. */
  for (j = 0; j < 2; j++)
    {
      apr_size_t len;

      len = strlen(file_data[j]);
      status = apr_file_write(f, file_data[j], &len);
      if (status || len != strlen(file_data[j]))
        return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
                                 "Cannot write to '%s'", fname);
      len = strlen(NL);
      status = apr_file_write(f, NL, &len);
      if (status || len != strlen(NL))
        return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
                                 "Cannot write to '%s'", fname);
    }

  /* Create a stream to read from the file. */
  stream = svn_stream_from_aprfile2(f, FALSE, pool);
  SVN_ERR(svn_stream_reset(stream));
  SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
  SVN_TEST_ASSERT(! eof && strcmp(line->data, file_data[0]) == 0);
  /* Set a mark at the beginning of the second line of the file. */
  SVN_ERR(svn_stream_mark(stream, &mark, pool));
  /* Read the second line and then seek back to the mark. */
  SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
  SVN_TEST_ASSERT(! eof && strcmp(line->data, file_data[1]) == 0);
  SVN_ERR(svn_stream_seek(stream, mark));
  /* The next read should return the second line again. */
  SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
  SVN_TEST_ASSERT(! eof && strcmp(line->data, file_data[1]) == 0);
  /* The next read should return EOF. */
  SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
  SVN_TEST_ASSERT(eof);

  /* Go back to the beginning of the last line and try to skip it
   * NOT including the EOL. */
  SVN_ERR(svn_stream_seek(stream, mark));
  SVN_ERR(svn_stream_skip(stream, strlen(file_data[1])));
  /* The remaining line should be empty */
  SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
  SVN_TEST_ASSERT(! eof && strcmp(line->data, "") == 0);
  /* The next read should return EOF. */
  SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
  SVN_TEST_ASSERT(eof);

  SVN_ERR(svn_stream_close(stream));

  return SVN_NO_ERROR;
}
Ejemplo n.º 8
0
/* Read the next entry in the changes record from file FILE and store
   the resulting change in *CHANGE_P.  If there is no next record,
   store NULL there.  Perform all allocations from POOL. */
static svn_error_t *
read_change(change_t **change_p,
            svn_stream_t *stream,
            apr_pool_t *result_pool,
            apr_pool_t *scratch_pool)
{
  svn_stringbuf_t *line;
  svn_boolean_t eof = TRUE;
  change_t *change;
  char *str, *last_str, *kind_str;
  svn_fs_path_change2_t *info;

  /* Default return value. */
  *change_p = NULL;

  SVN_ERR(svn_stream_readline(stream, &line, "\n", &eof, scratch_pool));

  /* Check for a blank line. */
  if (eof || (line->len == 0))
    return SVN_NO_ERROR;

  change = apr_pcalloc(result_pool, sizeof(*change));
  info = &change->info;
  last_str = line->data;

  /* Get the node-id of the change. */
  str = svn_cstring_tokenize(" ", &last_str);
  if (str == NULL)
    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                            _("Invalid changes line in rev-file"));

  SVN_ERR(svn_fs_fs__id_parse(&info->node_rev_id, str, result_pool));
  if (info->node_rev_id == NULL)
    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                            _("Invalid changes line in rev-file"));

  /* Get the change type. */
  str = svn_cstring_tokenize(" ", &last_str);
  if (str == NULL)
    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                            _("Invalid changes line in rev-file"));

  /* Don't bother to check the format number before looking for
   * node-kinds: just read them if you find them. */
  info->node_kind = svn_node_unknown;
  kind_str = strchr(str, '-');
  if (kind_str)
    {
      /* Cap off the end of "str" (the action). */
      *kind_str = '\0';
      kind_str++;
      if (strcmp(kind_str, SVN_FS_FS__KIND_FILE) == 0)
        info->node_kind = svn_node_file;
      else if (strcmp(kind_str, SVN_FS_FS__KIND_DIR) == 0)
        info->node_kind = svn_node_dir;
      else
        return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                                _("Invalid changes line in rev-file"));
    }

  if (strcmp(str, ACTION_MODIFY) == 0)
    {
      info->change_kind = svn_fs_path_change_modify;
    }
  else if (strcmp(str, ACTION_ADD) == 0)
    {
      info->change_kind = svn_fs_path_change_add;
    }
  else if (strcmp(str, ACTION_DELETE) == 0)
    {
      info->change_kind = svn_fs_path_change_delete;
    }
  else if (strcmp(str, ACTION_REPLACE) == 0)
    {
      info->change_kind = svn_fs_path_change_replace;
    }
  else if (strcmp(str, ACTION_RESET) == 0)
    {
      info->change_kind = svn_fs_path_change_reset;
    }
  else
    {
      return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                              _("Invalid change kind in rev file"));
    }

  /* Get the text-mod flag. */
  str = svn_cstring_tokenize(" ", &last_str);
  if (str == NULL)
    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                            _("Invalid changes line in rev-file"));

  if (strcmp(str, FLAG_TRUE) == 0)
    {
      info->text_mod = TRUE;
    }
  else if (strcmp(str, FLAG_FALSE) == 0)
    {
      info->text_mod = FALSE;
    }
  else
    {
      return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                              _("Invalid text-mod flag in rev-file"));
    }

  /* Get the prop-mod flag. */
  str = svn_cstring_tokenize(" ", &last_str);
  if (str == NULL)
    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                            _("Invalid changes line in rev-file"));

  if (strcmp(str, FLAG_TRUE) == 0)
    {
      info->prop_mod = TRUE;
    }
  else if (strcmp(str, FLAG_FALSE) == 0)
    {
      info->prop_mod = FALSE;
    }
  else
    {
      return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                              _("Invalid prop-mod flag in rev-file"));
    }

  /* Get the mergeinfo-mod flag if given.  Otherwise, the next thing
     is the path starting with a slash.  Also, we must initialize the
     flag explicitly because 0 is not valid for a svn_tristate_t. */
  info->mergeinfo_mod = svn_tristate_unknown;
  if (*last_str != '/')
    {
      str = svn_cstring_tokenize(" ", &last_str);
      if (str == NULL)
        return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                                _("Invalid changes line in rev-file"));

      if (strcmp(str, FLAG_TRUE) == 0)
        {
          info->mergeinfo_mod = svn_tristate_true;
        }
      else if (strcmp(str, FLAG_FALSE) == 0)
        {
          info->mergeinfo_mod = svn_tristate_false;
        }
      else
        {
          return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                              _("Invalid mergeinfo-mod flag in rev-file"));
        }
    }

  /* Get the changed path. */
  if (!svn_fspath__is_canonical(last_str))
    return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                            _("Invalid path in changes line"));

  change->path.len = strlen(last_str);
  change->path.data = apr_pstrdup(result_pool, last_str);

  /* Read the next line, the copyfrom line. */
  SVN_ERR(svn_stream_readline(stream, &line, "\n", &eof, scratch_pool));
  info->copyfrom_known = TRUE;
  if (eof || line->len == 0)
    {
      info->copyfrom_rev = SVN_INVALID_REVNUM;
      info->copyfrom_path = NULL;
    }
  else
    {
      last_str = line->data;
      SVN_ERR(parse_revnum(&info->copyfrom_rev, (const char **)&last_str));

      if (!svn_fspath__is_canonical(last_str))
        return svn_error_create(SVN_ERR_FS_CORRUPT, NULL,
                                _("Invalid copy-from path in changes line"));

      info->copyfrom_path = apr_pstrdup(result_pool, last_str);
    }

  *change_p = change;

  return SVN_NO_ERROR;
}
Ejemplo n.º 9
0
/* Implements svn_hash_read2 and svn_hash_read_incremental. */
static svn_error_t *
hash_read(apr_hash_t *hash, svn_stream_t *stream, const char *terminator,
          svn_boolean_t incremental, apr_pool_t *pool)
{
  svn_stringbuf_t *buf;
  svn_boolean_t eof;
  apr_size_t len, keylen, vallen;
  char c, *end, *keybuf, *valbuf;
  apr_pool_t *iterpool = svn_pool_create(pool);

  while (1)
    {
      svn_pool_clear(iterpool);

      /* Read a key length line.  Might be END, though. */
      SVN_ERR(svn_stream_readline(stream, &buf, "\n", &eof, iterpool));

      /* Check for the end of the hash. */
      if ((!terminator && eof && buf->len == 0)
          || (terminator && (strcmp(buf->data, terminator) == 0)))
        break;

      /* Check for unexpected end of stream */
      if (eof)
        return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                _("Serialized hash missing terminator"));

      if ((buf->len >= 3) && (buf->data[0] == 'K') && (buf->data[1] == ' '))
        {
          /* Get the length of the key */
          keylen = (size_t) strtoul(buf->data + 2, &end, 10);
          if (keylen == (size_t) ULONG_MAX || *end != '\0')
            return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                    _("Serialized hash malformed"));

          /* Now read that much into a buffer. */
          keybuf = apr_palloc(pool, keylen + 1);
          SVN_ERR(svn_stream_read(stream, keybuf, &keylen));
          keybuf[keylen] = '\0';

          /* Suck up extra newline after key data */
          len = 1;
          SVN_ERR(svn_stream_read(stream, &c, &len));
          if (c != '\n')
            return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                    _("Serialized hash malformed"));

          /* Read a val length line */
          SVN_ERR(svn_stream_readline(stream, &buf, "\n", &eof, iterpool));

          if ((buf->data[0] == 'V') && (buf->data[1] == ' '))
            {
              vallen = (size_t) strtoul(buf->data + 2, &end, 10);
              if (vallen == (size_t) ULONG_MAX || *end != '\0')
                return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                        _("Serialized hash malformed"));

              valbuf = apr_palloc(iterpool, vallen + 1);
              SVN_ERR(svn_stream_read(stream, valbuf, &vallen));
              valbuf[vallen] = '\0';

              /* Suck up extra newline after val data */
              len = 1;
              SVN_ERR(svn_stream_read(stream, &c, &len));
              if (c != '\n')
                return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                        _("Serialized hash malformed"));

              /* Add a new hash entry. */
              apr_hash_set(hash, keybuf, keylen,
                           svn_string_ncreate(valbuf, vallen, pool));
            }
          else
            return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                    _("Serialized hash malformed"));
        }
      else if (incremental && (buf->len >= 3)
               && (buf->data[0] == 'D') && (buf->data[1] == ' '))
        {
          /* Get the length of the key */
          keylen = (size_t) strtoul(buf->data + 2, &end, 10);
          if (keylen == (size_t) ULONG_MAX || *end != '\0')
            return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                    _("Serialized hash malformed"));

          /* Now read that much into a buffer. */
          keybuf = apr_palloc(iterpool, keylen + 1);
          SVN_ERR(svn_stream_read(stream, keybuf, &keylen));
          keybuf[keylen] = '\0';

          /* Suck up extra newline after key data */
          len = 1;
          SVN_ERR(svn_stream_read(stream, &c, &len));
          if (c != '\n')
            return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                    _("Serialized hash malformed"));

          /* Remove this hash entry. */
          apr_hash_set(hash, keybuf, keylen, NULL);
        }
      else
        {
          return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
                                  _("Serialized hash malformed"));
        }
    }

  svn_pool_destroy(iterpool);
  return SVN_NO_ERROR;
}
Ejemplo n.º 10
0
svn_error_t *
svn_client_blame5(const char *target,
                  const svn_opt_revision_t *peg_revision,
                  const svn_opt_revision_t *start,
                  const svn_opt_revision_t *end,
                  const svn_diff_file_options_t *diff_options,
                  svn_boolean_t ignore_mime_type,
                  svn_boolean_t include_merged_revisions,
                  svn_client_blame_receiver3_t receiver,
                  void *receiver_baton,
                  svn_client_ctx_t *ctx,
                  apr_pool_t *pool)
{
  struct file_rev_baton frb;
  svn_ra_session_t *ra_session;
  svn_revnum_t start_revnum, end_revnum;
  struct blame *walk, *walk_merged = NULL;
  apr_pool_t *iterpool;
  svn_stream_t *last_stream;
  svn_stream_t *stream;
  const char *target_abspath_or_url;

  if (start->kind == svn_opt_revision_unspecified
      || end->kind == svn_opt_revision_unspecified)
    return svn_error_create
      (SVN_ERR_CLIENT_BAD_REVISION, NULL, NULL);

  if (svn_path_is_url(target))
    target_abspath_or_url = target;
  else
    SVN_ERR(svn_dirent_get_absolute(&target_abspath_or_url, target, pool));

  /* Get an RA plugin for this filesystem object. */
  SVN_ERR(svn_client__ra_session_from_path(&ra_session, &end_revnum, NULL,
                                           target, NULL, peg_revision, end,
                                           ctx, pool));

  SVN_ERR(svn_client__get_revision_number(&start_revnum, NULL, ctx->wc_ctx,
                                          target_abspath_or_url, ra_session,
                                          start, pool));

  if (end_revnum < start_revnum)
    return svn_error_create
      (SVN_ERR_CLIENT_BAD_REVISION, NULL,
       _("Start revision must precede end revision"));

  frb.start_rev = start_revnum;
  frb.end_rev = end_revnum;
  frb.target = target;
  frb.ctx = ctx;
  frb.diff_options = diff_options;
  frb.ignore_mime_type = ignore_mime_type;
  frb.include_merged_revisions = include_merged_revisions;
  frb.last_filename = NULL;
  frb.last_original_filename = NULL;
  frb.chain = apr_palloc(pool, sizeof(*frb.chain));
  frb.chain->blame = NULL;
  frb.chain->avail = NULL;
  frb.chain->pool = pool;
  if (include_merged_revisions)
    {
      frb.merged_chain = apr_palloc(pool, sizeof(*frb.merged_chain));
      frb.merged_chain->blame = NULL;
      frb.merged_chain->avail = NULL;
      frb.merged_chain->pool = pool;
    }

  SVN_ERR(svn_ra_get_repos_root2(ra_session, &frb.repos_root_url, pool));

  frb.mainpool = pool;
  /* The callback will flip the following two pools, because it needs
     information from the previous call.  Obviously, it can't rely on
     the lifetime of the pool provided by get_file_revs. */
  frb.lastpool = svn_pool_create(pool);
  frb.currpool = svn_pool_create(pool);
  if (include_merged_revisions)
    {
      frb.filepool = svn_pool_create(pool);
      frb.prevfilepool = svn_pool_create(pool);
    }

  /* Collect all blame information.
     We need to ensure that we get one revision before the start_rev,
     if available so that we can know what was actually changed in the start
     revision. */
  SVN_ERR(svn_ra_get_file_revs2(ra_session, "",
                                start_revnum - (start_revnum > 0 ? 1 : 0),
                                end_revnum, include_merged_revisions,
                                file_rev_handler, &frb, pool));

  if (end->kind == svn_opt_revision_working)
    {
      /* If the local file is modified we have to call the handler on the
         working copy file with keywords unexpanded */
      svn_wc_status3_t *status;

      SVN_ERR(svn_wc_status3(&status, ctx->wc_ctx, target_abspath_or_url, pool,
                             pool));

      if (status->text_status != svn_wc_status_normal)
        {
          apr_hash_t *props;
          svn_stream_t *wcfile;
          svn_string_t *keywords;
          svn_stream_t *tempfile;
          const char *temppath;
          apr_hash_t *kw = NULL;

          SVN_ERR(svn_wc_prop_list2(&props, ctx->wc_ctx, target_abspath_or_url,
                                    pool, pool));
          SVN_ERR(svn_stream_open_readonly(&wcfile, target, pool, pool));

          keywords = apr_hash_get(props, SVN_PROP_KEYWORDS,
                                  APR_HASH_KEY_STRING);

          if (keywords)
            SVN_ERR(svn_subst_build_keywords2(&kw, keywords->data, NULL, NULL,
                                              0, NULL, pool));

          wcfile = svn_subst_stream_translated(wcfile, "\n", TRUE, kw, FALSE,
                                               pool);

          SVN_ERR(svn_stream_open_unique(&tempfile, &temppath, NULL,
                                         svn_io_file_del_on_pool_cleanup,
                                         pool, pool));

          SVN_ERR(svn_stream_copy3(wcfile, tempfile, ctx->cancel_func,
                                   ctx->cancel_baton, pool));

          SVN_ERR(add_file_blame(frb.last_filename, temppath, frb.chain, NULL,
                                 frb.diff_options, pool));

          frb.last_filename = temppath;
        }
    }

  /* Report the blame to the caller. */

  /* The callback has to have been called at least once. */
  SVN_ERR_ASSERT(frb.last_filename != NULL);

  /* Create a pool for the iteration below. */
  iterpool = svn_pool_create(pool);

  /* Open the last file and get a stream. */
  SVN_ERR(svn_stream_open_readonly(&last_stream, frb.last_filename,
                                   pool, pool));
  stream = svn_subst_stream_translated(last_stream,
                                       "\n", TRUE, NULL, FALSE, pool);

  /* Perform optional merged chain normalization. */
  if (include_merged_revisions)
    {
      /* If we never created any blame for the original chain, create it now,
         with the most recent changed revision.  This could occur if a file
         was created on a branch and them merged to another branch.  This is
         semanticly a copy, and we want to use the revision on the branch as
         the most recently changed revision.  ### Is this really what we want
         to do here?  Do the sematics of copy change? */
      if (!frb.chain->blame)
        frb.chain->blame = blame_create(frb.chain, frb.rev, 0);

      normalize_blames(frb.chain, frb.merged_chain, pool);
      walk_merged = frb.merged_chain->blame;
    }

  /* Process each blame item. */
  for (walk = frb.chain->blame; walk; walk = walk->next)
    {
      apr_off_t line_no;
      svn_revnum_t merged_rev;
      const char *merged_path;
      apr_hash_t *merged_rev_props;

      if (walk_merged)
        {
          merged_rev = walk_merged->rev->revision;
          merged_rev_props = walk_merged->rev->rev_props;
          merged_path = walk_merged->rev->path;
        }
      else
        {
          merged_rev = SVN_INVALID_REVNUM;
          merged_rev_props = NULL;
          merged_path = NULL;
        }

      for (line_no = walk->start;
           !walk->next || line_no < walk->next->start;
           ++line_no)
        {
          svn_boolean_t eof;
          svn_stringbuf_t *sb;

          svn_pool_clear(iterpool);
          SVN_ERR(svn_stream_readline(stream, &sb, "\n", &eof, iterpool));
          if (ctx->cancel_func)
            SVN_ERR(ctx->cancel_func(ctx->cancel_baton));
          if (!eof || sb->len)
            {
              if (walk->rev)
                SVN_ERR(receiver(receiver_baton, start_revnum, end_revnum,
                                 line_no, walk->rev->revision,
                                 walk->rev->rev_props, merged_rev,
                                 merged_rev_props, merged_path,
                                 sb->data, FALSE, iterpool));
              else
                SVN_ERR(receiver(receiver_baton, start_revnum, end_revnum,
                                 line_no, SVN_INVALID_REVNUM,
                                 NULL, SVN_INVALID_REVNUM,
                                 NULL, NULL,
                                 sb->data, TRUE, iterpool));
            }
          if (eof) break;
        }

      if (walk_merged)
        walk_merged = walk_merged->next;
    }

  SVN_ERR(svn_stream_close(stream));

  svn_pool_destroy(frb.lastpool);
  svn_pool_destroy(frb.currpool);
  if (include_merged_revisions)
    {
      svn_pool_destroy(frb.filepool);
      svn_pool_destroy(frb.prevfilepool);
    }
  svn_pool_destroy(iterpool);

  return SVN_NO_ERROR;
}
Ejemplo n.º 11
0
svn_error_t *
svn_client_blame4(const char *target,
                  const svn_opt_revision_t *peg_revision,
                  const svn_opt_revision_t *start,
                  const svn_opt_revision_t *end,
                  const svn_diff_file_options_t *diff_options,
                  svn_boolean_t ignore_mime_type,
                  svn_boolean_t include_merged_revisions,
                  svn_client_blame_receiver2_t receiver,
                  void *receiver_baton,
                  svn_client_ctx_t *ctx,
                  apr_pool_t *pool)
{
  struct file_rev_baton frb;
  svn_ra_session_t *ra_session;
  const char *url;
  svn_revnum_t start_revnum, end_revnum;
  struct blame *walk, *walk_merged = NULL;
  apr_file_t *file;
  apr_pool_t *iterpool;
  svn_stream_t *stream;

  if (start->kind == svn_opt_revision_unspecified
      || end->kind == svn_opt_revision_unspecified)
    return svn_error_create
      (SVN_ERR_CLIENT_BAD_REVISION, NULL, NULL);
  else if (start->kind == svn_opt_revision_working
           || end->kind == svn_opt_revision_working)
    return svn_error_create
      (SVN_ERR_UNSUPPORTED_FEATURE, NULL,
       _("blame of the WORKING revision is not supported"));

  /* Get an RA plugin for this filesystem object. */
  SVN_ERR(svn_client__ra_session_from_path(&ra_session, &end_revnum,
                                           &url, target, NULL,
                                           peg_revision, end,
                                           ctx, pool));

  SVN_ERR(svn_client__get_revision_number(&start_revnum, NULL, ra_session,
                                          start, target, pool));

  if (end_revnum < start_revnum)
    return svn_error_create
      (SVN_ERR_CLIENT_BAD_REVISION, NULL,
       _("Start revision must precede end revision"));

  frb.start_rev = start_revnum;
  frb.end_rev = end_revnum;
  frb.target = target;
  frb.ctx = ctx;
  frb.diff_options = diff_options;
  frb.ignore_mime_type = ignore_mime_type;
  frb.include_merged_revisions = include_merged_revisions;
  frb.last_filename = NULL;
  frb.last_original_filename = NULL;
  frb.chain = apr_palloc(pool, sizeof(*frb.chain));
  frb.chain->blame = NULL;
  frb.chain->avail = NULL;
  frb.chain->pool = pool;
  if (include_merged_revisions)
    {
      frb.merged_chain = apr_palloc(pool, sizeof(*frb.merged_chain));
      frb.merged_chain->blame = NULL;
      frb.merged_chain->avail = NULL;
      frb.merged_chain->pool = pool;
    }

  SVN_ERR(svn_io_temp_dir(&frb.tmp_path, pool));
  frb.tmp_path = svn_path_join(frb.tmp_path, "tmp", pool),

  frb.mainpool = pool;
  /* The callback will flip the following two pools, because it needs
     information from the previous call.  Obviously, it can't rely on
     the lifetime of the pool provided by get_file_revs. */
  frb.lastpool = svn_pool_create(pool);
  frb.currpool = svn_pool_create(pool);
  if (include_merged_revisions)
    {
      frb.filepool = svn_pool_create(pool);
      frb.prevfilepool = svn_pool_create(pool);
    }

  /* Collect all blame information.
     We need to ensure that we get one revision before the start_rev,
     if available so that we can know what was actually changed in the start
     revision. */
  SVN_ERR(svn_ra_get_file_revs2(ra_session, "",
                                start_revnum - (start_revnum > 0 ? 1 : 0),
                                end_revnum, include_merged_revisions,
                                file_rev_handler, &frb, pool));

  /* Report the blame to the caller. */

  /* The callback has to have been called at least once. */
  assert(frb.last_filename != NULL);

  /* Create a pool for the iteration below. */
  iterpool = svn_pool_create(pool);

  /* Open the last file and get a stream. */
  SVN_ERR(svn_io_file_open(&file, frb.last_filename, APR_READ | APR_BUFFERED,
                           APR_OS_DEFAULT, pool));
  stream = svn_subst_stream_translated(svn_stream_from_aprfile(file, pool),
                                       "\n", TRUE, NULL, FALSE, pool);

  /* Perform optional merged chain normalization. */
  if (include_merged_revisions)
    {
      /* If we never created any blame for the original chain, create it now,
         with the most recent changed revision.  This could occur if a file
         was created on a branch and them merged to another branch.  This is
         semanticly a copy, and we want to use the revision on the branch as
         the most recently changed revision.  ### Is this really what we want
         to do here?  Do the sematics of copy change? */
      if (!frb.chain->blame)
        frb.chain->blame = blame_create(frb.chain, frb.rev, 0);

      normalize_blames(frb.chain, frb.merged_chain, pool);
      walk_merged = frb.merged_chain->blame;
    }

  /* Process each blame item. */
  for (walk = frb.chain->blame; walk; walk = walk->next)
    {
      apr_off_t line_no;
      svn_revnum_t merged_rev;
      const char *merged_author, *merged_date, *merged_path;

      if (walk_merged)
        {
          merged_rev = walk_merged->rev->revision;
          merged_author = walk_merged->rev->author;
          merged_date = walk_merged->rev->date;
          merged_path = walk_merged->rev->path;
        }
      else
        {
          merged_rev = SVN_INVALID_REVNUM;
          merged_author = NULL;
          merged_date = NULL;
          merged_path = NULL;
        }

      for (line_no = walk->start;
           !walk->next || line_no < walk->next->start;
           ++line_no)
        {
          svn_boolean_t eof;
          svn_stringbuf_t *sb;

          svn_pool_clear(iterpool);
          SVN_ERR(svn_stream_readline(stream, &sb, "\n", &eof, iterpool));
          if (ctx->cancel_func)
            SVN_ERR(ctx->cancel_func(ctx->cancel_baton));
          if (!eof || sb->len)
            SVN_ERR(receiver(receiver_baton, line_no, walk->rev->revision,
                             walk->rev->author, walk->rev->date,
                             merged_rev, merged_author, merged_date,
                             merged_path, sb->data, iterpool));
          if (eof) break;
        }

      if (walk_merged)
        walk_merged = walk_merged->next;
    }

  SVN_ERR(svn_stream_close(stream));

  /* We don't need the temp file any more. */
  SVN_ERR(svn_io_file_close(file, pool));

  svn_pool_destroy(frb.lastpool);
  svn_pool_destroy(frb.currpool);
  if (include_merged_revisions)
    {
      svn_pool_destroy(frb.filepool);
      svn_pool_destroy(frb.prevfilepool);
    }
  svn_pool_destroy(iterpool);

  return SVN_NO_ERROR;
}
Ejemplo n.º 12
0
svn_error_t *
svn_ra_blame (svn_ra_plugin_t *ra_lib, void *ra_session,
              const char *target,
              svn_revnum_t start_revnum,
              svn_revnum_t end_revnum,
              svn_client_blame_receiver_t receiver,
              void *receiver_baton,
              apr_pool_t *pool)
{
    struct file_rev_baton frb;
    struct blame *walk;
    apr_file_t *tempfile;
    apr_pool_t *iterpool;
    svn_stream_t *stream;
    svn_error_t *err;

    // SVN_INVALID_REVNUM (=head revision)
    if ((end_revnum < start_revnum) && (end_revnum != SVN_INVALID_REVNUM))
      return svn_error_create(SVN_ERR_CLIENT_BAD_REVISION, NULL,
                              ("Start revision must precede end revision"));

    frb.start_rev = start_revnum;
    frb.end_rev = end_revnum;
    frb.target = target;
    frb.last_filename = NULL;
    frb.blame = NULL;
    frb.avail = NULL;
    frb.mainpool = pool;

  /* The callback will flip the following two pools, because it needs
     information from the previous call.  Obviously, it can't rely on
     the lifetime of the pool provided by get_file_revs. */
    frb.lastpool = svn_pool_create (pool);
    frb.currpool = svn_pool_create (pool);

    // do the actual blame
    err = do_blame (target, ra_lib, ra_session, &frb);
    if (err) return err;

    /* Report the blame to the caller. */

    /* The callback has to have been called at least once. */
    assert (frb.last_filename != NULL);

    /* Create a pool for the iteration below. */
    iterpool = svn_pool_create (pool);

    /* Open the last file and get a stream. */
    err = svn_io_file_open (&tempfile, frb.last_filename, APR_READ, APR_OS_DEFAULT, pool);
    if (err) return err;
    stream = svn_stream_from_aprfile(tempfile, pool);

    /* Process each blame item. */
    for (walk = frb.blame; walk; walk = walk->next)
    {
        apr_off_t line_no;
        for (line_no = walk->start; !walk->next || line_no < walk->next->start; ++line_no)
        {
            svn_boolean_t eof;
            svn_stringbuf_t *sb;
            apr_pool_clear (iterpool);
            err = svn_stream_readline (stream, &sb, "\n", &eof, iterpool);
            if (err) return err;

            if (!eof || sb->len)
                SVN_ERR (receiver (receiver_baton, line_no, walk->rev->revision,
                         walk->rev->author, walk->rev->date,
                         sb->data, iterpool));
            if (eof) break;
        }
    }

    err = svn_stream_close (stream);
    err = svn_io_file_close(tempfile, pool);
    svn_pool_destroy(frb.lastpool);
    svn_pool_destroy(frb.currpool);
    svn_pool_destroy(iterpool);
    return SVN_NO_ERROR;
}