/********************************************************************************************* * Copyright 2004 - Volian Enterprises, Inc. All rights reserved. * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE * ------------------------------------------------------------------------------ * $Workfile: VETempFile.cs $ $Revision: 1 $ * $Author: Kathy $ $Date: 7/27/04 8:45a $ * * $History: VETempFile.cs $ * * ***************** Version 1 ***************** * User: Kathy Date: 7/27/04 Time: 8:45a * Created in $/LibSource/VENetwork *********************************************************************************************/ using System; using System.IO; using System.Windows.Forms; namespace VENetwork { // this class manages the temps directory for multi-user support in ve-proms. // The temps directory is used to handle a user's temporary files in support of // printing, etc. public class VETempFile { public string TemporaryDirectoryName; private string TemporaryFileName; public string TempNum; FileStream tmpFs; private string CurrentDir; public VETempFile(string cd) { CurrentDir = cd; tmpFs=null; } ~ VETempFile() { CloseTempProc(); } // Converts the userid, num & PorD (characters 'P' or 'D') into a file- // name to be used for temporary file support. This creates the file- // name. The userid is extended to 8 characters with '_'s. public string MakeTempName(string userid, string num, char PorD) { // first, make the temp directory, i.e. temps, if it doesn't // exist. DirectoryInfo di = new DirectoryInfo(CurrentDir + "temps"); if (!di.Exists) di.Create(); string tmpuid = userid.PadRight(8,'_'); string tpath = "temps" + "\\" + tmpuid + "." + PorD.ToString() + num; return tpath; } // This method truncates the temp file (user___.P##) and then closes it. public void CloseTempProc() { if(tmpFs!=null) { try { // if called from a destructor, the file may have been closed - reopen // it if we can't write if (tmpFs.CanWrite==false) { tmpFs=null; tmpFs = new FileStream(TemporaryFileName,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None); } tmpFs.Seek(0L,SeekOrigin.Begin); tmpFs.SetLength(0L); // truncate the file tmpFs.Close(); tmpFs=null; TemporaryDirectoryName=null; } catch (Exception e) { MessageBox.Show(e.Message,this.TemporaryDirectoryName); } } } // make all subdirectories, if not exist private void MakeAllNecessaryDirectories(string path) { if (Directory.Exists(CurrentDir + path)) return; // Create the directory, and any subdirectories DirectoryInfo di = Directory.CreateDirectory(CurrentDir + path); } // sets class variables based on input. private void SetUpTempDir(string userid, string num) { string TempPath = MakeTempName(userid,num,'D'); MakeAllNecessaryDirectories(TempPath); TemporaryDirectoryName = TempPath + "\\"; } // This method attempts to attach to the specified temp file. // Userid and num are combined to create a process name and // TempPathDirectory. If the file is successfully opened and // a connection is established, a byte count matching the process // number is written to the file and the file is held open. (This // byte count is actually written in the connection logic, // veconn.cs, after a connection is successfully made.) public bool AttachSpecificTemp(string userid, string num) { // if already open, close. CloseTempProc(); string pname = CurrentDir + MakeTempName(userid,num,'P'); // check to see if this user can use the file, i.e. open // exclusively. try { tmpFs = new FileStream(pname,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None); } catch { return false; } TemporaryFileName = pname; TempNum = num; SetUpTempDir(userid,num); return true; } // if len is !0, try to delete files of the specified length. // zero length files are normally in transition and will be // deleted by their owners. public void DeleteFiles(string tmp, int len) { DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory()); FileInfo [] fis = di.GetFiles(tmp); foreach (FileInfo fi in fis) { if (len==0 || len==fi.Length) fi.Delete(); } } // find the next available temp directory for a given userid. public bool FindAvailableTempDir(string userid) { // first look for existing temp files for this user to see if any // can be used. int i; string ptmp = MakeTempName(userid,"*",'P'); // create a template // check to see if any exist. DirectoryInfo di = new DirectoryInfo(CurrentDir); FileInfo [] fis = di.GetFiles(ptmp); for (i=0;i0) { // this was left from a reboot or something. // delete ownership and wrq files of the // specified size. Check to see if it is // consistent with the process file first. DeleteFiles("*.own",(int)fi.Length); DeleteFiles("*.wrq",(int)fi.Length); } return true; } } fis = null; // Couldn't use an existing one, find a new one. for (i=0;i<99;i++) if (AttachSpecificTemp(userid,i.ToString("d2"))) return true; // if the user got this far, something's wrong - report error & exit. MessageBox.Show("Too many temporary process files for user.","VE-PROMS error"); return false; } // this method writes out the requested number of bytes to the file. public void WriteBytes(int num) { if (tmpFs != null) { while (num>0) { tmpFs.WriteByte((byte)'x'); num--; } tmpFs.Flush(); } } } }