Example #1
0
        /// <summary>
        /// Opens a new storage stream for writing.
        /// Throws an exception after n failed attempts.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="source"></param>
        /// <returns>the open stream on success</returns>
        public static Stream OpenWrite(string name, StorageSource source)
        {
            if ((source & StorageSource.TitleSpace) != 0)
            {
#if !Xbox
                System.Windows.Forms.MessageBox.Show(
                    "\nsource = " + source.ToString()
                    + "\nPathBase = " + PathBase(source)
                    + "\n name = " + name,
                    "OpenWrite failure, trying to write to title space.",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Asterisk);
#endif

                throw new Exception("Storage may not write to title space.");
            }

            dirty = true;

            int attempts = 0;

            Stream stream = null;

            while (stream == null)
            {
                try
                {
                    CreateDirectory(name, source);

                    stream = new FileStream(
                        Combine(PathBase(source), name),
                        FileMode.Create,
                        FileAccess.Write,
                        FileShare.None);
                }
                catch
                {
                    if (attempts >= kFileOperationRetryCount)
                    {
                        throw;
                    }
                    Thread.Sleep(kFileOperationRetryWaitMs);
                    ++attempts;
                }
            }

            if (stream == null)
            {
#if !Xbox
                System.Windows.Forms.MessageBox.Show(
                    "\nsource = " + source.ToString()
                    + "\nPathBase = " + PathBase(source)
                    + "\n name = " + name,
                    "OpenWrite failure.",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Asterisk);
#endif
            }

            return(stream);
        }   // end of OpenWrite()
Example #2
0
        /// <summary>
        /// Open a stream for read access.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sources"></param>
        /// <param name="wantRetry"></param>
        /// <returns></returns>
        public static Stream OpenRead(string name, StorageSource sources, bool wantRetry)
        {
#if OPEN_READ_DEBUG
            // Hack to try and debug file open errors.
            try
            {
                string     p  = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\OpenRead.txt";
                TextWriter tw = new StreamWriter(p, false);

                // Write a line of text to the file
                tw.WriteLine("filename : " + name);
                tw.WriteLine("sources : " + sources.ToString());

                StorageSource src = CheckSources(sources);

                tw.WriteLine("checked sources : " + src.ToString());

                bool local = 0 != (src & StorageSource.LocalSpace);
                tw.WriteLine("local : " + local.ToString());
                if (local)
                {
                    string path = Combine(PathBase(StorageSource.LocalSpace), name);
                    tw.WriteLine("    path : " + path.ToString());

                    bool exists = File.Exists(path);
                    tw.WriteLine("    exists : " + exists.ToString());

                    if (exists)
                    {
                        tw.WriteLine("        opening");
                        Stream str = null;
                        try
                        {
                            str = File.OpenRead(path);
                        }
                        catch (Exception e)
                        {
                            tw.WriteLine("        fail : ", e.Message);
                            tw.WriteLine("             : ", e.InnerException.Message);
                        }
                        finally
                        {
                            if (str != null)
                            {
                                str.Close();
                                tw.WriteLine("        closing");
                            }
                        }
                    }
                }

                bool user = 0 != (src & StorageSource.UserSpace);
                tw.WriteLine("user : "******"    path : " + path.ToString());

                    bool exists = File.Exists(path);
                    tw.WriteLine("    exists : " + exists.ToString());

                    if (exists)
                    {
                        tw.WriteLine("        opening");
                        Stream str = null;
                        try
                        {
                            str = File.OpenRead(path);
                        }
                        catch (Exception e)
                        {
                            tw.WriteLine("        fail : ", e.Message);
                            tw.WriteLine("             : ", e.InnerException.Message);
                        }
                        finally
                        {
                            if (str != null)
                            {
                                str.Close();
                                tw.WriteLine("        closing");
                            }
                        }
                    }
                }

                bool title = 0 != (src & StorageSource.TitleSpace);
                tw.WriteLine("title : " + title.ToString());
                if (title)
                {
                    string path = Combine(PathBase(StorageSource.TitleSpace), name);
                    tw.WriteLine("    path : " + path.ToString());

                    bool exists = File.Exists(path);
                    tw.WriteLine("    exists : " + exists.ToString());

                    if (exists)
                    {
                        tw.WriteLine("        opening");
                        Stream str = null;
                        try
                        {
                            str = File.OpenRead(path);
                        }
                        catch (Exception e)
                        {
                            tw.WriteLine("        fail : ", e.Message);
                            tw.WriteLine("             : ", e.InnerException.Message);
                        }
                        finally
                        {
                            if (str != null)
                            {
                                str.Close();
                                tw.WriteLine("        closing");
                            }
                        }
                    }
                }

                tw.WriteLine("");

                tw.Close();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message + "\n" + e.InnerException.Message);
            }
#endif



            Stream stream = null;

            int attempts = 0;
            while (true)
            {
                if (Initialized)
                {
                    sources = CheckSources(sources);
                    /// Try first on local space
                    if (0 != (sources & StorageSource.LocalSpace))
                    {
                        string path = Combine(PathBase(StorageSource.LocalSpace), name);

                        if (File.Exists(path))
                        {
                            stream = File.OpenRead(path);
                        }
                    }
                    /// If not there, try user space.
                    if ((stream == null) &&
                        (0 != (sources & StorageSource.UserSpace)))
                    {
                        string path = Combine(PathBase(StorageSource.UserSpace), name);

                        if (File.Exists(path))
                        {
                            stream = File.OpenRead(path);
                        }
                    }
                }
                // This next bit is the last chance to get it. If
                // we fail, we want to generate the exception, so no
                // try/catch wrapper here.
                if (stream == null && 0 != (sources & StorageSource.TitleSpace))
                {
                    string path = Combine(PathBase(StorageSource.TitleSpace), name);

                    if (File.Exists(path))
                    {
#if !PREBOOT && !ADDIN
                        // This file exists in title space, so log the filename.
                        BokuGame.LogContentFileLoaded(path);
#endif
                        stream = File.OpenRead(path);
                    }
                }

                if (stream != null)
                {
                    break;
                }

                if (!wantRetry)
                {
                    break;
                }

                if (attempts >= kFileOperationRetryCount)
                {
                    break;
                }
                Thread.Sleep(kFileOperationRetryWaitMs);
                attempts += 1;
            }

            if (stream == null)
            {
#if XBOX
                // Do nothing?
#else
                throw new FileNotFoundException(String.Format("File not found in {0}: {1}", sources, name));
#endif
            }

            return(stream);
        }