Module: Cfruby::FileOps

Defined in:
lib/libcfruby/fileops.rb

Defined Under Namespace

Modules: SymlinkHandler Classes: FileCommand, FileOpsError, FileOpsFileExistError, FileOpsOverwriteError, FileOpsSystemCommandError, FileOpsUnknownProtocolError, FileOpsWrongFiletypeError, HTTPFileCommand, LocalFileCommand, RsyncFileCommand

Constant Summary collapse

@@backup =

Class variable to control the behavior of FileOps.backup globally

true

Class Method Summary collapse

Class Method Details

.backup(filename, options = {}) ⇒ Object

Creates a backup copy of filename with the new filename filename_cfruby_yyyymmdd_x, where x increments as more backups are added to the same directory. Options:

:backupdir

directory to hold the backups (defaults to the same directory as filename)

:onlyonchange

prevent backup from making a backup if viable backup already exists.



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/libcfruby/fileops.rb', line 541

def FileOps.backup(filename, options={})
	if !@@backup
		return
	end
  # @@@	
    # print "Backup ",filename,"\n"
	Cfruby.controller.attempt("backup #{filename}", 'destructive') {
		if(!filename.respond_to?(:dirname))
			filename = Pathname.new(filename.to_s())
		end
		
		# set the backup directory if it wasn't passed in
		backupdir = options[:backupdir]
		if(backupdir == nil)
			backupdir = filename.dirname()
		end
		
      fn = backupdir.to_s+'/'+filename.basename().to_s
      # p [options,backupdir.to_s,fn]
      # print "ls #{fn}:\n"
      # print `ls -lrt #{fn}*`
      timedfn = fn+"_#{Time.now.strftime('%Y%m%d')}"
      search = timedfn+"_*"
      # print "Search ",search,"\n"
		# find the latest backup file and test the current file against it
		# if :onlyonchange is true
		if(options[:onlyonchange])
			backupfiles = Dir.glob(search)
			if(backupfiles.length > 0)
				lastbackup = backupfiles.sort.reverse()[0]
				currentchecksums = Cfruby::Checksum::Checksum.get_checksums(filename)
				lastbackupchecksums = Cfruby::Checksum::Checksum.get_checksums(lastbackup)
				if(currentchecksums.sha1 == lastbackupchecksums.sha1)
					Cfruby.controller.attempt_abort("viable backup already exists \"#{lastbackup}\"")
				end
			end
		end
		
		tries = 3
		numbermatch = /_[0-9]{8}_([0-9]+)$/
		begin
			nextnum = -1
			
			# loop through any existing backup files to get the next number
			Dir.glob(search) { |backupfile|
          # p ['found',backupfile]
				match = numbermatch.match(backupfile)
				if(match != nil)
            number = match[1].to_i
            # p ['MATCH!',backupfile,number]
					if(number > nextnum)
						nextnum = number
					end
				end
			}
			nextnum = nextnum + 1
			bufn = timedfn+"_#{nextnum}"
        # print "Backup to #{bufn}\n"
			raise(Exception, "Unable to create backup copy of #{filename} - trying #{bufn}") if File.exist?(bufn)

			# attempt to open it
			success = false
			begin
				File.open(bufn, File::RDONLY)
			rescue Exception
				FileOps.copy(filename,bufn)
				success = true
			end
			
			if(false == success)
				raise(Exception, "Unable to create backup copy of #{filename} - tried #{bufn}")
			end
		rescue Exception
			# we play this game three times just to try to handle possible race
			# conditions between the choice of filename and the opening of the file
			tries = tries - 1
			if(tries < 0)
				raise($!)
			end
		end
	}
end

.chmod(basedir, permissions, options = {}) ⇒ Object

Chmod’s matching files. Returns true if a change was made, false otherwise.



838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/libcfruby/fileops.rb', line 838

def FileOps.chmod(basedir, permissions, options = {})
  changemade = false
  
	Cfruby::FileFind.find(basedir, options) { |filename|
		attemptmessage = "changing permissions of \"#{filename}\" to \""
		if(permissions.kind_of?(Numeric))
			attemptmessage = attemptmessage + sprintf("%o\"", permissions)
		else
			attemptmessage = attemptmessage + "#{permissions}\""
		end
		Cfruby.controller.attempt(attemptmessage, 'destructive') {
			currentmode = File.stat(filename).mode()
			# try it with internal functions, but try to call chmod if we have to
			if(permissions.kind_of?(Numeric))
				FileUtils.chmod(permissions, filename)
			else
				output = Cfruby::Exec.exec("chmod '" + permissions.to_s.gsub(/'/, "\\\&") + "' '" + filename.realpath.to_s.gsub(/'/, "\\\&") + "'")
				if(output[1].length > 0)
					raise(FileOpsError, output.join("\n"))
				end
			end
			
			if(currentmode == File.stat(filename).mode())
				Cfruby.controller.attempt_abort("unchanged, already set to \"#{permissions}\"")
			else
			  changemade = true
			end
		}
	}
	
	return(changemade)
end

.chown(basedir, owner, group = nil, options = {}) ⇒ Object

was made, false otherwise.



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/libcfruby/fileops.rb', line 809

def FileOps.chown(basedir, owner, group=nil, options = {})
  changemade = false
	usermanager = Cfruby::OS::OSFactory.new.get_os.get_user_manager()
	if(owner and !owner.kind_of?(Integer))
		owner = usermanager.get_uid(owner)
	end
	if(group and !group.kind_of?(Integer))
		group = usermanager.get_gid(group)
	end

	Cfruby::FileFind.find(basedir, options) { |filename|
		Cfruby.controller.attempt("changing ownership of \"#{filename}\" to \"#{owner}:#{group}\"", 'destructive') {
			currentuid = File.stat(filename).uid
			currentgid = File.stat(filename).gid
        owner = currentuid if !owner
        group = currentgid if !group
			filename.chown(owner, group)
			if(currentuid == File.stat(filename).uid and currentgid == File.stat(filename).gid)
				Cfruby.controller.attempt_abort("unchanged, already owned by \"#{owner}:#{group}\"")
			end
			changemade = true
		}
	}
	
	return(changemade)
end

.chown_mod(basedir, owner, group, mode, options = {}) ⇒ Object

Changes the owner, group, and mode all at once. Returns true if a change was made to owner, group, or mode - false otherwise. If mode==nil it is ignored.



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/libcfruby/fileops.rb', line 666

def FileOps.chown_mod(basedir, owner, group, mode, options = {})
  changemade = false
	Cfruby.controller.attempt("changing ownership and mode of matching files in \"#{basedir}\"", 'destructive') {
		usermanager = Cfruby::OS::OSFactory.new.get_os.get_user_manager()
		if(owner and !owner.kind_of?(Integer))
			owner = usermanager.get_uid(owner)
		end
		if(group and !group.kind_of?(Integer))
			group = usermanager.get_gid(group)
		end
      Cfruby::FileFind.find(basedir, options) { |filename|
        if(FileOps.chown(filename, owner, group))
          changemade = true
        end
        if(mode!=nil and FileOps.chmod(filename, mode))
          changemade = true
        end
      }
	}
	
	return(changemade)
end

.copy(filename, newfilename, options = {}) ⇒ Object

Copies filename to newfilename. Options may be set to one or more of the following:

:??????

anything defined under the protocol specific copy function



330
331
332
# File 'lib/libcfruby/fileops.rb', line 330

def FileOps.copy(filename, newfilename, options = {})
	get_protocol(filename, newfilename).copy(strip_protocol(filename), strip_protocol(newfilename), options)
end

.create(filenames, owner = Process::Sys.geteuid(), group = Process::Sys.getegid(), mode = 0600) ⇒ Object

Creates an empty file filenames if the file does not already exist. filenames may be an Array or String. If the file does exist, the mode and ownership may be adjusted. Returns true if a file was created, false otherwise.



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/libcfruby/fileops.rb', line 468

def FileOps.create(filenames, owner = Process::Sys.geteuid(), group = Process::Sys.getegid(), mode = 0600)
	if(filenames.kind_of?(String))
		filenames = Array.[](filenames)
	end

	created = false
	filenames.each() { |filename|
		Cfruby.controller.attempt("create #{filename}", 'destructive') {
			currentumask = File.umask()
			begin
				if(!test(?f, filename))
					# set a umask that disables all access to the file by default
					File.umask(0777)
					File.open(filename, File::CREAT|File::WRONLY) { |fp| 
					}
					created = true
				end
				chmod = FileOps.chmod(filename, mode)
				chown = FileOps.chown(filename, owner, group)
				if(chmod == false and chown == false)
				  Cfruby.controller.attempt_abort("\"#{filename}\" exists and has the appropriate owner, group, and mode")
				else
				  created = true
			  end
			ensure
				# restore the umask
				File.umask(currentumask)
			end
		}
	}
	
	return(created)
end

.delete(basedir, options = {}) ⇒ Object

Deletes matching files. Returns true if a file is actually deleted, false otherwise. In addition to the normal find options delete also takes:

:force

> (true|false) delete non-empty matching directories



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/libcfruby/fileops.rb', line 642

def FileOps.delete(basedir, options = {})
	deletedsomething = false
	Cfruby.controller.attempt("deleting files from \"#{basedir}\"", 'nonreversible', 'destructive') {
		begin
			options[:returnorder] = 'delete'
			Cfruby::FileFind.find(basedir, options) { |filename|
				if(!filename.symlink?() and filename.directory?())
					FileOps.rmdir(filename, options[:force])
				else
					FileOps::SymlinkHandler.unlink(filename)
				end
				deletedsomething = true
			}
		rescue Cfruby::FileFind::FileExistError
			Cfruby.controller.attempt_abort("#{basedir} does not exist")
		end
	}
	
	return(deletedsomething)
end

.delete_nonalpha(basedir, options = {}) ⇒ Object

Deletes files that contain no alphanumeric characters. Returns true if any files were deleted false otherwise



627
628
629
630
631
632
633
634
635
636
# File 'lib/libcfruby/fileops.rb', line 627

def FileOps.delete_nonalpha(basedir, options = {})
  deleted = false
	Cfruby.controller.attempt("deleting files non-alpha files from \"#{basedir}\"", 'nonreversible', 'destructive') {
		if(FileOps.delete_not_matching_regex(basedir, /[a-zA-Z0-9]/))
		  deleted = true
	  end
	}
	
	return(deleted)
end

.disable(basedir, options = {}) ⇒ Object

Disables matching files by setting all permissions to 0000. Returns true if anything was disabled, false otherwise.



793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/libcfruby/fileops.rb', line 793

def FileOps.disable(basedir, options = {})
  disabled = false
	Cfruby.controller.attempt("disabling file in \"#{basedir}\"", 'destructive') {
		Cfruby::FileFind.find(basedir, options) { |filename|
			if(Cfruby::FileOps.chmod(filename, 0000))
			  disabled = true
		  end
		}
	}
	
	return(disabled)
end

.flock(fn, attr = nil, ext = '.cflock') ⇒ Object

Lock a file fn, using a lockfile, and return a file handle to fn. attr are standard file open attributes like ‘w’. File based locking is used to correctly handle mounted NFS and SMB shares.



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/libcfruby/fileops.rb', line 506

def FileOps.flock(fn, attr=nil, ext='.cflock')
	Cfruby.controller.attempt("lock #{fn}") {
		begin
			fnlock = fn+ext
			if File.exist? fnlock
				Cfruby.controller.inform("warn", "File #{fn} is locked by #{fnlock} (remove to fix) - skipping!")
			end

			Cfruby.controller.inform('debug', "locking #{fnlock}")
			fl = File.open fnlock,'w'
			fl.print "pid=#{Process.pid}\nCfruby lock file"
			fl.close
			f = File.open fn, attr
			
			# ---- Update file
			yield f
		ensure
			Cfruby.controller.inform('debug', "unlock #{fnlock}")
			File.unlink fnlock if fl
			f.close if f
		end
	}
end

.get_protocol(filename, newfilename) ⇒ Object

Returns a FileCommand object based on the first protocol it sees in either filename or newfilename



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/libcfruby/fileops.rb', line 293

def FileOps.get_protocol(filename, newfilename)
	protocolregex = /^([a-zA-Z]+):\/\//
	protocol = 'file'
	
	match = protocolregex.match(filename)
	if(match == nil)
		match = protocolregex.match(newfilename)
	end
	
	if(match != nil)
		protocol = match[1]
	end
	
	case(protocol)
		when 'file'
			return(LocalFileCommand.new())
		when 'rsync'
			return(RsyncFileCommand.new())
		when 'http'
			return(HTTPFileCommand.new())
		else
			raise(FileOpsUnknownProtocolError, "Unknown protocol - \"#{protocol}\"")
	end
end

Creates a symbolic link linkfile which points to filename. If linkfile already exists and it is a directory, creates a symbolic link linkfile/filename. If linkfile already exists and it is not a directory, raises FileOpsOverwriteError. Returns true if a link is made false otherwise. Options:

:force

if true, overwrite linkfile even if it already exists



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/libcfruby/fileops.rb', line 437

def FileOps.link(filename, linkfile, options={})
  createdlink = false
	if !File.exist? filename
		raise(FileOpsFileExistError, "filename '#{filename}' does not exist")
	else
		Cfruby.controller.attempt("link '#{linkfile}' -> '#{filename}'", 'destructive') {
			# Use a realpath for the filename - a relative path fails below
			filename = Pathname.new(filename).realpath
			if(File.exists?(linkfile))
				if(File.symlink?(linkfile) and Pathname.new(linkfile).realpath == filename)
					# if the link already exists do nothing
					Cfruby.controller.attempt_abort("#{linkfile} already exists as a symlink")
				elsif(options[:force])
					unlink(linkfile)
				else
					raise(FileOpsOverwriteError, "#{linkfile} already exists")
				end
			end
			
			FileUtils.ln_s(filename, linkfile)
			createdlink = true
		}
	end
	
	return(createdlink)
end

.mkdir(dirname, options = {}) ⇒ Object

Creates a directory entry. dirname can be an Array or String. Options:

:mode

mode of the directory

:user

user to own the directory

:group

group to own the directory

:makeparent

make any needed parent directories

Returns true if a directory was created, false otherwise



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/libcfruby/fileops.rb', line 368

def FileOps.mkdir(dirname, options = {})
	if(dirname.kind_of?(String))
		dirname = Array.[](dirname)
	end

	created = false

	dirname.each { |d|
		Cfruby.controller.attempt("mkdir #{d}", 'destructive') {
			if(!File.directory?(d))
				if(options[:makeparent])
					FileUtils.mkdir_p(d)
				else
					FileUtils.mkdir(d)
				end
			  created = true
				mode = options[:mode]
				user = options[:user] or Process.euid()
				group = options[:group] or Process.egid()
				FileOps.chown(d,user,group,options)
				FileOps.chmod(d,mode) if mode
			else
				Cfruby.controller.attempt_abort("#{d} already exists")
			end
		}
	}
	
	return(created)
end

.move(filename, newfilename, options = {}) ⇒ Object

Moves filename to newfilename. Options may be set to one or more of the following:

:??????

anything defined under the protocol specific copy function



322
323
324
# File 'lib/libcfruby/fileops.rb', line 322

def FileOps.move(filename, newfilename, options = {})
	get_protocol(filename, newfilename).move(strip_protocol(filename), strip_protocol(newfilename), options)
end

.rmdir(dirname, force = false) ⇒ Object

Remove a directory entry. dirname can be an Array or String. Returns true if a directory was removed, false otherwise



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/libcfruby/fileops.rb', line 401

def FileOps.rmdir(dirname, force = false)
	if(dirname.kind_of?(String) or dirname.kind_of?(Pathname))
		dirname = Array.[](dirname)
	end

	deletedsomething = false
	dirname.each do | d |
		Cfruby.controller.attempt("rmdir #{d}", 'nonreversible', 'destructive') {
			if(!test(?e, d))
				Cfruby.controller.attempt_abort("#{d} does not exist")
			end
			if(test(?d, d))
				if(force)
					FileUtils.rm_rf(d)
					deletedsomething = true
				else
					FileUtils.rmdir(d)
					deletedsomething = true
				end
			else
				raise(FileOpsWrongFiletypeError, "\"#{d}\" is not a directory")
			end
		}
	end
	
	return(deletedsomething)
end

.set_backup(newbackup) ⇒ Object

Sets @@backup



532
533
534
# File 'lib/libcfruby/fileops.rb', line 532

def FileOps.set_backup(newbackup)
	@@backup = newbackup
end

.shell_chown_mod(basedir, owner, group, mode, options = {}) ⇒ Object

Same as FileOps.chown_mod, but uses direct shell commands. It can be invoked from Cfenjin by using the shell=true attribute with the file command.



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# File 'lib/libcfruby/fileops.rb', line 693

def FileOps.shell_chown_mod(basedir, owner, group, mode, options = {})
  changemade = false
	Cfruby.controller.attempt("changing ownership and mode of matching files in \"#{basedir}\"", 'destructive') {
		usermanager = Cfruby::OS::OSFactory.new.get_os.get_user_manager()
		if(owner and !owner.kind_of?(Integer))
			owner = usermanager.get_uid(owner)
		end
		if(group and !group.kind_of?(Integer))
			group = usermanager.get_gid(group)
		end

      # Some interesting timings:
      #
      # Using shell `cmd`:
      # real    1m12.693s
      # user    0m17.009s
      # sys     0m53.631s
      #
      # Using shell `cmd` using filesonly:
      # real    0m50.606s
      # user    0m15.649s
      # sys     0m32.918s
      #
      # Using Kernel.system:
      # real    1m33.448s
      # user    0m20.549s
      # sys     1m10.764s
      #
      # Using Kernel system 'filesonly':
      # real    0m58.321s
      # user    0m17.189s
      # sys     0m39.066s
      #
      # Using original Ruby code:
      # real    1m50.561s
      # user    0m24.702s
      # sys     1m23.837s
      #
      # Smart use of the shell function:
      #
      # real    0m22.285s
      # user    0m16.320s
      # sys     0m2.950s

      # A runtime reduction of 80%
      #
      # Opting for the fastest method now - Kernel.system is slightly
      # safer as it returns an error code.
      #
      if (options[:filesonly] and options[:recursive] and File.exist?(basedir))
        # Speed up this specific case by executing a shell command
        if not File.executable?('/bin/chown') or not File.executable?('/bin/chmod') or not File.executable?('/usr/bin/find')
          raise(FileOpsFileExistError,"Problem finding chmod/chown/find executables")
        end
        type = 'f'
        type = 'd' if options[:directoriesonly]
        if owner or group
          cmd = "/bin/chown #{owner}"
          if group
            cmd += ".#{group}"
            cmd = "/bin/chgrp #{group}" if !owner
          end
          cmd = "/usr/bin/find -type #{type} -exec #{cmd} \\{\\} \\;"
          cmd = "cd #{basedir} ; #{cmd}"
          Cfruby.controller.inform('verbose', cmd)
          `#{cmd}`
        end
        #if not Kernel.system(cmd)
        #  raise FileOpsSystemCommandError,'Command failed '+cmd
        #end
        if mode != nil
          mode = sprintf("%o",mode) if !mode.kind_of?(String)
          cmd = "/usr/bin/find -type #{type} -exec /bin/chmod #{mode} \\{\\} \\;"
          cmd = "cd #{basedir} ; #{cmd}"
			  Cfruby.controller.inform('verbose', cmd)
          `#{cmd}`
          #Kernel.system(cmd)
          #if not Kernel.system(cmd)
          #  raise FileOpsSystemCommandError,'Command failed '+cmd
          #end
        end
        changemade = true # no way to test for that here
      else
        Cfruby::FileFind.find(basedir, options) { |filename|
          if(FileOps.chown(filename, owner, group))
            changemade = true
          end
          if(mode!=nil and FileOps.chmod(filename, mode))
            changemade = true
          end
		  }
      end
	}
	
	return(changemade)
end

.touch(filename) ⇒ Object

Create an empty file named filename Returns true if the file was created, false otherwise



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/libcfruby/fileops.rb', line 337

def FileOps.touch(filename)
  created = false
	Cfruby.controller.attempt("touch #{filename}") {
		if File.exist? filename
			# if the file already exists do nothing
			Cfruby.controller.attempt_abort("#{filename} already exists - won't create")
		else
			f = File.new(filename,File::CREAT|File::TRUNC|File::RDWR)
			f.close
			Cfruby.controller.inform('verbose', "created file #{filename}")
			created = true
		end
	}
	
	return(created)
end

Alias for delete



356
357
358
# File 'lib/libcfruby/fileops.rb', line 356

def FileOps.unlink(filenamelist)
	FileOps.delete(filenamelist)
end