tags:

views:

25

answers:

2

I'm trying to automate svnadmin dump using C# ProcessStartInfo.

The way I've done it on the command line is like so,

svnadmin dump c:\Repositories\hackyhacky > c:\backup\hackyhacky.svn_dump

Works a treat and dumps successfully, and I can verify this by restoring it into another repository like so

svnadmin load c:\Repositories\restore_test < c:\backup\hackyhacky.svn_dump

Which restores successfully - yay!

Now... I need to replicate the command line piping into another file using C#, but for some reason

var startInfo = new ProcessStartInfo(Path.Combine(SvnPath, "svnadmin"),"dump c:\Repositories\hackyhacky")
 {CreateNoWindow = true, RedirectStandardOutput = true,RedirectStandardError = true,UseShellExecute = false};
process.StartInfo = startInfo;
process.Start();
StreamReader reader = process.StandardOutput;
char[] standardOutputCharBuffer = new char[4096];
byte[] standardOutputByteBuffer;
int readChars = 0;
long totalReadBytes = 0;

// read from the StandardOutput, and write directly into another file

using (StreamWriter writer = new StreamWriter(@"C:\backup\hackyhacky.svn_dump", false)) {
    while (!reader.EndOfStream) {
       // read some chars from the standard out
       readChars = reader.Read(standardOutputCharBuffer, 0, standardOutputCharBuffer.Length);

       // convert the chars into bytes
       standardOutputByteBuffer = reader.CurrentEncoding.GetBytes(standardOutputCharBuffer);

       // write the bytes out into the file
       writer.Write(standardOutputCharBuffer.Take(readChars).ToArray());

       // increment the total read
       totalReadBytes += standardOutputByteBuffer.Length;
    }                    
}

Dumps the same repo into hackyhacky.svn_dump.

But when I run my load command line now

svnadmin load c:\Repositories\restore_test < c:\backup\hackyhacky.svn_dump

I get a checksum error weird-error!

svnadmin load c:\Repositories\restore_test < c:\backup\hackyhacky.svn_dump
< Started new transaction, based on original revision 1
     * adding path : Dependencies ... done.
     * adding path : Dependencies/BlogML.dll ...svnadmin: Checksum mismatch, fil
e '/Dependencies/BlogML.dll':
   expected:  d39863a4c14cf053d01f636002842bf9
     actual:  d19831be151d33650b3312a288aecadd

I'm guessing this is to do with how I'm redirecting and reading the StandardOutput.

Does anyone know the right way to mimic the command line file piping behaviour in C#?

Any help is greatly appreciated.

-CV

UPDATE

I've tried using a BinaryWriter and using the standardOutputByteBuffer to write to the file, but that doesn't work either. I get a different error about incorrect header format or something.

A: 

The first thing I'd try is writing the character array - not the byte array - to the file.

That should work as long as the output is just simple text. There's other encoding issues, though, if the output is more complex: you're writing the file as UTF-8, whereas the default for command-line output (I believe) is Windows-1252.

Stephen Cleary
Thanks Stephen, I am in the current code there writing the char array. I tried both switching the chars into bytes and writing those too. Both approaches don't work, and produce different errors
CVertex
A: 

Alright! If you can't beat em, join em....

I found a post where the author pipes to a file directly within the Process StartInfo, and claims it works.

http://weblogs.asp.net/israelio/archive/2004/08/31/223447.aspx

It didn't work for me, as described in another gentleman's post

http://webcache.googleusercontent.com/search?q=cache:http://www.deadbeef.com/index.php/redirecting_the_output_of_a_program_to_a

He writes a batch file first with the piping and then executes it...

amWriter bat = File.CreateText("foo.bat"); 
bat.WriteLine("@echo off"); 
bat.WriteLine("foo.exe -arg >" + dumpDir + "\\foo_arg.txt"); 
bat.Close(); 
Process task = new Process(); 
task.StartInfo.UseShellExecute = false; 
task.StartInfo.FileName = "foo.bat"; 
task.StartInfo.Arguments = ""; 
task.Start(); 
task.WaitForExit();

In his words:

Truly horrific, but it has the advantage of working!

To be perfectly frank, I'm a bit annoyed this has taken me as long as it has, so the batch file solution works well so I'm going to stick with it.

CVertex