Subversion Repository Public Repository

litesoft

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.util;

import java.io.*;
import java.util.*;

import org.litesoft.core.typeutils.*;

public class TriDirUpdater
{
    public static final String VERSION = "1.0";

    public static final int MAX_FILE_SIZE = 32 * 1024 * 1024;

    private static File checkPath( String pType, String pPath )
    {
        File zPath = new File( pPath );
        if ( !zPath.isDirectory() )
        {
            System.out.println( pType + " Path, Not a Directory: " + zPath.getAbsolutePath() );
            System.exit( 2 );
        }
        return zPath;
    }

    public static void main( String[] args )
            throws Exception
    {
        System.out.println( "3 (Tri) Directory Updater vs " + VERSION );
        String zEditor = System.getenv( "Editor" );
        if ( zEditor == null )
        {
            throw new IllegalStateException( "No 'Editor' environment variable" );
        }
        if ( args.length != 3 )
        {
            showHelp();
        }

        new TriDirUpdater( zEditor,
                           checkPath( "Update", args[0] ),
                           checkPath( "Original", args[1] ),
                           checkPath( "Working", args[2] ) ).process();
    }

    private static void showHelp()
    {
        System.out.println( "Requires 3 parameters:" );
        System.out.println();
        System.out.println( "   1) Update Directory Path" );
        System.out.println( "   2) Original Directory Path" );
        System.out.println( "   3) Working Directory Path" );
        System.out.println();
        System.out.println( "All the entries in the 'Update Directory Path' are compared with" );
        System.out.println( "all the entries in the 'Original Directory Path' and are processed" );
        System.out.println( "on a file by file basis in one of the following ways (affecting" );
        System.out.println( "the 'Working Directory Path'):" );
        System.out.println();
        System.out.println( "    1) Same - No Action taken." );
        System.out.println();
        System.out.println( "    2) No 'Original' file:" );
        System.out.println( "            a) 'Working' file exists" );
        System.out.println( "                    - Offer Merge between: 'Update' and 'Working'." );
        System.out.println( "            b) No 'Working' file" );
        System.out.println( "                    - copy 'Update' to 'Working'." );
        System.out.println( "       Copy 'Update' to 'Original'." );
        System.out.println();
        System.out.println( "    3) No 'Update' file:" );
        System.out.println( "            a) 'Working' same as 'Original' " );
        System.out.println( "                    - Delete 'Working'." );
        System.out.println( "            b) 'Working' differs from 'Original' " );
        System.out.println( "                    - Offer Delete 'Working'?" );
        System.out.println( "                        (if 'Working' is 0 length, then delete)." );
        System.out.println( "       Delete 'Original'." );
        System.out.println();
        System.out.println( "    4) Different:" );
        System.out.println( "            a) 'Working' same as 'Original' " );
        System.out.println( "                    - Copy 'Update' to 'Working'." );
        System.out.println( "            b) 'Working' differs from 'Original' " );
        System.out.println( "                    - Offer 3-way Merge: " );
        System.out.println( "                        'Update', 'Original',  and 'Working'." );
        System.out.println( "       Copy 'Update' to 'Original'." );
        System.exit( 1 );
    }

    private final String mEditor;
    private final File mUpdatePath;
    private final TargetTree mOriginal, mWorking;

    private TriDirUpdater( String pEditor, File pUpdatePath, File pOriginalPath, File pWorkingPath )
    {
        mEditor = pEditor;
        mUpdatePath = pUpdatePath;
        mOriginal = new TargetTree( pOriginalPath );
        mWorking = new TargetTree( pWorkingPath );
    }

    private void process()
    {
        mOriginal.populate();
        mWorking.populate();
        Queue<String> zDirectoriesToProcess = new LinkedList<String>();
        process( zDirectoriesToProcess, "", getDirectoryEntries( mUpdatePath ) );
        while ( !zDirectoriesToProcess.isEmpty() )
        {
            String zRelativeDirectoryPath = zDirectoriesToProcess.remove();
            process( zDirectoriesToProcess, zRelativeDirectoryPath, getDirectoryEntries( new File( mUpdatePath, zRelativeDirectoryPath ) ) );
        }
        for ( String zUnmatchedOriginalRelativePath : mOriginal.getRelativePaths() )
        {
            processNoUpdateFile( zUnmatchedOriginalRelativePath );
        }
    }

    private void process( Queue<String> pDirectoryCollector, String pRelativeDirectoryPath, String[] pNames )
    {
        for ( String zName : pNames )
        {
            String zRelativePath = relativePath( pRelativeDirectoryPath, zName );
            File zFile = new File( mUpdatePath, zRelativePath );
            if ( zFile.isFile() )
            {
                processUpdateFile( zRelativePath );
                continue;
            }
            if ( zFile.isDirectory() )
            {
                pDirectoryCollector.add( zRelativePath );
                continue;
            }
            throw new IllegalStateException( "Neither a File or a Directory: " + zFile.getAbsolutePath() );
        }
    }

    private static String[] getDirectoryEntries( File pDirectory )
    {
        String[] zFiles = pDirectory.list();
        if ( zFiles != null )
        {
            return zFiles;
        }
        throw new IllegalStateException( "Unable to get Directory Listing from: " + pDirectory.getAbsolutePath() );
    }

    private static String relativePath( String pRelativeDirectoryPath, String pName )
    {
        return (pRelativeDirectoryPath.length() == 0) ? pName : pRelativeDirectoryPath + "/" + pName;
    }

    private static class TargetTree
    {
        private final Map<String, File> mEntries = Maps.newHashMap();
        private final File mBasePath;

        public TargetTree( File pBasePath )
        {
            mBasePath = pBasePath;
        }

        public void populate()
        {
            populate( "", getDirectoryEntries( mBasePath ) );
        }

        private void populate( String pRelativeDirectoryPath, String[] pNames )
        {
            for ( String zName : pNames )
            {
                String zRelativePath = relativePath( pRelativeDirectoryPath, zName );
                File zFile = new File( mBasePath, zRelativePath );
                if ( zFile.isFile() )
                {
                    mEntries.put( zRelativePath, zFile );
                    continue;
                }
                if ( !zFile.isDirectory() )
                {
                    throw new IllegalStateException( "Neither a File or a Directory: " + zFile.getAbsolutePath() );
                }
                populate( zRelativePath, getDirectoryEntries( zFile ) );
            }
        }

        public File getFile( String pRelativePath )
        {
            return mEntries.get( pRelativePath );
        }

        public void removeEntry( String pRelativePath )
        {
            mEntries.remove( pRelativePath );
        }

        public String[] getRelativePaths()
        {
            return mEntries.keySet().toArray( new String[mEntries.size()] );
        }

        public void delete( String pRelativePath )
        {
            FileUtils.deleteIfExists( mEntries.remove( pRelativePath ) );
        }

        public void save( String pRelativePath, File pFile, byte[] pContents )
        {
            save( pFile, pContents );
            removeEntry( pRelativePath );
        }

        public void save( String pRelativePath, byte[] pContents )
        {
            save( new File( mBasePath, pRelativePath ), pContents );
        }

        private void save( File pFile, byte[] pContents )
        {
            FileUtils.store( pFile, pContents );
            FileUtils.deleteIfExists( new File( pFile.getAbsolutePath() + ".bak" ) );
        }
    }

    private byte[] load( File pUpdateFile )
    {
        return FileUtils.load( pUpdateFile, MAX_FILE_SIZE );
    }

    private boolean different( byte[] pBytes1, byte[] pBytes2 )
    {
        return !Arrays.equals( pBytes1, pBytes2 );
    }

    // Options:
    //
    //     No 'Original' file...
    //     Same - No Action taken...
    //     Different...
    private void processUpdateFile( String pRelativePath )
    {
        File zUpdateFile = new File( mUpdatePath, pRelativePath );
        byte[] zUpdateContents = load( zUpdateFile );
        File zOriginalFile = mOriginal.getFile( pRelativePath );
        if ( zOriginalFile == null )
        {
            processUpdateButNoOriginal( pRelativePath, zUpdateFile, zUpdateContents );
            return;
        }
        byte[] zOriginalContents = load( zOriginalFile );
        if ( different( zUpdateContents, zOriginalContents ) )
        {
            processUpdateDifferentThanOriginal( pRelativePath, zUpdateFile, zUpdateContents, zOriginalFile, zOriginalContents );
            return;
        }
        // Same!
        mOriginal.removeEntry( pRelativePath );
        mWorking.removeEntry( pRelativePath );
    }

    // No 'Update' file:
    //      a) 'Working' same as 'Original'
    //              - Delete 'Working'.
    //      b) 'Working' differs from 'Original'
    //              - Offer Delete 'Working'?
    //                  (if 'Working' is 0 length, then delete).
    //   Delete 'Original'.
    private void processNoUpdateFile( String pRelativePath )
    {
        File zWorkingFile = mWorking.getFile( pRelativePath );
        if ( zWorkingFile != null )
        {
            File zOriginalFile = mOriginal.getFile( pRelativePath );
            byte[] zWorkingContents = load( zWorkingFile );
            byte[] zOriginalContents = load( zOriginalFile );
            if ( different( zOriginalContents, zWorkingContents ) )
            {
                checkWorkingChangedButNoUpdate( pRelativePath, zWorkingFile, zOriginalFile );
                // ************************************* If Working is Empty, then complete merge!
                if ( zWorkingFile.length() != 0 )
                {
                    // Ignore!
                    mOriginal.removeEntry( pRelativePath );
                    mWorking.removeEntry( pRelativePath );
                    return;
                }
            }
            mWorking.delete( pRelativePath );
        }
        mOriginal.delete( pRelativePath );
    }

    // No 'Original' file:
    //      a) 'Working' file exists
    //              - Offer Merge between: 'Update' and 'Working'.
    //      b) No 'Working' file
    //              - copy 'Update' to 'Working'.
    //   Copy 'Update' to 'Original'.
    private void processUpdateButNoOriginal( String pRelativePath, File pUpdateFile, byte[] pUpdateContents )
    {
        File zWorkingFile = mWorking.getFile( pRelativePath );
        if ( zWorkingFile != null )
        {
            checkNewUpdateCollidesWithExistingWorking( pRelativePath, zWorkingFile, pUpdateFile );
            // ************************************* If Working is Empty, then complete merge!
            if ( zWorkingFile.length() != 0 )
            {
                // Ignore!
                mWorking.removeEntry( pRelativePath );
                return;
            }
        }
        mWorking.save( pRelativePath, pUpdateContents );
        mOriginal.save( pRelativePath, pUpdateContents );
    }

    // Different:
    //      a) 'Working' same as 'Original'
    //              - Copy 'Update' to 'Working'.
    //      b) 'Working' differs from 'Original'
    //              - Offer 3-way Merge:
    //                  'Update', 'Original',  and 'Working'.
    //   Copy 'Update' to 'Original'.
    private void processUpdateDifferentThanOriginal( String pRelativePath, File pUpdateFile, byte[] pUpdateContents, File pOriginalFile,
                                                     byte[] pOriginalContents )
    {
        File zWorkingFile = mWorking.getFile( pRelativePath );
        if ( zWorkingFile == null )
        {
            mWorking.save( pRelativePath, zWorkingFile, pUpdateContents );
        }
        else
        {
            byte[] zWorkingContents = load( zWorkingFile );
            if ( !different( pOriginalContents, zWorkingContents ) )
            {
                mWorking.save( pRelativePath, zWorkingFile, pUpdateContents );
            }
            else
            {
                checkUpdateAndWorkingDifferentThanOriginal_threeWayMerge( pRelativePath, zWorkingFile, pUpdateFile, pOriginalFile );
                // ************************************* If Working changed, then complete merge!
                if ( !different( zWorkingContents, load( zWorkingFile ) ) )
                {
                    // Ignore!
                    mOriginal.removeEntry( pRelativePath );
                    mWorking.removeEntry( pRelativePath );
                    return;
                }
            }
        }
        mOriginal.save( pRelativePath, pOriginalFile, pUpdateContents );
    }

    private void checkWorkingChangedButNoUpdate( String pRelativePath, File pWorkingFile, File pOriginalFile )
    {
        System.out.println( "Working Changed But No Update: " + pRelativePath + "\n" +
                            "   So Working shouldn't exist - Empty Working to complete merge..." );
        ShellUtils.shell( mEditor, pWorkingFile.getAbsolutePath(), pOriginalFile.getAbsolutePath() );
    }

    private void checkNewUpdateCollidesWithExistingWorking( String pRelativePath, File pWorkingFile, File pUpdateFile )
    {
        System.out.println( "New Update Collides With Existing Working: " + pRelativePath + "\n" +
                            "   Move contents of Working to new file (leaving Working empty) to complete merge..." );
        ShellUtils.shell( mEditor, pWorkingFile.getAbsolutePath(), pUpdateFile.getAbsolutePath() );
    }

    private void checkUpdateAndWorkingDifferentThanOriginal_threeWayMerge( String pRelativePath, File pWorkingFile, File pUpdateFile, File pOriginalFile )
    {
        System.out.println( "Change Collition, both Update AND Working differ from Original: " + pRelativePath + "\n" +
                            "   Change Working to complete merge..." );
        ShellUtils.shell( mEditor, pWorkingFile.getAbsolutePath(), pUpdateFile.getAbsolutePath(), pOriginalFile.getAbsolutePath() );
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/util/TriDirUpdater.java

Diff revisions: vs.
Revision Author Commited Message
906 Diff Diff GeorgeS picture GeorgeS Fri 07 Jun, 2013 22:50:38 +0000

Update to latest gwt-phonegap & MGWT.

878 Diff Diff GeorgeS picture GeorgeS Mon 10 Dec, 2012 03:45:05 +0000
823 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 16:10:13 +0000
812 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 17:45:40 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
80 Diff Diff GeorgeS picture GeorgeS Fri 29 Oct, 2010 17:23:26 +0000
76 Diff Diff GeorgeS picture GeorgeS Wed 27 Oct, 2010 00:24:59 +0000
73 Diff Diff GeorgeS picture GeorgeS Mon 25 Oct, 2010 20:47:48 +0000
72 Diff Diff GeorgeS picture GeorgeS Mon 25 Oct, 2010 19:13:46 +0000
71 GeorgeS picture GeorgeS Mon 25 Oct, 2010 18:30:09 +0000