| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
MD5UCFS=${1:-${MD5UCFS:-'/tmpfs/ucfs'}} |
| 21 |
|
| 22 |
|
| 23 |
declare -a \ |
| 24 |
EXCLUDE_PATHS=${2:-${EXCLUDE_PATHS:-'(/proc /dev /devfs /tmpfs)'}} |
| 25 |
|
| 26 |
|
| 27 |
declare -a \ |
| 28 |
EXCLUDE_DIRS=${3:-${EXCLUDE_DIRS:-'(ucfs lost+found tmp wtmp)'}} |
| 29 |
|
| 30 |
|
| 31 |
declare -a \ |
| 32 |
EXCLUDE_FILES=${3:-${EXCLUDE_FILES:-'(core "Name with Spaces")'}} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
: <<LSfieldsDoc |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
String format description based on: ls (GNU fileutils) version 4.0.36 |
| 46 |
|
| 47 |
Produces a line (or more) formatted: |
| 48 |
inode permissions hard-links owner group ... |
| 49 |
32736 -rw------- 1 mszick mszick |
| 50 |
|
| 51 |
size day month date hh:mm:ss year path |
| 52 |
2756608 Sun Apr 20 08:53:06 2003 /home/mszick/core |
| 53 |
|
| 54 |
Unless it is formatted: |
| 55 |
inode permissions hard-links owner group ... |
| 56 |
266705 crw-rw---- 1 root uucp |
| 57 |
|
| 58 |
major minor day month date hh:mm:ss year path |
| 59 |
4, 68 Sun Apr 20 09:27:33 2003 /dev/ttyS4 |
| 60 |
NOTE: that pesky comma after the major number |
| 61 |
|
| 62 |
NOTE: the 'path' may be multiple fields: |
| 63 |
/home/mszick/core |
| 64 |
/proc/982/fd/0 -> /dev/null |
| 65 |
/proc/982/fd/1 -> /home/mszick/.xsession-errors |
| 66 |
/proc/982/fd/13 -> /tmp/tmpfZVVOCs (deleted) |
| 67 |
/proc/982/fd/7 -> /tmp/kde-mszick/ksycoca |
| 68 |
/proc/982/fd/8 -> socket:[11586] |
| 69 |
/proc/982/fd/9 -> pipe:[11588] |
| 70 |
|
| 71 |
If that isn't enough to keep your parser guessing, |
| 72 |
either or both of the path components may be relative: |
| 73 |
../Built-Shared -> Built-Static |
| 74 |
../linux-2.4.20.tar.bz2 -> ../../../SRCS/linux-2.4.20.tar.bz2 |
| 75 |
|
| 76 |
The first character of the 11 (10?) character permissions field: |
| 77 |
's' Socket |
| 78 |
'd' Directory |
| 79 |
'b' Block device |
| 80 |
'c' Character device |
| 81 |
'l' Symbolic link |
| 82 |
NOTE: Hard links not marked - test for identical inode numbers |
| 83 |
on identical filesystems. |
| 84 |
All information about hard linked files are shared, except |
| 85 |
for the names and the name's location in the directory system. |
| 86 |
NOTE: A "Hard link" is known as a "File Alias" on some systems. |
| 87 |
'-' An undistingushed file |
| 88 |
|
| 89 |
Followed by three groups of letters for: User, Group, Others |
| 90 |
Character 1: '-' Not readable; 'r' Readable |
| 91 |
Character 2: '-' Not writable; 'w' Writable |
| 92 |
Character 3, User and Group: Combined execute and special |
| 93 |
'-' Not Executable, Not Special |
| 94 |
'x' Executable, Not Special |
| 95 |
's' Executable, Special |
| 96 |
'S' Not Executable, Special |
| 97 |
Character 3, Others: Combined execute and sticky (tacky?) |
| 98 |
'-' Not Executable, Not Tacky |
| 99 |
'x' Executable, Not Tacky |
| 100 |
't' Executable, Tacky |
| 101 |
'T' Not Executable, Tacky |
| 102 |
|
| 103 |
Followed by an access indicator |
| 104 |
Haven't tested this one, it may be the eleventh character |
| 105 |
or it may generate another field |
| 106 |
' ' No alternate access |
| 107 |
'+' Alternate access |
| 108 |
LSfieldsDoc |
| 109 |
|
| 110 |
|
| 111 |
ListDirectory() |
| 112 |
{ |
| 113 |
local -a T |
| 114 |
local -i of=0 # Default return in variable |
| 115 |
# OLD_IFS=$IFS # Using BASH default ' \t\n' |
| 116 |
|
| 117 |
case "$#" in |
| 118 |
3) case "$1" in |
| 119 |
-of) of=1 ; shift ;; |
| 120 |
* ) return 1 ;; |
| 121 |
esac ;; |
| 122 |
2) : ;; # Poor man's "continue" |
| 123 |
*) return 1 ;; |
| 124 |
esac |
| 125 |
|
| 126 |
|
| 127 |
T=( $(ls --inode --ignore-backups --almost-all --directory \ |
| 128 |
--full-time --color=none --time=status --sort=none \ |
| 129 |
--format=long $1) ) |
| 130 |
|
| 131 |
case $of in |
| 132 |
|
| 133 |
0) eval $2=\( \"\$\{T\[@\]\}\" \) ;; |
| 134 |
|
| 135 |
1) echo "${T[@]}" > "$2" ;; |
| 136 |
esac |
| 137 |
return 0 |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
IsNumber() |
| 146 |
{ |
| 147 |
local -i int |
| 148 |
if [ $# -eq 0 ] |
| 149 |
then |
| 150 |
return 1 |
| 151 |
else |
| 152 |
(let int=$1) 2>/dev/null |
| 153 |
return $? |
| 154 |
fi |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
: <<IndexListDoc |
| 167 |
Walk an array of directory fields produced by ListDirectory |
| 168 |
|
| 169 |
Having suppressed the line breaks in an otherwise line oriented |
| 170 |
report, build an index to the array element which starts each line. |
| 171 |
|
| 172 |
Each line gets two index entries, the first element of each line |
| 173 |
(inode) and the element that holds the pathname of the file. |
| 174 |
|
| 175 |
The first index entry pair (Line-Number==0) are informational: |
| 176 |
Index-Array-Name[0] : Number of "Lines" indexed |
| 177 |
Index-Array-Name[1] : "Current Line" pointer into Index-Array-Name |
| 178 |
|
| 179 |
The following index pairs (if any) hold element indexes into |
| 180 |
the Field-Array-Name per: |
| 181 |
Index-Array-Name[Line-Number * 2] : The "inode" field element. |
| 182 |
NOTE: This distance may be either +11 or +12 elements. |
| 183 |
Index-Array-Name[(Line-Number * 2) + 1] : The "pathname" element. |
| 184 |
NOTE: This distance may be a variable number of elements. |
| 185 |
Next line index pair for Line-Number+1. |
| 186 |
IndexListDoc |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
IndexList() |
| 191 |
{ |
| 192 |
local -a LIST |
| 193 |
local -a -i INDEX=( 0 0 ) |
| 194 |
local -i Lidx Lcnt |
| 195 |
local -i if=0 of=0 |
| 196 |
|
| 197 |
case "$#" in |
| 198 |
0) return 1 ;; |
| 199 |
1) return 1 ;; |
| 200 |
2) : ;; |
| 201 |
3) case "$1" in |
| 202 |
-if) if=1 ;; |
| 203 |
-of) of=1 ;; |
| 204 |
* ) return 1 ;; |
| 205 |
esac ; shift ;; |
| 206 |
4) if=1 ; of=1 ; shift ; shift ;; |
| 207 |
*) return 1 |
| 208 |
esac |
| 209 |
|
| 210 |
|
| 211 |
case "$if" in |
| 212 |
0) eval LIST=\( \"\$\{$1\[@\]\}\" \) ;; |
| 213 |
1) LIST=( $(cat $1) ) ;; |
| 214 |
esac |
| 215 |
|
| 216 |
|
| 217 |
Lcnt=${#LIST[@]} |
| 218 |
Lidx=0 |
| 219 |
until (( Lidx >= Lcnt )) |
| 220 |
do |
| 221 |
if IsNumber ${LIST[$Lidx]} |
| 222 |
then |
| 223 |
local -i inode name |
| 224 |
local ft |
| 225 |
inode=Lidx |
| 226 |
local m=${LIST[$Lidx+2]} |
| 227 |
ft=${LIST[$Lidx+1]:0:1} |
| 228 |
case $ft in |
| 229 |
b) ((Lidx+=12)) ;; |
| 230 |
c) ((Lidx+=12)) ;; |
| 231 |
*) ((Lidx+=11)) ;; |
| 232 |
esac |
| 233 |
name=Lidx |
| 234 |
case $ft in |
| 235 |
-) ((Lidx+=1)) ;; |
| 236 |
b) ((Lidx+=1)) ;; |
| 237 |
c) ((Lidx+=1)) ;; |
| 238 |
d) ((Lidx+=1)) ;; |
| 239 |
l) ((Lidx+=3)) ;; |
| 240 |
|
| 241 |
|
| 242 |
*) until IsNumber ${LIST[$Lidx]} || ((Lidx >= Lcnt)) |
| 243 |
do |
| 244 |
((Lidx+=1)) |
| 245 |
done |
| 246 |
;; |
| 247 |
esac |
| 248 |
INDEX[${#INDEX[*]}]=$inode |
| 249 |
INDEX[${#INDEX[*]}]=$name |
| 250 |
INDEX[0]=${INDEX[0]}+1 |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
else |
| 255 |
((Lidx+=1)) |
| 256 |
fi |
| 257 |
done |
| 258 |
case "$of" in |
| 259 |
0) eval $2=\( \"\$\{INDEX\[@\]\}\" \) ;; |
| 260 |
1) echo "${INDEX[@]}" > "$2" ;; |
| 261 |
esac |
| 262 |
return 0 |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
: <<DigestFilesDoc |
| 274 |
|
| 275 |
The key (no pun intended) to a Unified Content File System (UCFS) |
| 276 |
is to distinguish the files in the system based on their content. |
| 277 |
Distinguishing files by their name is just so 20th Century. |
| 278 |
|
| 279 |
The content is distinguished by computing a checksum of that content. |
| 280 |
This version uses the md5sum program to generate a 128 bit checksum |
| 281 |
representative of the file's contents. |
| 282 |
There is a chance that two files having different content might |
| 283 |
generate the same checksum using md5sum (or any checksum). Should |
| 284 |
that become a problem, then the use of md5sum can be replace by a |
| 285 |
cyrptographic signature. But until then... |
| 286 |
|
| 287 |
The md5sum program is documented as outputting three fields (and it |
| 288 |
does), but when read it appears as two fields (array elements). This |
| 289 |
is caused by the lack of whitespace between the second and third field. |
| 290 |
So this function gropes the md5sum output and returns: |
| 291 |
[0] 32 character checksum in hexidecimal (UCFS filename) |
| 292 |
[1] Single character: ' ' text file, '*' binary file |
| 293 |
[2] Filesystem (20th Century Style) name |
| 294 |
Note: That name may be the character '-' indicating STDIN read. |
| 295 |
|
| 296 |
DigestFilesDoc |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
DigestFile() |
| 301 |
{ |
| 302 |
local if=0 # Default, variable name |
| 303 |
local -a T1 T2 |
| 304 |
|
| 305 |
case "$#" in |
| 306 |
3) case "$1" in |
| 307 |
-if) if=1 ; shift ;; |
| 308 |
* ) return 1 ;; |
| 309 |
esac ;; |
| 310 |
2) : ;; # Poor man's "continue" |
| 311 |
*) return 1 ;; |
| 312 |
esac |
| 313 |
|
| 314 |
case $if in |
| 315 |
0) eval T1=\( \"\$\{$1\[@\]\}\" \) |
| 316 |
T2=( $(echo ${T1[@]} | md5sum -) ) |
| 317 |
;; |
| 318 |
1) T2=( $(md5sum $1) ) |
| 319 |
;; |
| 320 |
esac |
| 321 |
|
| 322 |
case ${#T2[@]} in |
| 323 |
0) return 1 ;; |
| 324 |
1) return 1 ;; |
| 325 |
2) case ${T2[1]:0:1} in |
| 326 |
\*) T2[${#T2[@]}]=${T2[1]:1} |
| 327 |
T2[1]=\* |
| 328 |
;; |
| 329 |
*) T2[${#T2[@]}]=${T2[1]} |
| 330 |
T2[1]=" " |
| 331 |
;; |
| 332 |
esac |
| 333 |
;; |
| 334 |
3) : ;; |
| 335 |
*) return 1 ;; |
| 336 |
esac |
| 337 |
|
| 338 |
local -i len=${#T2[0]} |
| 339 |
if [ $len -ne 32 ] ; then return 1 ; fi |
| 340 |
eval $2=\( \"\$\{T2\[@\]\}\" \) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
: <<StatFieldsDoc |
| 354 |
Based on stat, version 2.2 |
| 355 |
stat -t and stat -lt fields |
| 356 |
[0] name |
| 357 |
[1] Total size |
| 358 |
File - number of bytes |
| 359 |
Symbolic link - string length of pathname |
| 360 |
[2] Number of (512 byte) blocks allocated |
| 361 |
[3] File type and Access rights (hex) |
| 362 |
[4] User ID of owner |
| 363 |
[5] Group ID of owner |
| 364 |
[6] Device number |
| 365 |
[7] Inode number |
| 366 |
[8] Number of hard links |
| 367 |
[9] Device type (if inode device) Major |
| 368 |
[10] Device type (if inode device) Minor |
| 369 |
[11] Time of last access |
| 370 |
May be disabled in 'mount' with noatime |
| 371 |
atime of files changed by exec, read, pipe, utime, mknod (mmap?) |
| 372 |
atime of directories changed by addition/deletion of files |
| 373 |
[12] Time of last modification |
| 374 |
mtime of files changed by write, truncate, utime, mknod |
| 375 |
mtime of directories changed by addtition/deletion of files |
| 376 |
[13] Time of last change |
| 377 |
ctime reflects time of changed inode information (owner, group |
| 378 |
permissions, link count |
| 379 |
-*-*- Per: |
| 380 |
Return code: 0 |
| 381 |
Size of array: 14 |
| 382 |
Contents of array |
| 383 |
Element 0: /home/mszick |
| 384 |
Element 1: 4096 |
| 385 |
Element 2: 8 |
| 386 |
Element 3: 41e8 |
| 387 |
Element 4: 500 |
| 388 |
Element 5: 500 |
| 389 |
Element 6: 303 |
| 390 |
Element 7: 32385 |
| 391 |
Element 8: 22 |
| 392 |
Element 9: 0 |
| 393 |
Element 10: 0 |
| 394 |
Element 11: 1051221030 |
| 395 |
Element 12: 1051214068 |
| 396 |
Element 13: 1051214068 |
| 397 |
|
| 398 |
For a link in the form of linkname -> realname |
| 399 |
stat -t linkname returns the linkname (link) information |
| 400 |
stat -lt linkname returns the realname information |
| 401 |
|
| 402 |
stat -tf and stat -ltf fields |
| 403 |
[0] name |
| 404 |
[1] ID-0? |
| 405 |
[2] ID-0? |
| 406 |
|
| 407 |
|
| 408 |
These will be munged into: |
| 409 |
[1] UUID if possible |
| 410 |
[2] Volume Label if possible |
| 411 |
Note: 'mount -l' does return the label and could return the UUID |
| 412 |
|
| 413 |
[3] Maximum length of filenames |
| 414 |
[4] Filesystem type |
| 415 |
[5] Total blocks in the filesystem |
| 416 |
[6] Free blocks |
| 417 |
[7] Free blocks for non-root user(s) |
| 418 |
[8] Block size of the filesystem |
| 419 |
[9] Total inodes |
| 420 |
[10] Free inodes |
| 421 |
|
| 422 |
-*-*- Per: |
| 423 |
Return code: 0 |
| 424 |
Size of array: 11 |
| 425 |
Contents of array |
| 426 |
Element 0: /home/mszick |
| 427 |
Element 1: 0 |
| 428 |
Element 2: 0 |
| 429 |
Element 3: 255 |
| 430 |
Element 4: ef53 |
| 431 |
Element 5: 2581445 |
| 432 |
Element 6: 2277180 |
| 433 |
Element 7: 2146050 |
| 434 |
Element 8: 4096 |
| 435 |
Element 9: 1311552 |
| 436 |
Element 10: 1276425 |
| 437 |
|
| 438 |
StatFieldsDoc |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
LocateFile() |
| 445 |
{ |
| 446 |
local -a LOC LOC1 LOC2 |
| 447 |
local lk="" of=0 |
| 448 |
|
| 449 |
case "$#" in |
| 450 |
0) return 1 ;; |
| 451 |
1) return 1 ;; |
| 452 |
2) : ;; |
| 453 |
*) while (( "$#" > 2 )) |
| 454 |
do |
| 455 |
case "$1" in |
| 456 |
-l) lk=-1 ;; |
| 457 |
-of) of=1 ;; |
| 458 |
*) return 1 ;; |
| 459 |
esac |
| 460 |
shift |
| 461 |
done ;; |
| 462 |
esac |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
LOC=( ${LOC1[@]:0:1} ${LOC1[@]:3:11} |
| 469 |
${LOC2[@]:1:2} ${LOC2[@]:4:1} ) |
| 470 |
|
| 471 |
case "$of" in |
| 472 |
0) eval $2=\( \"\$\{LOC\[@\]\}\" \) ;; |
| 473 |
1) echo "${LOC[@]}" > "$2" ;; |
| 474 |
esac |
| 475 |
return 0 |
| 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 |
|
| 501 |
|
| 502 |
ListArray() |
| 503 |
{ |
| 504 |
local -a Ta |
| 505 |
|
| 506 |
eval Ta=\( \"\$\{$1\[@\]\}\" \) |
| 507 |
echo |
| 508 |
echo "-*-*- List of Array -*-*-" |
| 509 |
echo "Size of array $1: ${#Ta[*]}" |
| 510 |
echo "Contents of array $1:" |
| 511 |
for (( i=0 ; i<${#Ta[*]} ; i++ )) |
| 512 |
do |
| 513 |
echo -e "\tElement $i: ${Ta[$i]}" |
| 514 |
done |
| 515 |
return 0 |
| 516 |
} |
| 517 |
|
| 518 |
declare -a CUR_DIR |
| 519 |
|
| 520 |
ListDirectory "${PWD}" CUR_DIR |
| 521 |
ListArray CUR_DIR |
| 522 |
|
| 523 |
declare -a DIR_DIG |
| 524 |
DigestFile CUR_DIR DIR_DIG |
| 525 |
echo "The new \"name\" (checksum) for ${CUR_DIR[9]} is ${DIR_DIG[0]}" |
| 526 |
|
| 527 |
declare -a DIR_ENT |
| 528 |
|
| 529 |
|
| 530 |
ListDirectory "${CUR_DIR[11]}/*" DIR_ENT |
| 531 |
|
| 532 |
declare -a DIR_IDX |
| 533 |
|
| 534 |
IndexList DIR_ENT DIR_IDX |
| 535 |
|
| 536 |
declare -a IDX_DIG |
| 537 |
|
| 538 |
|
| 539 |
DigestFile DIR_ENT IDX_DIG |
| 540 |
|
| 541 |
|
| 542 |
echo "The \"name\" (checksum) for the contents of ${PWD} is ${IDX_DIG[0]}" |
| 543 |
|
| 544 |
declare -a FILE_LOC |
| 545 |
LocateFile ${PWD} FILE_LOC |
| 546 |
ListArray FILE_LOC |
| 547 |
|
| 548 |
exit 0 |