public virtual void TestFailToRename()
        {
            Assume.AssumeTrue(Shell.Windows);
            OutputStream fos = null;

            try
            {
                fos = new AtomicFileOutputStream(DstFile);
                fos.Write(Sharpen.Runtime.GetBytesForString(TestString));
                FileUtil.SetWritable(TestDir, false);
                exception.Expect(typeof(IOException));
                exception.ExpectMessage("failure in native rename");
                try
                {
                    fos.Close();
                }
                finally
                {
                    fos = null;
                }
            }
            finally
            {
                IOUtils.Cleanup(null, fos);
                FileUtil.SetWritable(TestDir, true);
            }
        }
Esempio n. 2
0
        /// <summary>Atomically write the given value to the given file, including fsyncing.</summary>
        /// <param name="file">destination file</param>
        /// <param name="val">value to write</param>
        /// <exception cref="System.IO.IOException">if the file cannot be written</exception>
        public static void WriteFile(FilePath file, long val)
        {
            AtomicFileOutputStream fos = new AtomicFileOutputStream(file);

            try
            {
                fos.Write(Sharpen.Runtime.GetBytesForString(val.ToString(), Charsets.Utf8));
                fos.Write('\n');
                fos.Close();
                fos = null;
            }
            finally
            {
                if (fos != null)
                {
                    fos.Abort();
                }
            }
        }
Esempio n. 3
0
        /// <exception cref="System.IO.IOException"/>
        private static void SaveMD5File(FilePath dataFile, string digestString)
        {
            FilePath md5File            = GetDigestFileForFile(dataFile);
            string   md5Line            = digestString + " *" + dataFile.GetName() + "\n";
            AtomicFileOutputStream afos = new AtomicFileOutputStream(md5File);

            afos.Write(Sharpen.Runtime.GetBytesForString(md5Line, Charsets.Utf8));
            afos.Close();
            if (Log.IsDebugEnabled())
            {
                Log.Debug("Saved MD5 " + digestString + " to " + md5File);
            }
        }
        public virtual void TestWriteNewFile()
        {
            OutputStream fos = new AtomicFileOutputStream(DstFile);

            NUnit.Framework.Assert.IsFalse(DstFile.Exists());
            fos.Write(Sharpen.Runtime.GetBytesForString(TestString));
            fos.Flush();
            NUnit.Framework.Assert.IsFalse(DstFile.Exists());
            fos.Close();
            NUnit.Framework.Assert.IsTrue(DstFile.Exists());
            string readBackData = DFSTestUtil.ReadFile(DstFile);

            NUnit.Framework.Assert.AreEqual(TestString, readBackData);
        }
        public virtual void TestOverwriteFile()
        {
            NUnit.Framework.Assert.IsTrue("Creating empty dst file", DstFile.CreateNewFile());
            OutputStream fos = new AtomicFileOutputStream(DstFile);

            NUnit.Framework.Assert.IsTrue("Empty file still exists", DstFile.Exists());
            fos.Write(Sharpen.Runtime.GetBytesForString(TestString));
            fos.Flush();
            // Original contents still in place
            NUnit.Framework.Assert.AreEqual(string.Empty, DFSTestUtil.ReadFile(DstFile));
            fos.Close();
            // New contents replace original file
            string readBackData = DFSTestUtil.ReadFile(DstFile);

            NUnit.Framework.Assert.AreEqual(TestString, readBackData);
        }