Class: FileUtils::Entry_

Inherits:
Object
  • Object
show all
Includes:
StreamUtils_
Defined in:
lib/fileutils.rb

Overview

:nodoc: internal use only

Constant Summary collapse

S_IF_DOOR =
0xD000

Instance Method Summary collapse

Constructor Details

#initialize(a, b = nil, deref = false) ⇒ Entry_

Returns a new instance of Entry_.



1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/fileutils.rb', line 1088

def initialize(a, b = nil, deref = false)
  @prefix = @rel = @path = nil
  if b
    @prefix = a
    @rel = b
  else
    @path = a
  end
  @deref = deref
  @stat = nil
  @lstat = nil
end

Instance Method Details

#blockdev?Boolean

Returns:

  • (Boolean)


1149
1150
1151
1152
# File 'lib/fileutils.rb', line 1149

def blockdev?
  s = lstat!
  s and s.blockdev?
end

#chardev?Boolean

Returns:

  • (Boolean)


1144
1145
1146
1147
# File 'lib/fileutils.rb', line 1144

def chardev?
  s = lstat!
  s and s.chardev?
end

#chmod(mode) ⇒ Object



1213
1214
1215
1216
1217
1218
1219
# File 'lib/fileutils.rb', line 1213

def chmod(mode)
  if symlink?
    File.lchmod mode, path() if have_lchmod?
  else
    File.chmod mode, path()
  end
end

#chown(uid, gid) ⇒ Object



1221
1222
1223
1224
1225
1226
1227
# File 'lib/fileutils.rb', line 1221

def chown(uid, gid)
  if symlink?
    File.lchown uid, gid, path() if have_lchown?
  else
    File.chown uid, gid, path()
  end
end

#copy(dest) ⇒ Object



1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/fileutils.rb', line 1229

def copy(dest)
  case
  when file?
    copy_file dest
  when directory?
    begin
      Dir.mkdir dest
    rescue
      raise unless File.directory?(dest)
    end
  when symlink?
    File.symlink File.readlink(path()), dest
  when chardev?
    raise "cannot handle device file" unless File.respond_to?(:mknod)
    mknod dest, ?c, 0666, lstat().rdev
  when blockdev?
    raise "cannot handle device file" unless File.respond_to?(:mknod)
    mknod dest, ?b, 0666, lstat().rdev
  when socket?
    raise "cannot handle socket" unless File.respond_to?(:mknod)
    mknod dest, nil, lstat().mode, 0
  when pipe?
    raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
    mkfifo dest, 0666
  when door?
    raise "cannot handle door: #{path()}"
  else
    raise "unknown file type: #{path()}"
  end
end

#copy_file(dest) ⇒ Object



1260
1261
1262
1263
1264
1265
1266
1267
# File 'lib/fileutils.rb', line 1260

def copy_file(dest)
  st = stat()
  File.open(path(),  'rb') {|r|
    File.open(dest, 'wb', st.mode) {|w|
      fu_copy_stream0 r, w, (fu_blksize(st) || fu_default_blksize())
    }
  }
end

#copy_metadata(path) ⇒ Object



1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/fileutils.rb', line 1269

def (path)
  st = lstat()
  File.utime st.atime, st.mtime, path
  begin
    File.chown st.uid, st.gid, path
  rescue Errno::EPERM
    # clear setuid/setgid
    File.chmod st.mode & 01777, path
  else
    File.chmod st.mode, path
  end
end

#dereference?Boolean

Returns:

  • (Boolean)


1121
1122
1123
# File 'lib/fileutils.rb', line 1121

def dereference?
  @deref
end

#directory?Boolean

Returns:

  • (Boolean)


1134
1135
1136
1137
# File 'lib/fileutils.rb', line 1134

def directory?
  s = lstat!
  s and s.directory?
end

#door?Boolean

Returns:

  • (Boolean)


1166
1167
1168
1169
# File 'lib/fileutils.rb', line 1166

def door?
  s = lstat!
  s and (s.mode & 0xF000 == S_IF_DOOR)
end

#entriesObject



1171
1172
1173
1174
1175
# File 'lib/fileutils.rb', line 1171

def entries
  Dir.entries(path())\
      .reject {|n| n == '.' or n == '..' }\
      .map {|n| Entry_.new(prefix(), join(rel(), n.untaint)) }
end

#exist?Boolean

Returns:

  • (Boolean)


1125
1126
1127
# File 'lib/fileutils.rb', line 1125

def exist?
  lstat! ? true : false
end

#file?Boolean

Returns:

  • (Boolean)


1129
1130
1131
1132
# File 'lib/fileutils.rb', line 1129

def file?
  s = lstat!
  s and s.file?
end

#inspectObject



1101
1102
1103
# File 'lib/fileutils.rb', line 1101

def inspect
  "\#<#{self.class} #{path()}>"
end

#lstatObject



1199
1200
1201
1202
1203
1204
1205
# File 'lib/fileutils.rb', line 1199

def lstat
  if dereference?
    @lstat ||= File.stat(path())
  else
    @lstat ||= File.lstat(path())
  end
end

#lstat!Object



1207
1208
1209
1210
1211
# File 'lib/fileutils.rb', line 1207

def lstat!
  lstat()
rescue SystemCallError
  nil
end

#pathObject



1105
1106
1107
1108
1109
1110
1111
# File 'lib/fileutils.rb', line 1105

def path
  if @path
    @path.to_str
  else
    join(@prefix, @rel)
  end
end

#pipe?Boolean

Returns:

  • (Boolean)


1159
1160
1161
1162
# File 'lib/fileutils.rb', line 1159

def pipe?
  s = lstat!
  s and s.pipe?
end

#platform_supportObject



1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
# File 'lib/fileutils.rb', line 1302

def platform_support
  return yield unless fu_windows?
  first_time_p = true
  begin
    yield
  rescue Errno::ENOENT
    raise
  rescue => err
    if first_time_p
      first_time_p = false
      begin
        File.chmod 0700, path()   # Windows does not have symlink
        retry
      rescue SystemCallError
      end
    end
    raise err
  end
end

#postorder_traverse {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
# File 'lib/fileutils.rb', line 1332

def postorder_traverse
  if directory?
    entries().each do |ent|
      ent.postorder_traverse do |e|
        yield e
      end
    end
  end
  yield self
end

#prefixObject



1113
1114
1115
# File 'lib/fileutils.rb', line 1113

def prefix
  @prefix || @path
end

#preorder_traverseObject Also known as: traverse



1322
1323
1324
1325
1326
1327
1328
# File 'lib/fileutils.rb', line 1322

def preorder_traverse
  stack = [self]
  while ent = stack.pop
    yield ent
    stack.concat ent.entries.reverse if ent.directory?
  end
end

#relObject



1117
1118
1119
# File 'lib/fileutils.rb', line 1117

def rel
  @rel
end

#removeObject



1282
1283
1284
1285
1286
1287
1288
# File 'lib/fileutils.rb', line 1282

def remove
  if directory?
    remove_dir1
  else
    remove_file
  end
end

#remove_dir1Object



1290
1291
1292
1293
1294
# File 'lib/fileutils.rb', line 1290

def remove_dir1
  platform_support {
    Dir.rmdir path().sub(%r</\z>, '')
  }
end

#remove_fileObject



1296
1297
1298
1299
1300
# File 'lib/fileutils.rb', line 1296

def remove_file
  platform_support {
    File.unlink path
  }
end

#socket?Boolean

Returns:

  • (Boolean)


1154
1155
1156
1157
# File 'lib/fileutils.rb', line 1154

def socket?
  s = lstat!
  s and s.socket?
end

#statObject



1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'lib/fileutils.rb', line 1177

def stat
  return @stat if @stat
  if lstat() and lstat().symlink?
    @stat = File.stat(path())
  else
    @stat = lstat()
  end
  @stat
end

#stat!Object



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
# File 'lib/fileutils.rb', line 1187

def stat!
  return @stat if @stat
  if lstat! and lstat!.symlink?
    @stat = File.stat(path())
  else
    @stat = lstat!
  end
  @stat
rescue SystemCallError
  nil
end

#symlink?Boolean

Returns:

  • (Boolean)


1139
1140
1141
1142
# File 'lib/fileutils.rb', line 1139

def symlink?
  s = lstat!
  s and s.symlink?
end