Changeset 4705f01


Ignore:
Timestamp:
Apr 8, 2015 12:33:16 PM (6 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
780479be
Parents:
6aa1284
Message:

Router: Move update extraction code to new class in tasks/

Location:
router/java/src/net/i2p/router
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/Router.java

    r6aa1284 r4705f01  
    99 */
    1010
    11 import java.io.BufferedReader;
    1211import java.io.File;
    13 import java.io.FileInputStream;
    14 import java.io.InputStreamReader;
    15 import java.io.IOException;
    1612import java.util.Collection;
    1713import java.util.Collections;
    1814import java.util.List;
    19 import java.util.Locale;
    2015import java.util.Map;
    2116import java.util.Properties;
     
    5045import net.i2p.stat.StatManager;
    5146import net.i2p.util.ByteCache;
    52 import net.i2p.util.FileUtil;
    5347import net.i2p.util.FortunaRandomSource;
    5448import net.i2p.util.I2PAppThread;
     
    114108    private static final String EVENTLOG = "eventlog.txt";
    115109    private static final String PROP_JBIGI = "jbigi.loadedResource";
     110    public static final String UPDATE_FILE = "i2pupdate.zip";
    116111       
    117112    private static final String originalTimeZoneID;
     
    14961491            // I guess it's better to have the other-router check above this, we don't want to
    14971492            // overwrite an existing running router's jar files. Other than ours.
    1498             r.installUpdates();
     1493            InstallUpdate.installUpdates(r);
    14991494            // *********  Start no threads before here ********* //
    15001495            r.runRouter();
    1501         }
    1502     }
    1503    
    1504     public static final String UPDATE_FILE = "i2pupdate.zip";
    1505     private static final String DELETE_FILE = "deletelist.txt";
    1506    
    1507     /**
    1508      * Context must be available.
    1509      * Unzip update file found in the router dir OR base dir, to the base dir
    1510      *
    1511      * If successfull, will call exit() and never return.
    1512      *
    1513      * If we can't write to the base dir, complain.
    1514      * Note: _log not available here.
    1515      */
    1516     private void installUpdates() {
    1517         File updateFile = new File(_context.getRouterDir(), UPDATE_FILE);
    1518         boolean exists = updateFile.exists();
    1519         if (!exists) {
    1520             updateFile = new File(_context.getBaseDir(), UPDATE_FILE);
    1521             exists = updateFile.exists();
    1522         }
    1523         if (exists) {
    1524             // do a simple permissions test, if it fails leave the file in place and don't restart
    1525             File test = new File(_context.getBaseDir(), "history.txt");
    1526             if ((test.exists() && !test.canWrite()) || (!_context.getBaseDir().canWrite())) {
    1527                 System.out.println("ERROR: No write permissions on " + _context.getBaseDir() +
    1528                                    " to extract software update file");
    1529                 // carry on
    1530                 return;
    1531             }
    1532             System.out.println("INFO: Update file exists [" + UPDATE_FILE + "] - installing");
    1533             // verify the whole thing first
    1534             // we could remember this fails, and not bother restarting, but who cares...
    1535             boolean ok = FileUtil.verifyZip(updateFile);
    1536             if (ok) {
    1537                 // This may be useful someday. First added in 0.8.2
    1538                 // Moved above the extract so we don't NCDFE
    1539                 _config.put("router.updateLastInstalled", "" + System.currentTimeMillis());
    1540                 // Set the last version to the current version, since 0.8.13
    1541                 _config.put("router.previousVersion", RouterVersion.VERSION);
    1542                 _config.put("router.previousFullVersion", RouterVersion.FULL_VERSION);
    1543                 saveConfig();
    1544                 ok = FileUtil.extractZip(updateFile, _context.getBaseDir());
    1545             }
    1546 
    1547             // Very important - we have now trashed our jars.
    1548             // After this point, do not use any new I2P classes, or they will fail to load
    1549             // and we will die with NCDFE.
    1550             // Ideally, do not use I2P classes at all, new or not.
    1551             try {
    1552                 if (ok) {
    1553                     // We do this here so we may delete old jars before we restart
    1554                     deleteListedFiles();
    1555                     System.out.println("INFO: Update installed");
    1556                 } else {
    1557                     System.out.println("ERROR: Update failed!");
    1558                 }
    1559                 if (!ok) {
    1560                     // we can't leave the file in place or we'll continually restart, so rename it
    1561                     File bad = new File(_context.getRouterDir(), "BAD-" + UPDATE_FILE);
    1562                     boolean renamed = updateFile.renameTo(bad);
    1563                     if (renamed) {
    1564                         System.out.println("Moved update file to " + bad.getAbsolutePath());
    1565                     } else {
    1566                         System.out.println("Deleting file " + updateFile.getAbsolutePath());
    1567                         ok = true;  // so it will be deleted
    1568                     }
    1569                 }
    1570                 if (ok) {
    1571                     boolean deleted = updateFile.delete();
    1572                     if (!deleted) {
    1573                         System.out.println("ERROR: Unable to delete the update file!");
    1574                         updateFile.deleteOnExit();
    1575                     }
    1576                 }
    1577                 // exit whether ok or not
    1578                 if (_context.hasWrapper())
    1579                     System.out.println("INFO: Restarting after update");
    1580                 else
    1581                     System.out.println("WARNING: Exiting after update, restart I2P");
    1582             } catch (Throwable t) {
    1583                 // hide the NCDFE
    1584                 // hopefully the update file got deleted or we will loop
    1585             }
    1586             System.exit(EXIT_HARD_RESTART);
    1587         } else {
    1588             deleteJbigiFiles();
    1589             // It was here starting in 0.8.12 so it could be used the very first time
    1590             // Now moved up so it is usually run only after an update
    1591             // But the first time before jetty 6 it will run here...
    1592             // Here we can't remove jars
    1593             deleteListedFiles();
    1594         }
    1595     }
    1596 
    1597     /**
    1598      *  Remove extracted libjbigi.so and libjcpuid.so files if we have a newer jbigi.jar,
    1599      *  so the new ones will be extracted.
    1600      *  We do this after the restart, not after the extract, because it's safer, and
    1601      *  because people may upgrade their jbigi.jar file manually.
    1602      *
    1603      *  Copied from NativeBigInteger, which we can't access here or the
    1604      *  libs will get loaded.
    1605      */
    1606     private void deleteJbigiFiles() {
    1607             boolean isX86 = SystemVersion.isX86();
    1608             String osName = System.getProperty("os.name").toLowerCase(Locale.US);
    1609             boolean isWin = SystemVersion.isWindows();
    1610             boolean isMac = SystemVersion.isMac();
    1611             // only do this on these OSes
    1612             boolean goodOS = isWin || isMac ||
    1613                              osName.contains("linux") || osName.contains("freebsd");
    1614 
    1615             // only do this on these x86
    1616             File jbigiJar = new File(_context.getBaseDir(), "lib/jbigi.jar");
    1617             if (isX86 && goodOS && jbigiJar.exists()) {
    1618                 String libPrefix = (isWin ? "" : "lib");
    1619                 String libSuffix = (isWin ? ".dll" : isMac ? ".jnilib" : ".so");
    1620 
    1621                 File jcpuidLib = new File(_context.getBaseDir(), libPrefix + "jcpuid" + libSuffix);
    1622                 if (jcpuidLib.canWrite() && jbigiJar.lastModified() > jcpuidLib.lastModified()) {
    1623                     String path = jcpuidLib.getAbsolutePath();
    1624                     boolean success = FileUtil.copy(path, path + ".bak", true, true);
    1625                     if (success) {
    1626                         boolean success2 = jcpuidLib.delete();
    1627                         if (success2) {
    1628                             System.out.println("New jbigi.jar detected, moved jcpuid library to " +
    1629                                                path + ".bak");
    1630                             System.out.println("Check logs for successful installation of new library");
    1631                         }
    1632                     }
    1633                 }
    1634 
    1635                 File jbigiLib = new File(_context.getBaseDir(), libPrefix + "jbigi" + libSuffix);
    1636                 if (jbigiLib.canWrite() && jbigiJar.lastModified() > jbigiLib.lastModified()) {
    1637                     String path = jbigiLib.getAbsolutePath();
    1638                     boolean success = FileUtil.copy(path, path + ".bak", true, true);
    1639                     if (success) {
    1640                         boolean success2 = jbigiLib.delete();
    1641                         if (success2) {
    1642                             System.out.println("New jbigi.jar detected, moved jbigi library to " +
    1643                                                path + ".bak");
    1644                             System.out.println("Check logs for successful installation of new library");
    1645                         }
    1646                     }
    1647                 }
    1648             }
    1649     }
    1650    
    1651     /**
    1652      *  Delete all files listed in the delete file.
    1653      *  Format: One file name per line, comment lines start with '#'.
    1654      *  All file names must be relative to $I2P, absolute file names not allowed.
    1655      *  We probably can't remove old jars this way.
    1656      *  Fails silently.
    1657      *  Use no new I2P classes here so it may be called after zip extraction.
    1658      *  @since 0.8.12
    1659      */
    1660     private void deleteListedFiles() {
    1661         File deleteFile = new File(_context.getBaseDir(), DELETE_FILE);
    1662         if (!deleteFile.exists())
    1663             return;
    1664         // this is similar to FileUtil.readTextFile() but we can't use any I2P classes here
    1665         FileInputStream fis = null;
    1666         BufferedReader in = null;
    1667         try {
    1668             fis = new FileInputStream(deleteFile);
    1669             in = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
    1670             String line;
    1671             while ( (line = in.readLine()) != null) {
    1672                 String fl = line.trim();
    1673                 if (fl.contains("..") || fl.startsWith("#") || fl.length() == 0)
    1674                     continue;
    1675                 File df = new File(fl);
    1676                 if (df.isAbsolute())
    1677                     continue;
    1678                 df = new File(_context.getBaseDir(), fl);
    1679                 if (df.exists() && !df.isDirectory()) {
    1680                     if (df.delete())
    1681                         System.out.println("INFO: File [" + fl + "] deleted");
    1682                 }
    1683             }
    1684         } catch (IOException ioe) {}
    1685         finally {
    1686             if (in != null) try { in.close(); } catch(IOException ioe) {}
    1687             if (deleteFile.delete())
    1688                 System.out.println("INFO: File [" + DELETE_FILE + "] deleted");
    16891496        }
    16901497    }
  • router/java/src/net/i2p/router/tasks/package.html

    r6aa1284 r4705f01  
    55Jobs, Threads, and SimpleTimer.TimedEvents.
    66These are used only by Router.java.
     7Nothing here is to be used externally, not a part of the public API.
    78</p>
    89</body>
Note: See TracChangeset for help on using the changeset viewer.