fileutils_ln_r.patch

Zachary Scott, 11/19/2012 07:09 AM

Download (4.72 KB)

View differences:

lib/fileutils.rb
317 317
  OPT_TABLE['link'] = [:force, :noop, :verbose]
318 318

  
319 319
  #
320
  # Options: noop verbose dereference_root remove_destination
321
  #
322
  # Hard link +src+ to +dest+. If +src+ is a directory, this method links
323
  # all its contents recursively. If +dest+ is a directory, links
324
  # +src+ to +dest/src+.
325
  #
326
  # +src+ can be a list of files.
327
  #
328
  #   # Installing ruby library "mylib" under the site_ruby
329
  #   FileUtils.rm_r site_ruby + '/mylib', :force
330
  #   FileUtils.ln_r 'lib/', site_ruby + '/mylib'
331
  #
332
  #   # Examples of copying several files to target directory.
333
  #   FileUtils.ln_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
334
  #   FileUtils.ln_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop => true, :verbose => true
335
  #
336
  #   # If you want to copy all contents of a directory instead of the
337
  #   # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
338
  #   # use following code.
339
  #   FileUtils.ln_r 'src/.', 'dest'     # cp_r('src', 'dest') makes src/dest,
340
  #                                      # but this doesn't.
341
  #
342
  # TODO: Why --remove-destination and not --force?
343
  def ln_r(src, dest, options = {})
344
    fu_check_options options, OPT_TABLE['ln_r']
345
    fu_output_message "cp -r#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
346
    return if options[:noop]
347
    options = options.dup
348
    options[:dereference_root] = true unless options.key?(:dereference_root)
349
    fu_each_src_dest(src, dest) do |s, d|
350
      link_entry s, d, options[:dereference_root], options[:remove_destination]
351
    end
352
  end
353
  module_function :ln_r
354

  
355
  OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination]
356

  
357
  #
320 358
  # Options: force noop verbose
321 359
  #
322 360
  # <b><tt>ln_s(old, new, options = {})</tt></b>
......
372 410
  OPT_TABLE['ln_sf'] = [:noop, :verbose]
373 411

  
374 412
  #
413
  # Hard links a file system entry +src+ to +dest+.
414
  # If +src+ is a directory, this method links its contents recursively.
415
  #
416
  # Both of +src+ and +dest+ must be a path name.
417
  # +src+ must exist, +dest+ must not exist.
418
  #
419
  # If +dereference_root+ is true, this method dereference tree root.
420
  #
421
  # If +remove_destination+ is true, this method removes each destination file before copy.
422
  #
423
  def link_entry(src, dest, dereference_root = false, remove_destination = false)
424
    Entry_.new(src, nil, dereference_root).traverse do |ent|
425
      destent = Entry_.new(dest, ent.rel, false)
426
      File.unlink destent.path if remove_destination && File.file?(destent.path)
427
      ent.link destent.path
428
    end
429
  end
430
  module_function :link_entry
431

  
432
  #
375 433
  # Options: preserve noop verbose
376 434
  #
377 435
  # Copies a file content +src+ to +dest+.  If +dest+ is a directory,
......
1235 1293
      end
1236 1294
    end
1237 1295

  
1296
    def link(dest)
1297
      case
1298
      when directory?
1299
        if !File.exist?(dest) and descendant_diretory?(dest, path)
1300
          raise ArgumentError, "cannot link directory %s to itself %s" % [path, dest]
1301
        end
1302
        begin
1303
          Dir.mkdir dest
1304
        rescue
1305
          raise unless File.directory?(dest)
1306
        end
1307
      else
1308
        File.link path(), dest
1309
      end
1310
    end
1311

  
1238 1312
    def copy(dest)
1239 1313
      case
1240 1314
      when file?
1241
- 
lib/fileutils.rb
342 342
  # TODO: Why --remove-destination and not --force?
343 343
  def ln_r(src, dest, options = {})
344 344
    fu_check_options options, OPT_TABLE['ln_r']
345
    fu_output_message "cp -r#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
345
    fu_output_message "ln -r#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
346 346
    return if options[:noop]
347 347
    options = options.dup
348 348
    options[:dereference_root] = true unless options.key?(:dereference_root)
349
-