private void testReadOnlyFiles(boolean setReadOnly) throws Exception { new File(System.getProperty("java.io.tmpdir")).mkdirs(); File f = File.createTempFile("test", "temp"); assertTrue(f.canWrite()); f.setReadOnly(); assertTrue(!f.canWrite()); f.delete(); f = File.createTempFile("test", "temp"); RandomAccessFile r = new RandomAccessFile(f, "rw"); r.write(1); f.setReadOnly(); r.close(); assertTrue(!f.canWrite()); f.delete(); deleteDb("readonlyFiles"); Connection conn = getConnection("readonlyFiles"); Statement stat = conn.createStatement(); stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); stat.execute("INSERT INTO TEST VALUES(1, 'Hello')"); stat.execute("INSERT INTO TEST VALUES(2, 'World')"); assertTrue(!conn.isReadOnly()); conn.close(); if (setReadOnly) { setReadOnly(); conn = getConnection("readonlyFiles"); } else { conn = getConnection("readonlyFiles;ACCESS_MODE_DATA=r"); } assertTrue(conn.isReadOnly()); stat = conn.createStatement(); stat.execute("SELECT * FROM TEST"); assertThrows(ErrorCode.DATABASE_IS_READ_ONLY, stat).execute("DELETE FROM TEST"); conn.close(); if (setReadOnly) { conn = getConnection("readonlyFiles;DB_CLOSE_DELAY=1"); } else { conn = getConnection("readonlyFiles;DB_CLOSE_DELAY=1;ACCESS_MODE_DATA=r"); } stat = conn.createStatement(); stat.execute("SELECT * FROM TEST"); assertThrows(ErrorCode.DATABASE_IS_READ_ONLY, stat).execute("DELETE FROM TEST"); stat.execute("SET DB_CLOSE_DELAY=0"); conn.close(); }
/** * Create bitarchive with access denied to location of admin data verify that exceptions are * thrown */ @Test public void testAccessDenied() { // Make sure archive exists assertTrue("Inaccessible archive dir must exist", NOACCESS_ARCHIVE_DIR.exists()); if (NOACCESS_ARCHIVE_DIR.canWrite()) { NOACCESS_ARCHIVE_DIR.setReadOnly(); } // and that admin file is inaccessible assertFalse( "Must not be able to write to inaccessible admin file", NOACCESS_ARCHIVE_DIR.canWrite()); try { Settings.set( ArchiveSettings.BITARCHIVE_SERVER_FILEDIR, NOACCESS_ARCHIVE_DIR.getAbsolutePath()); Bitarchive ba = Bitarchive.getInstance(); ba.close(); fail("Accessing read-only archive should throw exception"); // do not come here } catch (PermissionDenied e) { // Expected case StringAsserts.assertStringContains( "Should mention noaccess dir", "noaccess/filedir", e.getMessage()); } }
@Test public void testReadOnlyTempFolderWithoutLog() { if (!_shouldTest()) { return; } List<LogRecord> logRecords = JDKLoggerTestUtil.configureJDKLogger(FIFOUtil.class.getName(), Level.OFF); File tempFolder = new File("tempFolder"); tempFolder.mkdirs(); tempFolder.setReadOnly(); String oldTempFolder = System.getProperty("java.io.tmpdir"); System.setProperty("java.io.tmpdir", tempFolder.getAbsolutePath()); try { Assert.assertFalse(FIFOUtil.isFIFOSupported()); } finally { System.setProperty("java.io.tmpdir", oldTempFolder); } Assert.assertTrue(logRecords.isEmpty()); }
public static void main(String[] args) { // create new file try { File file = new File("src/IOTasks/output/test.txt"); if (file.createNewFile()) { System.out.println("File is created"); } else { System.out.println("File already exisits"); } } catch (IOException e) { e.printStackTrace(); } // construct filepath try { String filename = "file.txt"; String workingDirectory = System.getProperty("user.dir"); // String absoluteFilePath = workingDirectory + File.separator + filename; // File file1 = new File(absoluteFilePath); File file1 = new File(workingDirectory, filename); file1.createNewFile(); System.out.println("Final pathname : " + file1.getAbsolutePath()); file1.setExecutable(false); file1.setReadOnly(); if (file1.exists()) { System.out.println("executable : " + file1.canExecute()); System.out.println("writable : " + file1.canWrite()); } file1.setWritable(true); File file2 = new File("src/IOTasks/input/file.jpg"); long lastModifiedTime = file2.lastModified(); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY hh:mm:ss"); System.out.println("Last modified : " + sdf.format(lastModifiedTime)); String newLastModified = "31/01/2015 10:15:21"; // convert to miliseconds in long Date newDate = sdf.parse(newLastModified); file2.setLastModified(newDate.getTime()); long size = file2.length(); System.out.println(size / 1024 + " kB"); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } // get file size }
private void makeFilesReadOnly() { // Make the files in the old team data directory read-only. // This will cause the WBS to open in read-only mode. // It will also prevent individuals from exporting their data // until they update their team project location. for (File f : oldTeamDataDir.listFiles()) f.setReadOnly(); }
private void compress(final File src) { final File dir = src.getParentFile(); final File dst = new File(dir, src.getName() + ".gz"); final File tmp = new File(dir, ".tmp." + src.getName()); try { final InputStream in = new FileInputStream(src); try { OutputStream out = new GZIPOutputStream(new FileOutputStream(tmp)); try { final byte[] buf = new byte[2048]; int n; while (0 < (n = in.read(buf))) { out.write(buf, 0, n); } } finally { out.close(); } tmp.setReadOnly(); } finally { in.close(); } if (!tmp.renameTo(dst)) { throw new IOException("Cannot rename " + tmp + " to " + dst); } src.delete(); } catch (IOException e) { log.error("Cannot compress " + src, e); tmp.delete(); } }
protected static void write(byte[] data, File dest) throws IOException { File tempDest = tempDest(dest); FileOutputStream fos = new FileOutputStream(tempDest); fos.write(data); fos.close(); tempDest.setReadOnly(); tempDest.renameTo(dest); }
private void setReadOnly(final File directory) { if (directory == null) return; for (File file : directory.listFiles()) { if (file.isDirectory()) { setReadOnly(file); } else if (file.getName().endsWith(".log.db")) file.delete(); else file.setReadOnly(); } }
@Override public void iconPicked(int requestCode, int resultCode, Intent in) { Drawable ic = null; String iconType = null; String pkgName = null; String iconSource = null; if (requestCode == IconPicker.REQUEST_PICK_GALLERY) { if (resultCode == Activity.RESULT_OK) { File mImage = new File( mActivity.getFilesDir() + "/lockscreen_" + System.currentTimeMillis() + ".png"); if (mImageTmp.exists()) { mImageTmp.renameTo(mImage); } mImage.setReadOnly(); iconType = GlowPadView.ICON_FILE; iconSource = mImage.toString(); ic = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(mImage.toString())); } else { if (mImageTmp.exists()) { mImageTmp.delete(); } return; } } else if (requestCode == IconPicker.REQUEST_PICK_SYSTEM) { String resourceName = in.getStringExtra(IconPicker.RESOURCE_NAME); ic = mResources .getDrawable(mResources.getIdentifier(resourceName, "drawable", "android")) .mutate(); iconType = GlowPadView.ICON_RESOURCE; iconSource = resourceName; } else if (requestCode == IconPicker.REQUEST_PICK_ICON_PACK && resultCode == Activity.RESULT_OK) { String resourceName = in.getStringExtra(IconPicker.RESOURCE_NAME); pkgName = in.getStringExtra(IconPicker.PACKAGE_NAME); try { Context rContext = mActivity.createPackageContext(pkgName, 0); int id = rContext.getResources().getIdentifier(resourceName, "drawable", pkgName); ic = rContext.getResources().getDrawable(id); } catch (NameNotFoundException e) { e.printStackTrace(); } iconType = GlowPadView.ICON_RESOURCE; iconSource = resourceName; } else { return; } TargetInfo tmpIcon = new TargetInfo(null); tmpIcon.iconType = iconType; tmpIcon.iconSource = iconSource; tmpIcon.pkgName = pkgName; mDialogIcon.setTag(tmpIcon); mDialogIcon.setImageDrawable(ic); }
/** * Pack all reachable objects in the repository into a single pack file. * * <p>All loose objects are automatically pruned. Existing packs however are not removed. * * @throws Exception */ public void packAndPrune() throws Exception { if (db.getObjectDatabase() instanceof ObjectDirectory) { ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase(); NullProgressMonitor m = NullProgressMonitor.INSTANCE; final File pack, idx; PackWriter pw = new PackWriter(db); try { Set<ObjectId> all = new HashSet<ObjectId>(); for (Ref r : db.getAllRefs().values()) all.add(r.getObjectId()); pw.preparePack(m, all, Collections.<ObjectId>emptySet()); final ObjectId name = pw.computeName(); OutputStream out; pack = nameFor(odb, name, ".pack"); out = new SafeBufferedOutputStream(new FileOutputStream(pack)); try { pw.writePack(m, m, out); } finally { out.close(); } pack.setReadOnly(); idx = nameFor(odb, name, ".idx"); out = new SafeBufferedOutputStream(new FileOutputStream(idx)); try { pw.writeIndex(out); } finally { out.close(); } idx.setReadOnly(); } finally { pw.release(); } odb.openPack(pack, idx); updateServerInfo(); prunePacked(odb); } }
/** Test to check that a DN goes down when all its volumes have failed. */ @Test public void testShutdown() throws Exception { if (System.getProperty("os.name").startsWith("Windows")) { /** * This test depends on OS not allowing file creations on a directory that does not have write * permissions for the user. Apparently it is not the case on Windows (at least under Cygwin), * and possibly AIX. This is disabled on Windows. */ return; } // Bring up two more datanodes cluster.startDataNodes(conf, 2, true, null, null); cluster.waitActive(); final int dnIndex = 0; String bpid = cluster.getNamesystem().getBlockPoolId(); File storageDir = cluster.getInstanceStorageDir(dnIndex, 0); File dir1 = MiniDFSCluster.getRbwDir(storageDir, bpid); storageDir = cluster.getInstanceStorageDir(dnIndex, 1); File dir2 = MiniDFSCluster.getRbwDir(storageDir, bpid); try { // make the data directory of the first datanode to be readonly assertTrue("Couldn't chmod local vol", dir1.setReadOnly()); assertTrue("Couldn't chmod local vol", dir2.setReadOnly()); // create files and make sure that first datanode will be down DataNode dn = cluster.getDataNodes().get(dnIndex); for (int i = 0; dn.isDatanodeUp(); i++) { Path fileName = new Path("/test.txt" + i); DFSTestUtil.createFile(fs, fileName, 1024, (short) 2, 1L); DFSTestUtil.waitReplication(fs, fileName, (short) 2); fs.delete(fileName, true); } } finally { // restore its old permission FileUtil.setWritable(dir1, true); FileUtil.setWritable(dir2, true); } }
public static void main(String[] args) throws Exception { File d = new File(System.getProperty("test.dir", ".")); File f = new File(d, "x.SetReadOnly"); if (f.exists()) { if (!f.delete()) throw new Exception("Can't delete test file " + f); } if (f.setReadOnly()) throw new Exception("Succeeded on non-existent file: " + f); OutputStream o = new FileOutputStream(f); o.write('x'); o.close(); if (!f.setReadOnly()) throw new Exception(f + ": Failed on file"); if (f.canWrite()) throw new Exception(f + ": File is writeable"); f = new File(d, "x.SetReadOnly.dir"); if (f.exists()) { if (!f.delete()) throw new Exception("Can't delete test directory " + f); } if (!f.mkdir()) throw new Exception(f + ": Cannot create directory"); if (f.setReadOnly() && f.canWrite()) throw new Exception(f + ": Directory is writeable"); if (!f.delete()) throw new Exception(f + ": Cannot delete directory"); }
public void testSetPasswordFileWithReadOnlyFile() { File testFile = createPasswordFile(0, 0); testFile.setReadOnly(); try { _database.setPasswordFile(testFile.toString()); } catch (FileNotFoundException fnfe) { assertTrue(fnfe.getMessage().startsWith("Cannot read password file ")); } catch (IOException e) { fail("Password File was not created." + e.getMessage()); } testFile.delete(); }
protected static void copy(File src, File dest) throws IOException { File tempDest = tempDest(dest); FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(tempDest); byte[] buffer = new byte[65536]; int r; while ((r = fis.read(buffer)) > 0) { fos.write(buffer, 0, r); } fis.close(); fos.close(); tempDest.setLastModified(src.lastModified()); tempDest.setReadOnly(); tempDest.renameTo(dest); }
public static void main(String[] args) throws Exception { File file = new File("ReadOnly.txt"); // Create a file only if it doesn't exist. file.createNewFile(); // Set file attribute to read only so that it cannot be written file.setReadOnly(); // We are using the canWrite() method to check whether we can // modified file content. if (file.canWrite()) { System.out.println("File is writable!"); } else { System.out.println("File is in read only mode!"); } }
public static void main(String[] args) { if (args.length != 1) { System.out.println("USAGE : java FileMerge filename"); System.exit(0); // 프로그램을 종료한다. } String mergeFilename = args[0]; try { File tempFile = File.createTempFile("~mergetemp", ".tmp"); tempFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempFile); BufferedOutputStream bos = new BufferedOutputStream(fos); FileInputStream fis = null; BufferedInputStream bis = null; int number = 1; File f = new File(mergeFilename + "_." + number); while (f.exists()) { f.setReadOnly(); // 작업중에 파일의 내용이 변경되지 않도록 한다. bis = new BufferedInputStream(new FileInputStream(f)); int data = 0; while ((data = bis.read()) != -1) { bos.write(data); } bis.close(); f = new File(mergeFilename + "_." + ++number); } // while bos.close(); File oldFile = new File(mergeFilename); if (oldFile.exists()) oldFile.delete(); tempFile.renameTo(oldFile); } catch (IOException e) { } } // main
@Override public void setAttributes(URI uri, Map<String, ?> attributes, Map<?, ?> options) throws IOException { String filePath = uri.toFileString(); File file = new File(filePath); if (file.exists()) { Long timeStamp = (Long) attributes.get(IURIConverter.ATTRIBUTE_TIME_STAMP); if (timeStamp != null && !file.setLastModified(timeStamp)) { throw new IOException("Could not set the timestamp for the file '" + file + "'"); } Boolean isReadOnly = (Boolean) attributes.get(IURIConverter.ATTRIBUTE_READ_ONLY); if (Boolean.TRUE.equals(isReadOnly) && !file.setReadOnly()) { throw new IOException("Could not set the file '" + file + "' to be read only"); } } else { throw new FileNotFoundException("The file '" + file + "' does not exist"); } }
public void testCopyToNotWritableDirectory() throws IOException { URL resourceURL = Platform.getBundle(BUNDLE_ID).getEntry(RESOURCE_DIR); File resourceFolder = ResourceUtil.resourcePathToFile(resourceURL); File source = new File(resourceFolder, TEST_DIR); File dest = new File(tempDir, "testdir"); try { dest.mkdir(); dest.setReadOnly(); IOUtil.copyDirectory(source, new File(dest, "testdir2")); assertDirectory(source, dest); fail("Expected directories to not match"); } catch (AssertionError ae) { // expected } finally { FileUtil.deleteRecursively(dest); } }
static void saveAuIdProperties(String location, Properties props) { // XXX these AU_ID_FILE entries need to be backed up elsewhere to avoid // single-point corruption File propDir = new File(location); if (!propDir.exists()) { logger.debug("Creating directory '" + propDir.getAbsolutePath() + "'"); propDir.mkdirs(); } File propFile = new File(propDir, AU_ID_FILE); try { logger.debug3("Saving au id properties at '" + location + "'."); OutputStream os = new BufferedOutputStream(new FileOutputStream(propFile)); props.store(os, "ArchivalUnit id info"); os.close(); propFile.setReadOnly(); } catch (IOException ioe) { logger.error("Couldn't write properties for " + propFile.getPath() + ".", ioe); throw new LockssRepository.RepositoryStateException("Couldn't write au id properties file."); } }
/** * Copy the <i>source</i> file to <i>target</i>. If <i>target</i> does not exist, it is assumed to * be the name of the copied file. If <i>target</i> is a directory, the file will be copied into * that directory. * * @param progress if non-null, this progress monitor is updated with the name of each file as it * is copied. * @param source source directory * @param target target file/directory * @param saveSuffix if non-null and <i>target</i> exists, <i>target</i> will be renamed to * <tt>name + saveSuffix</tt>. * @return false if any problems were encountered. */ public static final boolean copyFile( ProgressMonitor progress, File source, File target, String saveSuffix) { // don't copy directories if (source.isDirectory()) { return false; } // if source and target are the same, we're done if (getPath(source).equals(getPath(target))) { return false; } if (target.isDirectory()) { target = new File(target, source.getName()); } FileInputStream in; try { in = new FileInputStream(source); } catch (IOException ioe) { System.err.println("Couldn't open source file " + source); return false; } copyStreamToFile(progress, in, target, saveSuffix); try { in.close(); } catch (Exception e) {; } // if source was read-only, the target should be as well if (!source.canWrite()) { target.setReadOnly(); } // sync up last-modified time target.setLastModified(source.lastModified()); return true; }
/** * Copy files under the <i>source</i> directory to the <i>target</i> directory. If necessary, * <i>target</i> directory is created.<br> * <br> * For example, if this method is called with a <i>source</i> of <tt>/foo</tt> (which contains * <tt>/foo/a</tt> and <tt>/foo/b</tt>) and a <i>target</i> of <tt>/bar</tt>, when this method * exits <tt>/bar</tt> will contain <tt>/bar/a</tt> and <tt>/bar/b</tt>. Note that <tt>foo</tt> * itself is not copied. * * @param progress if non-null, this progress monitor is updated with the name of each file as it * is copied. * @param source source directory * @param target directory * @param saveSuffix if non-null, pre-existing files under <i>target</i> whose paths match files * to be copied from <i>source</i> will be renamed to <tt>name + saveSuffix</tt>. * @return false if any problems were encountered. */ public static final boolean copyDirectory( ProgressMonitor progress, File source, File target, String saveSuffix) { // source must be a directory if (!source.isDirectory() || (target.exists() && !target.isDirectory())) { return false; } // if source and target are the same, we're done if (getPath(source).equals(getPath(target))) { return false; } // if the target doesn't exist yet, create it if (!target.exists()) { target.mkdirs(); } boolean result = true; String[] list = source.list(); for (int i = 0; i < list.length; i++) { File srcFile = new File(source, list[i]); File tgtFile = new File(target, list[i]); if (srcFile.isDirectory()) { result |= copyDirectory(progress, srcFile, tgtFile, saveSuffix); } else { result |= copyFile(progress, srcFile, tgtFile, saveSuffix); } } // if source was read-only, the target should be as well if (!source.canWrite()) { target.setReadOnly(); } // sync up last-modified time target.setLastModified(source.lastModified()); return result; }
@Test public void testReadOnlyTempFolderWithLog() { if (!_shouldTest()) { return; } List<LogRecord> logRecords = JDKLoggerTestUtil.configureJDKLogger(FIFOUtil.class.getName(), Level.WARNING); File tempFolder = new File("tempFolder"); tempFolder.mkdirs(); tempFolder.setReadOnly(); String oldTempFolder = System.getProperty("java.io.tmpdir"); System.setProperty("java.io.tmpdir", tempFolder.getAbsolutePath()); try { Assert.assertFalse(FIFOUtil.isFIFOSupported()); } finally { System.setProperty("java.io.tmpdir", oldTempFolder); } Assert.assertEquals(1, logRecords.size()); LogRecord logRecord = logRecords.get(0); Assert.assertEquals("Unable to detect FIFO support", logRecord.getMessage()); Throwable throwable = logRecord.getThrown(); Assert.assertEquals(Exception.class, throwable.getClass()); String message = throwable.getMessage(); Assert.assertTrue( message.startsWith( "Unable to create FIFO with command \"mkfifo\", external " + "process returned ")); }
public static void main(String[] args) throws Exception { String osName = System.getProperty("os.name"); if (osName.startsWith("Windows")) return; Preferences root = Preferences.userRoot(); Preferences node1 = root.node("node1"); Preferences node1A = node1.node("node1A"); Preferences node1B = node1.node("node1B"); node1B.put("mykey", "myvalue"); node1.flush(); String node1BDirName = System.getProperty("user.home") + "/.java/.userPrefs" + "/node1/node1B"; File node1BDir = new File(node1BDirName); node1BDir.setReadOnly(); try { node1.removeNode(); } catch (BackingStoreException ex) { // expected exception } finally { Runtime.getRuntime().exec("chmod 755 " + node1BDirName).waitFor(); try { node1.removeNode(); } catch (Exception e) { } } }
/* * Verify the number of blocks and files are correct after volume failure, * and that we can replicate to both datanodes even after a single volume * failure if the configuration parameter allows this. */ @Test public void testVolumeFailure() throws Exception { System.out.println("Data dir: is " + dataDir.getPath()); // Data dir structure is dataDir/data[1-4]/[current,tmp...] // data1,2 is for datanode 1, data2,3 - datanode2 String filename = "/test.txt"; Path filePath = new Path(filename); // we use only small number of blocks to avoid creating subdirs in the data dir.. int filesize = block_size * blocks_num; DFSTestUtil.createFile(fs, filePath, filesize, repl, 1L); DFSTestUtil.waitReplication(fs, filePath, repl); System.out.println("file " + filename + "(size " + filesize + ") is created and replicated"); // fail the volume // delete/make non-writable one of the directories (failed volume) data_fail = new File(dataDir, "data3"); failedDir = MiniDFSCluster.getFinalizedDir(dataDir, cluster.getNamesystem().getBlockPoolId()); if (failedDir.exists() && // !FileUtil.fullyDelete(failedDir) !deteteBlocks(failedDir)) { throw new IOException("Could not delete hdfs directory '" + failedDir + "'"); } data_fail.setReadOnly(); failedDir.setReadOnly(); System.out.println("Deleteing " + failedDir.getPath() + "; exist=" + failedDir.exists()); // access all the blocks on the "failed" DataNode, // we need to make sure that the "failed" volume is being accessed - // and that will cause failure, blocks removal, "emergency" block report triggerFailure(filename, filesize); // make sure a block report is sent DataNode dn = cluster.getDataNodes().get(1); // corresponds to dir data3 String bpid = cluster.getNamesystem().getBlockPoolId(); DatanodeRegistration dnR = dn.getDNRegistrationForBP(bpid); Map<DatanodeStorage, BlockListAsLongs> perVolumeBlockLists = dn.getFSDataset().getBlockReports(bpid); // Send block report StorageBlockReport[] reports = new StorageBlockReport[perVolumeBlockLists.size()]; int reportIndex = 0; for (Map.Entry<DatanodeStorage, BlockListAsLongs> kvPair : perVolumeBlockLists.entrySet()) { DatanodeStorage dnStorage = kvPair.getKey(); BlockListAsLongs blockList = kvPair.getValue(); reports[reportIndex++] = new StorageBlockReport(dnStorage, blockList); } cluster.getNameNodeRpc().blockReport(dnR, bpid, reports, null); // verify number of blocks and files... verify(filename, filesize); // create another file (with one volume failed). System.out.println("creating file test1.txt"); Path fileName1 = new Path("/test1.txt"); DFSTestUtil.createFile(fs, fileName1, filesize, repl, 1L); // should be able to replicate to both nodes (2 DN, repl=2) DFSTestUtil.waitReplication(fs, fileName1, repl); System.out.println("file " + fileName1.getName() + " is created and replicated"); }
public boolean setReadOnly() { return file.setReadOnly(); }
public static void main(String as[]) { try { System.out.println("FIle.pathSeparator\t -: " + File.pathSeparator); System.out.println("FIle.pathSeparatorChar\t -: " + File.pathSeparatorChar); System.out.println("FIle.separator\t -: " + File.separator); System.out.println("FIle.separatorChar\t -: " + File.separatorChar); /* File[] far=File.listRoots(); System.out.println(far.length); for(int i=0;i<far.length;i++){ System.out.println(far[i].getName()+"\t"+far[i].isDirectory()+"\t"+far[i].isFile()+"\t"+far[i].isAbsolute()+"\t"+far[i].getPath()+"\t"+far[i].getAbsolutePath()); */ File f1 = new File("c:\\", "Anshu.txt"); f1.createNewFile(); File f2 = new File("Pra"); f2.mkdir(); File f3 = new File(f2, "om\\io"); f3.mkdirs(); File f4 = new File(f3, "san.txt"); System.out.println("f4.createNewFile() -: " + f4.createNewFile()); System.out.println("f4.getName() -: " + f4.getName()); System.out.println("f4.isFile() -: " + f4.isFile()); System.out.println("f4.length() -: " + f4.length()); System.out.println("f4.isHidden() -: " + f4.isHidden()); System.out.println("f4.getPath() -: " + f4.getPath()); System.out.println("f4.getCanonicalPath() -: " + f4.getCanonicalPath()); System.out.println("f4.getAbsolutePath() -: " + f4.getAbsolutePath()); System.out.println("f4.exists() -: " + f4.exists()); System.out.println("f4.getParent() -: " + f4.getParent()); System.out.println("f4.canRead() -: " + f4.canRead()); System.out.println("f4.canWrite() -: " + f4.canWrite()); f4.setReadOnly(); System.out.println("f4.canRead() -: " + f4.canRead()); System.out.println("f4.canWrite() -: " + f4.canWrite()); File f5 = new File("Hello.txt"); System.out.println("f5.createNewFile() -: " + f5.createNewFile()); System.out.println("f5.getPath() -: " + f5.getPath()); System.out.println("f5.getCanonicalPath() -: " + f5.getCanonicalPath()); System.out.println("f5.getAbsolutePath() -: " + f5.getAbsolutePath()); System.out.println("f5.getParent() -: " + f5.getParent()); File f6 = new File(f3, "Hello1.txt"); System.out.println("f6.createNewFile() -: " + f6.createNewFile()); File f7 = new File(f3, "Hello2.txt"); System.out.println("f7.createNewFile() -: " + f7.createNewFile()); File f8 = new File(f3, "Hello3.txt"); System.out.println("f8.createNewFile() -: " + f8.createNewFile()); File f9 = new File(f3, "Hello4.txt"); System.out.println("f9.createNewFile() -: " + f9.createNewFile()); File f10 = new File(f3, "Hello5.txt"); System.out.println("f10.createNewFile() -: " + f10.createNewFile()); String fnl[] = f3.list(); for (int i = 0; i < fnl.length; i++) { System.out.print(fnl[i] + ","); } } catch (Exception e) { e.printStackTrace(); } }
private void makeSiteConfig(SitePaths site, Config cfg, SshInfo sshInfo) throws IOException { if (!Files.exists(site.tmp_dir)) { Files.createDirectories(site.tmp_dir); } Path myconf = Files.createTempFile(site.tmp_dir, "gitweb_config", ".perl"); // To make our configuration file only readable or writable by us; // this reduces the chances of someone tampering with the file. // // TODO(dborowitz): Is there a portable way to do this with NIO? File myconfFile = myconf.toFile(); myconfFile.setWritable(false, false /* all */); myconfFile.setReadable(false, false /* all */); myconfFile.setExecutable(false, false /* all */); myconfFile.setWritable(true, true /* owner only */); myconfFile.setReadable(true, true /* owner only */); myconfFile.deleteOnExit(); _env.set("GIT_DIR", "."); _env.set("GITWEB_CONFIG", myconf.toAbsolutePath().toString()); try (PrintWriter p = new PrintWriter(Files.newBufferedWriter(myconf, UTF_8))) { p.print("# Autogenerated by Gerrit Code Review \n"); p.print("# DO NOT EDIT\n"); p.print("\n"); // We are mounted at the same level in the context as the main // UI, so we can include the same header and footer scheme. // Path hdr = site.site_header; if (Files.isRegularFile(hdr)) { p.print("$site_header = " + quoteForPerl(hdr) + ";\n"); } Path ftr = site.site_footer; if (Files.isRegularFile(ftr)) { p.print("$site_footer = " + quoteForPerl(ftr) + ";\n"); } // Top level should return to Gerrit's UI. // p.print("$home_link = $ENV{'GERRIT_CONTEXT_PATH'};\n"); p.print("$home_link_str = 'Code Review';\n"); p.print("$favicon = 'favicon.ico';\n"); p.print("$logo = 'gitweb-logo.png';\n"); p.print("$javascript = 'gitweb.js';\n"); p.print("@stylesheets = ('gitweb-default.css');\n"); Path css = site.site_css; if (Files.isRegularFile(css)) { p.print("push @stylesheets, 'gitweb-site.css';\n"); } // Try to make the title match Gerrit's normal window title // scheme of host followed by 'Code Review'. // p.print("$site_name = $home_link_str;\n"); p.print("$site_name = qq{$1 $site_name} if "); p.print("$ENV{'SERVER_NAME'} =~ m,^([^.]+(?:\\.[^.]+)?)(?:\\.|$),;\n"); // Assume by default that XSS is a problem, and try to prevent it. // p.print("$prevent_xss = 1;\n"); // Generate URLs using smart http:// // p.print("{\n"); p.print(" my $secure = $ENV{'HTTPS'} =~ /^ON$/i;\n"); p.print(" my $http_url = $secure ? 'https://' : 'http://';\n"); p.print(" $http_url .= qq{$ENV{'GERRIT_USER_NAME'}@}\n"); p.print(" unless $ENV{'GERRIT_ANONYMOUS_READ'};\n"); p.print(" $http_url .= $ENV{'SERVER_NAME'};\n"); p.print(" $http_url .= qq{:$ENV{'SERVER_PORT'}}\n"); p.print(" if (( $secure && $ENV{'SERVER_PORT'} != 443)\n"); p.print(" || (!$secure && $ENV{'SERVER_PORT'} != 80)\n"); p.print(" );\n"); p.print(" $http_url .= qq{$ENV{'GERRIT_CONTEXT_PATH'}p};\n"); p.print(" push @git_base_url_list, $http_url;\n"); p.print("}\n"); // Generate URLs using anonymous git:// // String url = cfg.getString("gerrit", null, "canonicalGitUrl"); if (url != null) { if (url.endsWith("/")) { url = url.substring(0, url.length() - 1); } p.print("if ($ENV{'GERRIT_ANONYMOUS_READ'}) {\n"); p.print(" push @git_base_url_list, "); p.print(quoteForPerl(url)); p.print(";\n"); p.print("}\n"); } // Generate URLs using authenticated ssh:// // if (sshInfo != null && !sshInfo.getHostKeys().isEmpty()) { String sshAddr = sshInfo.getHostKeys().get(0).getHost(); p.print("if ($ENV{'GERRIT_USER_NAME'}) {\n"); p.print(" push @git_base_url_list, join('', 'ssh://'"); p.print(", $ENV{'GERRIT_USER_NAME'}"); p.print(", '@'"); if (sshAddr.startsWith("*:") || "".equals(sshAddr)) { p.print(", $ENV{'SERVER_NAME'}"); } if (sshAddr.startsWith("*")) { sshAddr = sshAddr.substring(1); } p.print(", " + quoteForPerl(sshAddr)); p.print(");\n"); p.print("}\n"); } // Link back to Gerrit (when possible, to matching review record). // Supported gitweb's hash values are: // - (missing), // - HEAD, // - refs/heads/<branch>, // - refs/changes/*/<change>/*, // - <revision>. // p.print("sub add_review_link {\n"); p.print(" my $h = shift;\n"); p.print(" my $q;\n"); p.print(" if (!$h || $h eq 'HEAD') {\n"); p.print(" $q = qq{#q,project:$ENV{'GERRIT_PROJECT_NAME'}};\n"); p.print(" } elsif ($h =~ /^refs\\/heads\\/([-\\w]+)$/) {\n"); p.print(" $q = qq{#q,project:$ENV{'GERRIT_PROJECT_NAME'}"); p.print("+branch:$1};\n"); // wrapped p.print(" } elsif ($h =~ /^refs\\/changes\\/\\d{2}\\/(\\d+)\\/\\d+$/) "); p.print("{\n"); // wrapped p.print(" $q = qq{#/c/$1};\n"); p.print(" } else {\n"); p.print(" $q = qq{#/q/$h};\n"); p.print(" }\n"); p.print(" my $r = qq{$ENV{'GERRIT_CONTEXT_PATH'}$q};\n"); p.print(" push @{$feature{'actions'}{'default'}},\n"); p.print(" ('review',$r,'commitdiff');\n"); p.print("}\n"); p.print("if ($cgi->param('hb')) {\n"); p.print(" add_review_link($cgi->param('hb'));\n"); p.print("} elsif ($cgi->param('h')) {\n"); p.print(" add_review_link($cgi->param('h'));\n"); p.print("} else {\n"); p.print(" add_review_link();\n"); p.print("}\n"); // If the administrator has created a site-specific gitweb_config, // load that before we perform any final overrides. // Path sitecfg = site.site_gitweb; if (Files.isRegularFile(sitecfg)) { p.print("$GITWEB_CONFIG = " + quoteForPerl(sitecfg) + ";\n"); p.print("if (-e $GITWEB_CONFIG) {\n"); p.print(" do " + quoteForPerl(sitecfg) + ";\n"); p.print("}\n"); } Path root = repoManager.getBasePath(); p.print("$projectroot = " + quoteForPerl(root) + ";\n"); // Permit exporting only the project we were started for. // We use the name under $projectroot in case symlinks // were involved in the path. // p.print("$export_auth_hook = sub {\n"); p.print(" my $dir = shift;\n"); p.print(" my $name = $ENV{'GERRIT_PROJECT_NAME'};\n"); p.print(" my $allow = qq{$projectroot/$name.git};\n"); p.print(" return $dir eq $allow;\n"); p.print(" };\n"); // Do not allow the administrator to enable path info, its // not a URL format we currently support. // p.print("$feature{'pathinfo'}{'override'} = 0;\n"); p.print("$feature{'pathinfo'}{'default'} = [0];\n"); // We don't do forking, so don't allow it to be enabled. // p.print("$feature{'forks'}{'override'} = 0;\n"); p.print("$feature{'forks'}{'default'} = [0];\n"); } myconfFile.setReadOnly(); }
@Override public void setReadOnly(File file) { file.setReadOnly(); }