Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
FileSupport.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.IO;
24 
25 namespace Lucene.Net.Support
26 {
27  /// <summary>
28  /// Represents the methods to support some operations over files.
29  /// </summary>
30  public class FileSupport
31  {
32  /// <summary>
33  /// Returns an array of abstract pathnames representing the files and directories of the specified path.
34  /// </summary>
35  /// <param name="path">The abstract pathname to list it childs.</param>
36  /// <returns>An array of abstract pathnames childs of the path specified or null if the path is not a directory</returns>
37  public static System.IO.FileInfo[] GetFiles(System.IO.FileInfo path)
38  {
39  if ((path.Attributes & FileAttributes.Directory) > 0)
40  {
41  String[] fullpathnames = Directory.GetFileSystemEntries(path.FullName);
42  System.IO.FileInfo[] result = new System.IO.FileInfo[fullpathnames.Length];
43  for (int i = 0; i < result.Length; i++)
44  result[i] = new System.IO.FileInfo(fullpathnames[i]);
45  return result;
46  }
47  else
48  return null;
49  }
50 
51  // TODO: This filesupport thing is silly. Same goes with _TestUtil's RMDir.
52  // If we're removing a directory
53  public static System.IO.FileInfo[] GetFiles(System.IO.DirectoryInfo path)
54  {
55  return GetFiles(new FileInfo(path.FullName));
56  }
57 
58  /// <summary>
59  /// Returns a list of files in a give directory.
60  /// </summary>
61  /// <param name="fullName">The full path name to the directory.</param>
62  /// <param name="indexFileNameFilter"></param>
63  /// <returns>An array containing the files.</returns>
64  public static System.String[] GetLuceneIndexFiles(System.String fullName,
65  Index.IndexFileNameFilter indexFileNameFilter)
66  {
67  System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(fullName);
68  System.Collections.ArrayList list = new System.Collections.ArrayList();
69  foreach (System.IO.FileInfo fInfo in dInfo.GetFiles())
70  {
71  if (indexFileNameFilter.Accept(fInfo, fInfo.Name) == true)
72  {
73  list.Add(fInfo.Name);
74  }
75  }
76  System.String[] retFiles = new System.String[list.Count];
77  list.CopyTo(retFiles);
78  return retFiles;
79  }
80 
81  // Disable the obsolete warning since we must use FileStream.Handle
82  // because Mono does not support FileSystem.SafeFileHandle at present.
83 #pragma warning disable 618
84 
85  /// <summary>
86  /// Flushes the specified file stream. Ensures that all buffered
87  /// data is actually written to the file system.
88  /// </summary>
89  /// <param name="fileStream">The file stream.</param>
90  public static void Sync(System.IO.FileStream fileStream)
91  {
92  if (fileStream == null)
93  throw new ArgumentNullException("fileStream");
94 
95  fileStream.Flush();
96 
97  //if (OS.IsWindows)
98  //{
99  // if (!FlushFileBuffers(fileStream.Handle))
100  // throw new System.IO.IOException();
101  //}
102  //else if (OS.IsUnix)
103  //{
104  // if (fsync(fileStream.Handle) != IntPtr.Zero)
105  // throw new System.IO.IOException();
106  //}
107  //else
108  //{
109  // throw new NotImplementedException();
110  //}
111  }
112 
113 #pragma warning restore 618
114 
115  //[System.Runtime.InteropServices.DllImport("libc")]
116  //extern static IntPtr fsync(IntPtr fd);
117 
118  //[System.Runtime.InteropServices.DllImport("kernel32.dll")]
119  //extern static bool FlushFileBuffers(IntPtr hFile);
120  }
121 }