File: TestXfer.cs

package info (click to toggle)
gtk-sharp2 2.8.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 21,208 kB
  • ctags: 5,843
  • sloc: xml: 245,420; cs: 24,183; sh: 8,547; ansic: 2,739; makefile: 1,582; perl: 1,177
file content (164 lines) | stat: -rw-r--r-- 4,909 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Gnome.Vfs;
using Mono.GetOptions;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("TestXfer")]
[assembly: AssemblyDescription("Test case for the Gnome.Vfs.Xfer class")]
[assembly: AssemblyCopyright("(C) 2004 Jeroen Zwartepoorte")]
[assembly: Mono.About("Distributed under the GPL")]
[assembly: Mono.UsageComplement("<src> <target>")]
[assembly: Mono.Author("Jeroen Zwartepoorte")]
[assembly: AssemblyVersion("1.0.*")]

namespace TestGnomeVfs {
	class TestXferOptions : Options {
		[Option("Copy directories recursively", 'r')]
		public bool recursive = false;
		
		[Option("Follow symlinks", 'L', "follow-symlinks")]
		public bool followSymlinks = false;
		
		[Option("Follow symlinks recursively", 'Z', "recursive-symlinks")]
		public bool recursiveSymlinks = false;

		[Option("Replace files automatically", 'R')]
		public bool replace = false;

		[Option("Delete source files", 'd', "delete-source")]
		public bool deleteSource = false;
		
		public TestXferOptions ()
		{
			ParsingMode = OptionsParsingMode.Both;
		}

		public override WhatToDoNext DoHelp ()
		{
			base.DoHelp();
			return WhatToDoNext.AbandonProgram; 
		}

		[Option("Show usage syntax", 'u', "usage")]
		public override WhatToDoNext DoUsage()
		{
			base.DoUsage();
			return WhatToDoNext.AbandonProgram; 
		}
	}

	public class TestXfer {
		static void Main (string[] args)
		{
			TestXferOptions opt = new TestXferOptions ();
			opt.ProcessArgs (args);
			
			if (opt.RemainingArguments.Length < 2) {
				opt.DoUsage ();
				return;
			}
			
			XferOptions options = XferOptions.Default;
			XferOverwriteMode overwriteMode = XferOverwriteMode.Query;
			if (opt.recursive) {
				Console.WriteLine ("Warning: Recursive xfer of directories.");
				options |= XferOptions.Recursive;
			}
			if (opt.followSymlinks) {
				Console.WriteLine ("Warning: Following symlinks.");
				options |= XferOptions.FollowLinks;
			}
			if (opt.recursiveSymlinks) {
				Console.WriteLine ("Warning: Following symlinks recursively.");
				options |= XferOptions.FollowLinksRecursive;
			}
			if (opt.replace) {
				Console.WriteLine ("Warning: Using replace overwrite mode.");
				overwriteMode = XferOverwriteMode.Replace;
			}
			if (opt.deleteSource) {
				Console.WriteLine ("Warning: Removing source files.");
				options |= XferOptions.Removesource;
			}
			
			Gnome.Vfs.Vfs.Initialize ();
			
			Gnome.Vfs.Uri source = new Gnome.Vfs.Uri (opt.RemainingArguments[0]);
			Console.WriteLine ("Source: `{0}'", source);
			Gnome.Vfs.Uri target = new Gnome.Vfs.Uri (opt.RemainingArguments[1]);
			Console.WriteLine ("Target: `{0}'", target);

			Result result = Xfer.XferUri (source, target, options,
						      XferErrorMode.Query,
						      overwriteMode,
						      new XferProgressCallback (OnProgress));
			Console.WriteLine ("Result: {0}", Gnome.Vfs.Vfs.ResultToString (result));
			
			Gnome.Vfs.Vfs.Shutdown ();
		}
		
		public static int OnProgress (XferProgressInfo info)
		{
			switch (info.Status) {
			case XferProgressStatus.Vfserror:
				Console.WriteLine ("Vfs error: {0}",
					Gnome.Vfs.Vfs.ResultToString (info.VfsStatus));
				break;
			case XferProgressStatus.Overwrite:
				Console.WriteLine ("Overwriting `{0}' with `{1}'",
						   info.TargetName, info.SourceName);
				break;
			case XferProgressStatus.Ok:
				Console.WriteLine ("Status: Ok");
				switch (info.Phase) {
				case XferPhase.PhaseInitial:
					Console.WriteLine ("Initial phase");
					return 1;
				case XferPhase.PhaseCollecting:
					Console.WriteLine ("Collecting file list");
					return 1;
				case XferPhase.PhaseReadytogo:
					Console.WriteLine ("Ready to go!");
					return 1;
				case XferPhase.PhaseOpensource:
					Console.WriteLine ("Opening source");
					return 1;
				case XferPhase.PhaseOpentarget:
					Console.WriteLine ("Opening target");
					return 1;
				case XferPhase.PhaseCopying:
					Console.WriteLine ("Transferring `{0}' to `{1}' " + 
							   "(file {2}/{3}, byte {4}/{5} in file, " +
							   "{6}/{7} total", info.SourceName,
							   info.TargetName, info.FileIndex,
							   info.FilesTotal, (long)info.BytesCopied,
							   (long)info.FileSize, info.TotalBytesCopied,
							   info.BytesTotal);
					break;
				case XferPhase.PhaseClosesource:
					Console.WriteLine ("Closing source");
					return 1;
				case XferPhase.PhaseClosetarget:
					Console.WriteLine ("Closing target");
					return 1;
				case XferPhase.PhaseFilecompleted:
					Console.WriteLine ("Done with `{0}' -> `{1}', going next",
							   info.SourceName, info.TargetName);
					return 1;
				case XferPhase.PhaseCompleted:
					Console.WriteLine ("All done.");
					return 1;
				default:
					Console.WriteLine ("Unexpected phase: {0}", info.Phase);
					return 1; // Keep going anyway.
				}
				break;
			case XferProgressStatus.Duplicate:
				break;
			}

			return 0;
		}
	}
}