Esempio n. 1
0
        /// <summary>
        /// Run a write action to a file.
        /// This will always rewrite the file (no appending).
        /// </summary>
        /// <param name="path">File path to write</param>
        /// <param name="action">Action to write into file stream. You do not need to close the stream yourself.</param>
        public static void Write(string path, Action <Stream> action)
        {
            lock (_lock)
            {
                // if the old copy file exists, this means that we have
                // a previous corrupt write, so we will not overrite it, but
                // rather overwrite the current file and keep it as our backup.
                if (File.Exists(path + ".old_copy") == false)
                {
                    File.Move(path, path + ".old_copy");
                }

                using (
                    var stream = new FileStream(path,
                                                FileMode.Create,
                                                FileAccess.Write,
                                                FileShare.None,
                                                0x10000,
                                                FileOptions.WriteThrough | FileOptions.SequentialScan)
                    )
                {
                    SetPermissions.TryAllowReadWriteForAll(path);
                    action(stream);
                    stream.Flush();
                }

                WaitDelete(path + ".old_copy");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Run a read action over a file by name.
        /// Access is optimised for sequential scanning.
        /// No file share is permitted.
        /// </summary>
        /// <param name="path">File path to read</param>
        /// <param name="action">Action to consume file stream. You do not need to close the stream yourself.</param>
        public static void Read(string path, Action <Stream> action)
        {
            lock (_lock)
            {
                if (File.Exists(path + ".old_copy"))
                {
                    if (WaitDelete(path))
                    {
                        File.Move(path + ".old_copy", path);
                    }
                }

                using (
                    var stream = new FileStream(path,
                                                FileMode.OpenOrCreate,
                                                FileAccess.Read,
                                                FileShare.None,
                                                0x10000,
                                                FileOptions.SequentialScan)
                    )
                {
                    SetPermissions.TryAllowReadWriteForAll(path);
                    action(stream);
                }
            }
        }
Esempio n. 3
0
        private FileStream CreateWriter()
        {
            var dataFilePath = GetDataPath(CurrentFileNumber);
            var stream       = new FileStream(
                dataFilePath,
                FileMode.OpenOrCreate,
                FileAccess.Write,
                FileShare.ReadWrite,
                0x10000,
                FileOptions.Asynchronous | FileOptions.SequentialScan | FileOptions.WriteThrough);

            SetPermissions.TryAllowReadWriteForAll(dataFilePath);
            return(stream);
        }
Esempio n. 4
0
 void CreateDirectory(string s)
 {
     Directory.CreateDirectory(s);
     SetPermissions.TryAllowReadWriteForAll(s);
 }
Esempio n. 5
0
        public Set(JsonValue json, IJsonContext context) : this()
        {
            var dict = json as JsonDictionary;

            if (dict == null)
            {
                throw new JsonConvertException("Expected a JSON dictionary to be converted to a SetInfo");
            }

            if (!dict.Items.ContainsKey("id"))
            {
                throw new JsonConvertException("Expected SetInfo JSON to contain an 'id' property");
            }

            foreach (var k in dict.Items)
            {
                switch (k.Key)
                {
                case "id": ID = context.FromJson <long>(k.Value); break;

                case "url": Uri = context.FromJson <string>(k.Value); break;

                case "title": Title = context.FromJson <string>(k.Value); break;

                case "created_by": Author = context.FromJson <string>(k.Value); break;

                case "description": Description = context.FromJson <string>(k.Value); break;

                case "term_count": TermCount = context.FromJson <int>(k.Value); break;

                case "created_date": Created = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(k.Value)); break;

                case "modified_date": Modified = new DateTime(1970, 1, 1).AddSeconds(context.FromJson <long>(k.Value)); break;

                case "subjects": AddSubjects(context.FromJson <List <string> >(k.Value)); break;

                case "visibility": Visibility = SetPermissions.ParseVisibility(context.FromJson <string>(k.Value)); break;

                case "editable": Editable = SetPermissions.ParseEditPermissions(context.FromJson <string>(k.Value)); break;

                case "has_access": HasAccess = context.FromJson <bool>(k.Value); break;

                case "has_discussion": HasDiscussion = context.FromJson <bool>(k.Value); break;

                case "lang_terms": TermLanguageCode = context.FromJson <string>(k.Value); break;

                case "lang_definitions": DefinitionLanguageCode = context.FromJson <string>(k.Value); break;

                case "terms":
                    var list = new List <Term>();
                    if (k.Value is JsonArray)
                    {
                        foreach (var termJson in ((JsonArray)k.Value).Items)
                        {
                            if (!(termJson is JsonDictionary))
                            {
                                throw new JsonConvertException("Expected SetInfo.Terms to be an array of JSON dictionaries");
                            }
                            var term = new Term {
                                ID         = context.FromJson <long>(((JsonDictionary)termJson).Items["id"]),
                                TermValue  = context.FromJson <string>(((JsonDictionary)termJson).Items["term"]),
                                Definition = context.FromJson <string>(((JsonDictionary)termJson).Items["definition"])
                            };
                            if (term.TermValue == null || term.Definition == null)
                            {
                                throw new JsonConvertException("Either term or definition was not group when converting from JSON to Set");
                            }
                            list.Add(term);
                        }
                    }
                    else
                    {
                        throw new JsonConvertException("Expected SetInfo.Terms to be an array");
                    }
                    AddTerms(list);
                    break;
                }
            }

            // TODO: Validate that important fields are defined
        }
        public async Task <IActionResult> SetPermissionsAsync([FromBody] SetPermissions command)
        {
            await DispatchAsync(command);

            return(NoContent());
        }