Class: Dir

Inherits:
Object
  • Object
show all
Extended by:
Functions
Includes:
Constants, Structs
Defined in:
lib/win32/dir.rb

Defined Under Namespace

Modules: Constants, Functions, Structs

Constant Summary collapse

VERSION =

The version of the win32-dir library.

'0.4.7'
MYDOCUMENTS =
PERSONAL

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*glob_patterns) ⇒ Object

Same as the standard MRI Dir[] method except that it handles backslashes in path names.



129
130
131
132
# File 'lib/win32/dir.rb', line 129

def [](*glob_patterns)
  temp = glob_patterns.map!{ |pattern| "#{pattern}".tr("\\", "/") }
  old_ref(*temp)
end

.create_junction(to, from) ⇒ Object

Creates the symlink to, linked to the existing directory from. If the to directory already exists, it must be empty or an error is raised.

Example:

Dir.mkdir('C:/from')
Dir.create_junction('C:/to', 'C:/from')


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/win32/dir.rb', line 194

def self.create_junction(to, from)
  to   = "#{to}".wincode
  from = "#{from}".wincode

  from_path = (0.chr * 1024).encode('UTF-16LE')

  length = GetFullPathNameW(from, from_path.size, from_path, nil)

  if length == 0 || length > from_path.size
    raise SystemCallError.new("GetFullPathNameW", FFI.errno)
  else
    from_path.strip!
  end

  to_path = (0.chr * 1024).encode('UTF-16LE')

  length = GetFullPathNameW(to, to_path.size, to_path, nil)

  if length == 0 || length > to_path.size
    raise SystemCallError.new("GetFullPathNameW", FFI.errno)
  else
    to_path.strip!
  end

  # You can create a junction to a directory that already exists, so
  # long as it's empty.
  unless CreateDirectoryW(to_path, nil)
    if FFI.errno != ERROR_ALREADY_EXISTS
      raise SystemCallError.new("CreateDirectoryW", FFI.errno)
    end
  end

  begin
    # Generic read & write + open existing + reparse point & backup semantics
    handle = CreateFileW(
      to_path,
      GENERIC_READ | GENERIC_WRITE,
      0,
      nil,
      OPEN_EXISTING,
      FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
      0
    )

    if handle == INVALID_HANDLE_VALUE
      raise SystemCallError.new("CreateFileW", FFI.errno)
    end

    target = "\\??\\".encode('UTF-16LE') + from_path

    rdb = REPARSE_JDATA_BUFFER.new
    rdb[:ReparseTag] = 2684354563 # IO_REPARSE_TAG_MOUNT_POINT
    rdb[:ReparseDataLength] = target.bytesize + 12
    rdb[:Reserved] = 0
    rdb[:SubstituteNameOffset] = 0
    rdb[:SubstituteNameLength] = target.bytesize
    rdb[:PrintNameOffset] = target.bytesize + 2
    rdb[:PrintNameLength] = 0
    rdb[:PathBuffer] = target

    bytes = FFI::MemoryPointer.new(:ulong)

    begin
      bool = DeviceIoControl(
        handle,
        CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, 0),
        rdb,
        rdb[:ReparseDataLength] + rdb.header_size,
        nil,
        0,
        bytes,
        nil
      )

      error = FFI.errno

      unless bool
        RemoveDirectoryW(to_path)
        raise SystemCallError.new("DeviceIoControl", error)
      end
    ensure
      CloseHandle(handle)
    end
  end

  self
end

.empty?(path) ⇒ Boolean

Returns whether or not path is empty. Returns false if path is not a directory, or contains any files other than ‘.’ or ‘..’.

Returns:

  • (Boolean)


372
373
374
# File 'lib/win32/dir.rb', line 372

def self.empty?(path)
  PathIsDirectoryEmptyW("#{path}".wincode)
end

.glob(glob_pattern, flags = 0, &block) ⇒ Object

Same as the standard MRI Dir.glob method except that it handles backslashes in path names.



114
115
116
117
118
119
120
121
122
# File 'lib/win32/dir.rb', line 114

def glob(glob_pattern, flags = 0, &block)
  if glob_pattern.is_a?(Array)
    temp = glob_pattern.map!{ |pattern| "#{pattern}".tr("\\", "/") }
  else
    temp = "#{glob_pattern}".tr("\\", "/")
  end

  old_glob(temp, flags, &block)
end

.junction?(path) ⇒ Boolean Also known as: reparse_dir?

Returns whether or not path is a junction.

Returns:

  • (Boolean)


378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/win32/dir.rb', line 378

def self.junction?(path)
  bool = true

  attrib = GetFileAttributesW("#{path}".wincode)

  # Only directories with a reparse point attribute can be junctions
  if attrib == INVALID_FILE_ATTRIBUTES ||
     attrib & FILE_ATTRIBUTE_DIRECTORY == 0 ||
     attrib & FILE_ATTRIBUTE_REPARSE_POINT == 0
  then
    bool = false
  end

  bool
end

.old_globObject



109
# File 'lib/win32/dir.rb', line 109

alias old_glob glob

.old_refObject



124
# File 'lib/win32/dir.rb', line 124

alias old_ref []

.read_junction(junction) ⇒ Object

Returns the path that a given junction points to. Raises an Errno::ENOENT error if the given path does not exist. Returns false if it is not a junction.

Example:

Dir.mkdir('C:/from')
Dir.create_junction('C:/to', 'C:/from')
Dir.read_junction("c:/to") # => "c:/from"


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/win32/dir.rb', line 292

def self.read_junction(junction)
  return false unless Dir.junction?("#{junction}")

  junction = "#{junction}".wincode

  junction_path = (0.chr * 1024).encode('UTF-16LE')

  length = GetFullPathNameW(junction, junction_path.size, junction_path, nil)

  if length == 0 || length > junction_path.size
    raise SystemCallError.new("GetFullPathNameW", FFI.errno)
  else
    junction_path.strip!
  end

  begin
    # Generic read & write + open existing + reparse point & backup semantics
    handle = CreateFileW(
      junction_path,
      GENERIC_READ | GENERIC_WRITE,
      0,
      nil,
      OPEN_EXISTING,
      FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
      0
    )

    if handle == INVALID_HANDLE_VALUE
      raise SystemCallError.new("CreateFileW", FFI.errno)
    end

    rdb = REPARSE_JDATA_BUFFER.new
    rdb[:ReparseTag] = 0
    rdb[:ReparseDataLength] = 0
    rdb[:Reserved] = 0
    rdb[:SubstituteNameOffset] = 0
    rdb[:SubstituteNameLength] = 0
    rdb[:PrintNameOffset] = 0
    rdb[:PrintNameLength] = 0
    rdb[:PathBuffer] = ''

    bytes = FFI::MemoryPointer.new(:ulong)

    begin
      bool = DeviceIoControl(
        handle,
        CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, 0),
        nil,
        0,
        rdb,
        1024,
        bytes,
        nil
      )

      error = FFI.errno

      unless bool
        raise SystemCallError.new("DeviceIoControl", error)
      end
    ensure
      CloseHandle(handle)
    end
  end

  # MSDN says print and substitute names can be in any order
  jname = (rdb[:PathBuffer].to_ptr + rdb[:SubstituteNameOffset]).read_string(rdb[:SubstituteNameLength])
  jname = jname.bytes.to_a.pack('C*')
  jname = jname.force_encoding("UTF-16LE")
  raise "Junction name came back as #{jname}" unless jname[0..3] == "\\??\\".encode("UTF-16LE")
  begin
    File.expand_path(jname[4..-1].encode(Encoding.default_external))
  rescue Encoding::UndefinedConversionError
    File.expand_path(jname[4..-1].encode('UTF-8'))
  end
end

Instance Method Details

#getwdObject Also known as: pwd

Returns the present working directory. Unlike MRI, this method always normalizes the path.

Examples:

Dir.chdir("C:/Progra~1")
Dir.getwd # => C:\Program Files

Dir.chdir("C:/PROGRAM FILES")
Dir.getwd # => C:\Program Files


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/win32/dir.rb', line 150

def getwd
  path1 = FFI::Buffer.new(:wint_t, 1024, true)
  path2 = FFI::Buffer.new(:wint_t, 1024, true)
  path3 = FFI::Buffer.new(:wint_t, 1024, true)

  length = GetCurrentDirectoryW(path1.size, path1)

  if length == 0 || length > path1.size
    raise SystemCallError.new("GetCurrentDirectoryW", FFI.errno)
  end

  length = GetShortPathNameW(path1, path2, path2.size)

  if length == 0 || length > path2.size
    raise SystemCallError.new("GetShortPathNameW", FFI.errno)
  end

  length = GetLongPathNameW(path2, path3, path3.size)

  if length == 0 || length > path3.size
    raise SystemCallError.new("GetLongPathNameW", FFI.errno)
  end

  path = path3.read_bytes(length * 2).wstrip

  begin
    path.encode(Encoding.default_external)
  rescue Encoding::UndefinedConversionError
    path.encode('UTF-8')
  end
end

#oldgetwdObject



136
# File 'lib/win32/dir.rb', line 136

alias oldgetwd getwd