Exemplo n.º 1
0
        bool DoSave()
        {
            if (IsReadOnly)
            {
                SetStatus("Cannot save read-only");
                return(false);
            }

            string DocText = Doc.Text;

            {
                try
                {
                    string fn = PrettyFile;
                    if (fn.StartsWith("dfs://", StringComparison.OrdinalIgnoreCase))
                    {
                        fn = fn.Substring(6);
                    }
                    dfs      dc     = Surrogate.ReadMasterDfsConfig();
                    string[] slaves = dc.Slaves.SlaveList.Split(';');
                    if (0 == slaves.Length || dc.Slaves.SlaveList.Length == 0)
                    {
                        throw new Exception("DFS SlaveList error (machines)");
                    }
                    Random rnd = new Random(DateTime.Now.Millisecond / 2 + System.Diagnostics.Process.GetCurrentProcess().Id / 2);
                    string newactualfilehost = slaves[rnd.Next() % slaves.Length];
                    string newactualfilename = dfs.GenerateZdFileDataNodeName(fn);
                    string myActualFile      = Surrogate.NetworkPathForHost(newactualfilehost) + @"\" + newactualfilename;
                    {
                        byte[] smallbuf = new byte[4];
                        MySpace.DataMining.DistributedObjects.Entry.ToBytes(4, smallbuf, 0);
                        using (System.IO.FileStream fs = new System.IO.FileStream(myActualFile, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
                        {
                            fs.Write(smallbuf, 0, 4);
                            byte[] buf = Encoding.UTF8.GetBytes(Doc.Text);
                            fs.Write(buf, 0, buf.Length);
                        }
                        if (IsNewFile)
                        {
                            Console.Write(MySpace.DataMining.DistributedObjects.Exec.Shell(
                                              "DSpace -dfsbind \"" + newactualfilehost + "\" \"" + newactualfilename + "\" \"" + fn + "\" " + DfsFileTypes.NORMAL
                                              + " -h4"));
                        }
                        else
                        {
                            string tempdfsfile = fn + Guid.NewGuid().ToString() + dfs.TEMP_FILE_MARKER;
                            Console.Write(MySpace.DataMining.DistributedObjects.Exec.Shell(
                                              "DSpace -dfsbind \"" + newactualfilehost + "\" \"" + newactualfilename + "\" \"" + tempdfsfile + "\" " + DfsFileTypes.NORMAL
                                              + " -h4"));
                            MySpace.DataMining.DistributedObjects.Exec.Shell("DSpace swap \"" + tempdfsfile + "\" \"" + fn + "\"");
                            MySpace.DataMining.DistributedObjects.Exec.Shell("DSpace delete \"" + tempdfsfile + "\"", true); // suppresserrors=true
                        }
                    }
                    //ActualFile = myActualFile; // Only update this when fully committed to DFS!
                    IsNewFile = false;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    MessageBox.Show(this, "Unable to save new file to DFS:\r\n\r\n" + e.Message, "Save-New Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false); // Important! don't continue with any of the rest if this fails!
                }
            }

            Doc.Modified = false; // !
            return(true);
        }