check.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /* check.c - Check and repair a PC/MS-DOS file system
  2. Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
  3. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems, the complete text of the GNU General Public License
  15. can be found in /usr/share/common-licenses/GPL-3 file.
  16. */
  17. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  18. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <limits.h>
  23. #include <time.h>
  24. #include "common.h"
  25. #include "dosfsck.h"
  26. #include "io.h"
  27. #include "fat.h"
  28. #include "file.h"
  29. #include "lfn.h"
  30. #include "check.h"
  31. static DOS_FILE *root;
  32. /* get start field of a dir entry */
  33. #define FSTART(p,fs) \
  34. ((unsigned long)CF_LE_W(p->dir_ent.start) | \
  35. (fs->fat_bits == 32 ? CF_LE_W(p->dir_ent.starthi) << 16 : 0))
  36. #define MODIFY(p,i,v) \
  37. do { \
  38. if (p->offset) { \
  39. p->dir_ent.i = v; \
  40. fs_write(p->offset+offsetof(DIR_ENT,i), \
  41. sizeof(p->dir_ent.i),&p->dir_ent.i); \
  42. } \
  43. } while(0)
  44. #define MODIFY_START(p,v,fs) \
  45. do { \
  46. unsigned long __v = (v); \
  47. if (!p->offset) { \
  48. /* writing to fake entry for FAT32 root dir */ \
  49. if (!__v) die("Oops, deleting FAT32 root dir!"); \
  50. fs->root_cluster = __v; \
  51. p->dir_ent.start = CT_LE_W(__v&0xffff); \
  52. p->dir_ent.starthi = CT_LE_W(__v>>16); \
  53. __v = CT_LE_L(__v); \
  54. fs_write((loff_t)offsetof(struct boot_sector,root_cluster), \
  55. sizeof(((struct boot_sector *)0)->root_cluster), \
  56. &__v); \
  57. } \
  58. else { \
  59. MODIFY(p,start,CT_LE_W((__v)&0xffff)); \
  60. if (fs->fat_bits == 32) \
  61. MODIFY(p,starthi,CT_LE_W((__v)>>16)); \
  62. } \
  63. } while(0)
  64. loff_t alloc_rootdir_entry(DOS_FS *fs, DIR_ENT *de, const char *pattern)
  65. {
  66. static int curr_num = 0;
  67. loff_t offset;
  68. if (fs->root_cluster) {
  69. DIR_ENT d2;
  70. int i = 0, got = 0;
  71. unsigned long clu_num, prev = 0;
  72. loff_t offset2;
  73. clu_num = fs->root_cluster;
  74. offset = cluster_start(fs,clu_num);
  75. while (clu_num > 0 && clu_num != -1) {
  76. fs_read(offset,sizeof(DIR_ENT),&d2);
  77. if (IS_FREE(d2.name) && d2.attr != VFAT_LN_ATTR) {
  78. got = 1;
  79. break;
  80. }
  81. i += sizeof(DIR_ENT);
  82. offset += sizeof(DIR_ENT);
  83. if ((i % fs->cluster_size) == 0) {
  84. prev = clu_num;
  85. if ((clu_num = next_cluster(fs,clu_num)) == 0 || clu_num == -1)
  86. break;
  87. offset = cluster_start(fs,clu_num);
  88. }
  89. }
  90. if (!got) {
  91. /* no free slot, need to extend root dir: alloc next free cluster
  92. * after previous one */
  93. if (!prev)
  94. die("Root directory has no cluster allocated!");
  95. for (clu_num = prev+1; clu_num != prev; clu_num++) {
  96. FAT_ENTRY entry;
  97. if (clu_num >= fs->clusters+2) clu_num = 2;
  98. get_fat(&entry, fs->fat, clu_num, fs);
  99. if (!entry.value)
  100. break;
  101. }
  102. if (clu_num == prev)
  103. die("Root directory full and no free cluster");
  104. set_fat(fs,prev,clu_num);
  105. set_fat(fs,clu_num,-1);
  106. set_owner(fs, clu_num, get_owner(fs, fs->root_cluster));
  107. /* clear new cluster */
  108. memset( &d2, 0, sizeof(d2) );
  109. offset = cluster_start(fs,clu_num);
  110. for( i = 0; i < fs->cluster_size; i += sizeof(DIR_ENT) )
  111. fs_write( offset+i, sizeof(d2), &d2 );
  112. }
  113. memset(de,0,sizeof(DIR_ENT));
  114. while (1) {
  115. char expanded[12];
  116. sprintf(expanded, pattern, curr_num);
  117. memcpy(de->name+4, expanded, 4);
  118. memcpy(de->ext, expanded+4, 3);
  119. clu_num = fs->root_cluster;
  120. i = 0;
  121. offset2 = cluster_start(fs,clu_num);
  122. while (clu_num > 0 && clu_num != -1) {
  123. fs_read(offset2,sizeof(DIR_ENT),&d2);
  124. if (offset2 != offset &&
  125. !strncmp(d2.name,de->name,MSDOS_NAME))
  126. break;
  127. i += sizeof(DIR_ENT);
  128. offset2 += sizeof(DIR_ENT);
  129. if ((i % fs->cluster_size) == 0) {
  130. if ((clu_num = next_cluster(fs,clu_num)) == 0 ||
  131. clu_num == -1)
  132. break;
  133. offset2 = cluster_start(fs,clu_num);
  134. }
  135. }
  136. if (clu_num == 0 || clu_num == -1)
  137. break;
  138. if (++curr_num >= 10000) die("Unable to create unique name");
  139. }
  140. }
  141. else {
  142. DIR_ENT *root;
  143. int next_free = 0, scan;
  144. root = alloc(fs->root_entries*sizeof(DIR_ENT));
  145. fs_read(fs->root_start,fs->root_entries*sizeof(DIR_ENT),root);
  146. while (next_free < fs->root_entries)
  147. if (IS_FREE(root[next_free].name) &&
  148. root[next_free].attr != VFAT_LN_ATTR)
  149. break;
  150. else next_free++;
  151. if (next_free == fs->root_entries)
  152. die("Root directory is full.");
  153. offset = fs->root_start+next_free*sizeof(DIR_ENT);
  154. memset(de,0,sizeof(DIR_ENT));
  155. while (1) {
  156. sprintf(de->name,pattern,curr_num);
  157. for (scan = 0; scan < fs->root_entries; scan++)
  158. if (scan != next_free &&
  159. !strncmp(root[scan].name,de->name,MSDOS_NAME))
  160. break;
  161. if (scan == fs->root_entries) break;
  162. if (++curr_num >= 10000) die("Unable to create unique name");
  163. }
  164. free(root);
  165. }
  166. ++n_files;
  167. return offset;
  168. }
  169. /**
  170. * Construct a full path (starting with '/') for the specified dentry,
  171. * relative to the partition. All components are "long" names where possible.
  172. *
  173. * @param[in] file Information about dentry (file or directory) of interest
  174. *
  175. * return Pointer to static string containing file's full path
  176. */
  177. static char *path_name(DOS_FILE *file)
  178. {
  179. static char path[PATH_MAX*2];
  180. if (!file) *path = 0; /* Reached the root directory */
  181. else {
  182. if (strlen(path_name(file->parent)) > PATH_MAX)
  183. die("Path name too long.");
  184. if (strcmp(path,"/") != 0) strcat(path,"/");
  185. /* Append the long name to the path,
  186. * or the short name if there isn't a long one
  187. */
  188. strcpy(strrchr(path,0),file->lfn?file->lfn:file_name(file->dir_ent.name));
  189. }
  190. return path;
  191. }
  192. static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 };
  193. /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
  194. /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
  195. time_t date_dos2unix(unsigned short time,unsigned short date)
  196. {
  197. int month,year;
  198. time_t secs;
  199. month = ((date >> 5) & 15)-1;
  200. year = date >> 9;
  201. secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400*
  202. ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 &&
  203. month < 2 ? 1 : 0)+3653);
  204. /* days since 1.1.70 plus 80's leap day */
  205. return secs;
  206. }
  207. static char *file_stat(DOS_FILE *file)
  208. {
  209. static char temp[100];
  210. struct tm *tm;
  211. char tmp[100];
  212. time_t date;
  213. date = date_dos2unix(CF_LE_W(file->dir_ent.time),CF_LE_W(file->
  214. dir_ent.date));
  215. tm = localtime(&date);
  216. strftime(tmp,99,"%H:%M:%S %b %d %Y",tm);
  217. sprintf(temp," Size %u bytes, date %s",CF_LE_L(file->dir_ent.size),tmp);
  218. return temp;
  219. }
  220. static int bad_name(DOS_FILE *file)
  221. {
  222. int i, spc, suspicious = 0;
  223. char *bad_chars = atari_format ? "*?\\/:" : "*?<>|\"\\/:";
  224. unsigned char *name = file->dir_ent.name;
  225. /* Do not complain about (and auto-correct) the extended attribute files
  226. * of OS/2. */
  227. if (strncmp(name,"EA DATA SF",11) == 0 ||
  228. strncmp(name,"WP ROOT SF",11) == 0) return 0;
  229. /* don't complain about the dummy 11 bytes used by patched Linux
  230. kernels */
  231. if (file->dir_ent.lcase & FAT_NO_83NAME) return 0;
  232. for (i = 0; i < 8; i++) {
  233. if (name[i] < ' ' || name[i] == 0x7f) return 1;
  234. if (name[i] > 0x7f) ++suspicious;
  235. if (strchr(bad_chars,name[i])) return 1;
  236. }
  237. for (i = 8; i < 11; i++) {
  238. if (name[i] < ' ' || name[i] == 0x7f) return 1;
  239. if (name[i] > 0x7f) ++suspicious;
  240. if (strchr(bad_chars,name[i])) return 1;
  241. }
  242. spc = 0;
  243. for (i = 0; i < 8; i++) {
  244. if (name[i] == ' ')
  245. spc = 1;
  246. else if (spc)
  247. /* non-space after a space not allowed, space terminates the name
  248. * part */
  249. return 1;
  250. }
  251. spc = 0;
  252. for (i = 8; i < 11; i++) {
  253. if (name[i] == ' ')
  254. spc = 1;
  255. else if (spc)
  256. /* non-space after a space not allowed, space terminates the name
  257. * part */
  258. return 1;
  259. }
  260. /* Under GEMDOS, chars >= 128 are never allowed. */
  261. if (atari_format && suspicious)
  262. return 1;
  263. /* Under MS-DOS and Windows, chars >= 128 in short names are valid
  264. * (but these characters can be visualised differently depending on
  265. * local codepage: CP437, CP866, etc). The chars are all basically ok,
  266. * so we shouldn't auto-correct such names. */
  267. return 0;
  268. }
  269. static void drop_file(DOS_FS *fs,DOS_FILE *file)
  270. {
  271. unsigned long cluster;
  272. MODIFY(file,name[0],DELETED_FLAG);
  273. for (cluster = FSTART(file,fs); cluster > 0 && cluster <
  274. fs->clusters+2; cluster = next_cluster(fs,cluster))
  275. set_owner(fs,cluster,NULL);
  276. --n_files;
  277. }
  278. static void truncate_file(DOS_FS *fs,DOS_FILE *file,unsigned long clusters)
  279. {
  280. int deleting;
  281. unsigned long walk,next,prev;
  282. walk = FSTART(file,fs);
  283. prev = 0;
  284. if ((deleting = !clusters)) MODIFY_START(file,0,fs);
  285. while (walk > 0 && walk != -1) {
  286. next = next_cluster(fs,walk);
  287. if (deleting) set_fat(fs,walk,0);
  288. else if ((deleting = !--clusters)) set_fat(fs,walk,-1);
  289. prev = walk;
  290. walk = next;
  291. }
  292. }
  293. static void auto_rename(DOS_FILE *file)
  294. {
  295. DOS_FILE *first,*walk;
  296. unsigned long int number;
  297. if (!file->offset) return; /* cannot rename FAT32 root dir */
  298. first = file->parent ? file->parent->first : root;
  299. number = 0;
  300. while (1) {
  301. char num[8];
  302. sprintf(num, "%07d", number);
  303. memcpy(file->dir_ent.name, "FSCK", 4);
  304. memcpy(file->dir_ent.name+4, num, 4);
  305. memcpy(file->dir_ent.ext, num+4, 3);
  306. for (walk = first; walk; walk = walk->next)
  307. if (walk != file && !strncmp(walk->dir_ent.name,file->dir_ent.
  308. name,MSDOS_NAME)) break;
  309. if (!walk) {
  310. fs_write(file->offset,MSDOS_NAME,file->dir_ent.name);
  311. return;
  312. }
  313. number++;
  314. if (number > 9999999) {
  315. die("Too many files need repair.");
  316. }
  317. }
  318. die("Can't generate a unique name.");
  319. }
  320. static void rename_file(DOS_FILE *file)
  321. {
  322. unsigned char name[46];
  323. unsigned char *walk,*here;
  324. if (!file->offset) {
  325. printf( "Cannot rename FAT32 root dir\n" );
  326. return; /* cannot rename FAT32 root dir */
  327. }
  328. while (1) {
  329. printf("New name: ");
  330. fflush(stdout);
  331. if (fgets(name,45,stdin)) {
  332. if ((here = strchr(name,'\n'))) *here = 0;
  333. for (walk = strrchr(name,0); walk >= name && (*walk == ' ' ||
  334. *walk == '\t'); walk--);
  335. walk[1] = 0;
  336. for (walk = name; *walk == ' ' || *walk == '\t'; walk++);
  337. if (file_cvt(walk,file->dir_ent.name)) {
  338. fs_write(file->offset,MSDOS_NAME,file->dir_ent.name);
  339. return;
  340. }
  341. }
  342. }
  343. }
  344. static int handle_dot(DOS_FS *fs,DOS_FILE *file,int dots)
  345. {
  346. char *name;
  347. name = strncmp(file->dir_ent.name,MSDOS_DOT,MSDOS_NAME) ? ".." : ".";
  348. if (!(file->dir_ent.attr & ATTR_DIR)) {
  349. printf("%s\n Is a non-directory.\n",path_name(file));
  350. if (interactive)
  351. printf("1) Drop it\n2) Auto-rename\n3) Rename\n"
  352. "4) Convert to directory\n");
  353. else printf(" Auto-renaming it.\n");
  354. switch (interactive ? get_key("1234","?") : '2') {
  355. case '1':
  356. drop_file(fs,file);
  357. return 1;
  358. case '2':
  359. auto_rename(file);
  360. printf(" Renamed to %s\n",file_name(file->dir_ent.name));
  361. return 0;
  362. case '3':
  363. rename_file(file);
  364. return 0;
  365. case '4':
  366. MODIFY(file,size,CT_LE_L(0));
  367. MODIFY(file,attr,file->dir_ent.attr | ATTR_DIR);
  368. break;
  369. }
  370. }
  371. if (!dots) {
  372. printf("Root contains directory \"%s\". Dropping it.\n",name);
  373. drop_file(fs,file);
  374. return 1;
  375. }
  376. return 0;
  377. }
  378. static int check_file(DOS_FS *fs,DOS_FILE *file)
  379. {
  380. DOS_FILE *owner;
  381. int restart;
  382. unsigned long expect,curr,this,clusters,prev,walk,clusters2;
  383. if (file->dir_ent.attr & ATTR_DIR) {
  384. if (CF_LE_L(file->dir_ent.size)) {
  385. printf("%s\n Directory has non-zero size. Fixing it.\n",
  386. path_name(file));
  387. MODIFY(file,size,CT_LE_L(0));
  388. }
  389. if (file->parent && !strncmp(file->dir_ent.name,MSDOS_DOT,MSDOS_NAME)) {
  390. expect = FSTART(file->parent,fs);
  391. if (FSTART(file,fs) != expect) {
  392. printf("%s\n Start (%ld) does not point to parent (%ld)\n",
  393. path_name(file),FSTART(file,fs),expect);
  394. MODIFY_START(file,expect,fs);
  395. }
  396. return 0;
  397. }
  398. if (file->parent && !strncmp(file->dir_ent.name,MSDOS_DOTDOT,
  399. MSDOS_NAME)) {
  400. expect = file->parent->parent ? FSTART(file->parent->parent,fs):0;
  401. if (fs->root_cluster && expect == fs->root_cluster)
  402. expect = 0;
  403. if (FSTART(file,fs) != expect) {
  404. printf("%s\n Start (%lu) does not point to .. (%lu)\n",
  405. path_name(file),FSTART(file,fs),expect);
  406. MODIFY_START(file,expect,fs);
  407. }
  408. return 0;
  409. }
  410. if (FSTART(file,fs)==0){
  411. printf ("%s\n Start does point to root directory. Deleting dir. \n",
  412. path_name(file));
  413. MODIFY(file,name[0],DELETED_FLAG);
  414. return 0;
  415. }
  416. }
  417. if (FSTART(file,fs) >= fs->clusters+2) {
  418. printf("%s\n Start cluster beyond limit (%lu > %lu). Truncating file.\n",
  419. path_name(file),FSTART(file,fs),fs->clusters+1);
  420. if (!file->offset)
  421. die( "Bad FAT32 root directory! (bad start cluster)\n" );
  422. MODIFY_START(file,0,fs);
  423. }
  424. clusters = prev = 0;
  425. for (curr = FSTART(file,fs) ? FSTART(file,fs) :
  426. -1; curr != -1; curr = next_cluster(fs,curr)) {
  427. FAT_ENTRY curEntry;
  428. get_fat(&curEntry, fs->fat, curr, fs);
  429. if (!curEntry.value || bad_cluster(fs,curr)) {
  430. printf("%s\n Contains a %s cluster (%lu). Assuming EOF.\n",
  431. path_name(file), curEntry.value ? "bad" : "free",curr);
  432. if (prev) set_fat(fs,prev,-1);
  433. else if (!file->offset)
  434. die( "FAT32 root dir starts with a bad cluster!" );
  435. else MODIFY_START(file,0,fs);
  436. break;
  437. }
  438. if (!(file->dir_ent.attr & ATTR_DIR) && CF_LE_L(file->dir_ent.size) <=
  439. (unsigned long long)clusters*fs->cluster_size) {
  440. printf("%s\n File size is %u bytes, cluster chain length is > %llu "
  441. "bytes.\n Truncating file to %u bytes.\n",path_name(file),
  442. CF_LE_L(file->dir_ent.size),(unsigned long long)clusters*fs->cluster_size,
  443. CF_LE_L(file->dir_ent.size));
  444. truncate_file(fs,file,clusters);
  445. break;
  446. }
  447. if ((owner = get_owner(fs,curr))) {
  448. int do_trunc = 0;
  449. printf("%s and\n",path_name(owner));
  450. printf("%s\n share clusters.\n",path_name(file));
  451. clusters2 = 0;
  452. for (walk = FSTART(owner,fs); walk > 0 && walk != -1; walk =
  453. next_cluster(fs,walk))
  454. if (walk == curr) break;
  455. else clusters2++;
  456. restart = file->dir_ent.attr & ATTR_DIR;
  457. if (!owner->offset) {
  458. printf( " Truncating second to %llu bytes because first "
  459. "is FAT32 root dir.\n", (unsigned long long)clusters2*fs->cluster_size );
  460. do_trunc = 2;
  461. }
  462. else if (!file->offset) {
  463. printf( " Truncating first to %llu bytes because second "
  464. "is FAT32 root dir.\n", (unsigned long long)clusters*fs->cluster_size );
  465. do_trunc = 1;
  466. }
  467. else if (interactive)
  468. printf("1) Truncate first to %llu bytes%s\n"
  469. "2) Truncate second to %llu bytes\n",(unsigned long long)clusters*fs->cluster_size,
  470. restart ? " and restart" : "",(unsigned long long)clusters2*fs->cluster_size);
  471. else printf(" Truncating second to %llu bytes.\n",(unsigned long long)clusters2*
  472. fs->cluster_size);
  473. if (do_trunc != 2 &&
  474. (do_trunc == 1 ||
  475. (interactive && get_key("12","?") == '1'))) {
  476. prev = 0;
  477. clusters = 0;
  478. for (this = FSTART(owner,fs); this > 0 && this != -1; this =
  479. next_cluster(fs,this)) {
  480. if (this == curr) {
  481. if (prev) set_fat(fs,prev,-1);
  482. else MODIFY_START(owner,0,fs);
  483. MODIFY(owner,size,CT_LE_L((unsigned long long)clusters*fs->cluster_size));
  484. if (restart) return 1;
  485. while (this > 0 && this != -1) {
  486. set_owner(fs,this,NULL);
  487. this = next_cluster(fs,this);
  488. }
  489. this = curr;
  490. break;
  491. }
  492. clusters++;
  493. prev = this;
  494. }
  495. if (this != curr)
  496. die("Internal error: didn't find cluster %d in chain"
  497. " starting at %d",curr,FSTART(owner,fs));
  498. }
  499. else {
  500. if (prev) set_fat(fs,prev,-1);
  501. else MODIFY_START(file,0,fs);
  502. break;
  503. }
  504. }
  505. set_owner(fs,curr,file);
  506. clusters++;
  507. prev = curr;
  508. }
  509. if (!(file->dir_ent.attr & ATTR_DIR) && CF_LE_L(file->dir_ent.size) >
  510. (unsigned long long)clusters*fs->cluster_size) {
  511. printf("%s\n File size is %u bytes, cluster chain length is %llu bytes."
  512. "\n Truncating file to %lu bytes.\n",path_name(file),CF_LE_L(file->
  513. dir_ent.size),(unsigned long long)clusters*fs->cluster_size,(unsigned long long)clusters*fs->cluster_size);
  514. MODIFY(file,size,CT_LE_L((unsigned long long)clusters*fs->cluster_size));
  515. }
  516. return 0;
  517. }
  518. static int check_files(DOS_FS *fs,DOS_FILE *start)
  519. {
  520. while (start) {
  521. if (check_file(fs,start)) return 1;
  522. start = start->next;
  523. }
  524. return 0;
  525. }
  526. static int check_dir(DOS_FS *fs,DOS_FILE **root,int dots)
  527. {
  528. DOS_FILE *parent,**walk,**scan;
  529. int dot,dotdot,skip,redo;
  530. int good,bad;
  531. if (!*root) return 0;
  532. parent = (*root)->parent;
  533. good = bad = 0;
  534. for (walk = root; *walk; walk = &(*walk)->next)
  535. if (bad_name(*walk)) bad++;
  536. else good++;
  537. if (*root && parent && good+bad > 4 && bad > good/2) {
  538. printf("%s\n Has a large number of bad entries. (%d/%d)\n",
  539. path_name(parent),bad,good+bad);
  540. if (!dots) printf( " Not dropping root directory.\n" );
  541. else if (!interactive) printf(" Not dropping it in auto-mode.\n");
  542. else if (get_key("yn","Drop directory ? (y/n)") == 'y') {
  543. truncate_file(fs,parent,0);
  544. MODIFY(parent,name[0],DELETED_FLAG);
  545. /* buglet: deleted directory stays in the list. */
  546. return 1;
  547. }
  548. }
  549. dot = dotdot = redo = 0;
  550. walk = root;
  551. while (*walk) {
  552. if (!strncmp((*walk)->dir_ent.name,MSDOS_DOT,MSDOS_NAME) ||
  553. !strncmp((*walk)->dir_ent.name,MSDOS_DOTDOT,MSDOS_NAME)) {
  554. if (handle_dot(fs,*walk,dots)) {
  555. *walk = (*walk)->next;
  556. continue;
  557. }
  558. if (!strncmp((*walk)->dir_ent.name,MSDOS_DOT,MSDOS_NAME)) dot++;
  559. else dotdot++;
  560. }
  561. if (!((*walk)->dir_ent.attr & ATTR_VOLUME) &&
  562. bad_name(*walk)) {
  563. puts(path_name(*walk));
  564. printf(" Bad short file name (%s).\n", file_name((*walk)->dir_ent.name));
  565. if (interactive)
  566. printf("1) Drop file\n2) Rename file\n3) Auto-rename\n"
  567. "4) Keep it\n");
  568. else printf(" Auto-renaming it.\n");
  569. switch (interactive ? get_key("1234","?") : '3') {
  570. case '1':
  571. drop_file(fs,*walk);
  572. walk = &(*walk)->next;
  573. continue;
  574. case '2':
  575. rename_file(*walk);
  576. redo = 1;
  577. break;
  578. case '3':
  579. auto_rename(*walk);
  580. printf(" Renamed to %s\n",file_name((*walk)->dir_ent.
  581. name));
  582. break;
  583. case '4':
  584. break;
  585. }
  586. }
  587. /* don't check for duplicates of the volume label */
  588. if (!((*walk)->dir_ent.attr & ATTR_VOLUME)) {
  589. scan = &(*walk)->next;
  590. skip = 0;
  591. while (*scan && !skip) {
  592. if (!((*scan)->dir_ent.attr & ATTR_VOLUME) &&
  593. !memcmp((*walk)->dir_ent.name,(*scan)->dir_ent.name,MSDOS_NAME)) {
  594. printf("%s\n Duplicate directory entry.\n First %s\n",
  595. path_name(*walk),file_stat(*walk));
  596. printf(" Second %s\n",file_stat(*scan));
  597. if (interactive)
  598. printf("1) Drop first\n2) Drop second\n3) Rename first\n"
  599. "4) Rename second\n5) Auto-rename first\n"
  600. "6) Auto-rename second\n");
  601. else printf(" Auto-renaming second.\n");
  602. switch (interactive ? get_key("123456","?") : '6') {
  603. case '1':
  604. drop_file(fs,*walk);
  605. *walk = (*walk)->next;
  606. skip = 1;
  607. break;
  608. case '2':
  609. drop_file(fs,*scan);
  610. *scan = (*scan)->next;
  611. continue;
  612. case '3':
  613. rename_file(*walk);
  614. printf(" Renamed to %s\n",path_name(*walk));
  615. redo = 1;
  616. break;
  617. case '4':
  618. rename_file(*scan);
  619. printf(" Renamed to %s\n",path_name(*walk));
  620. redo = 1;
  621. break;
  622. case '5':
  623. auto_rename(*walk);
  624. printf(" Renamed to %s\n",file_name((*walk)->dir_ent.
  625. name));
  626. break;
  627. case '6':
  628. auto_rename(*scan);
  629. printf(" Renamed to %s\n",file_name((*scan)->dir_ent.
  630. name));
  631. break;
  632. }
  633. }
  634. scan = &(*scan)->next;
  635. }
  636. if (skip) continue;
  637. }
  638. if (!redo) walk = &(*walk)->next;
  639. else {
  640. walk = root;
  641. dot = dotdot = redo = 0;
  642. }
  643. }
  644. if (dots && !dot)
  645. printf("%s\n \".\" is missing. Can't fix this yet.\n",
  646. path_name(parent));
  647. if (dots && !dotdot)
  648. printf("%s\n \"..\" is missing. Can't fix this yet.\n",
  649. path_name(parent));
  650. return 0;
  651. }
  652. /**
  653. * Check a dentry's cluster chain for bad clusters.
  654. * If requested, we verify readability and mark unreadable clusters as bad.
  655. *
  656. * @param[inout] fs Information about the filesystem
  657. * @param[in] file dentry to check
  658. * @param[in] read_test Nonzero == verify that dentry's clusters can
  659. * be read
  660. */
  661. static void test_file(DOS_FS *fs,DOS_FILE *file,int read_test)
  662. {
  663. DOS_FILE *owner;
  664. unsigned long walk,prev,clusters,next_clu;
  665. prev = clusters = 0;
  666. for (walk = FSTART(file,fs); walk > 0 && walk < fs->clusters+2;
  667. walk = next_clu) {
  668. next_clu = next_cluster(fs,walk);
  669. /* In this stage we are checking only for a loop within our own
  670. * cluster chain.
  671. * Cross-linking of clusters is handled in check_file()
  672. */
  673. if ((owner = get_owner(fs,walk))) {
  674. if (owner == file) {
  675. printf("%s\n Circular cluster chain. Truncating to %lu "
  676. "cluster%s.\n",path_name(file),clusters,clusters == 1 ? "" :
  677. "s");
  678. if (prev) set_fat(fs,prev,-1);
  679. else if (!file->offset)
  680. die( "Bad FAT32 root directory! (bad start cluster)\n" );
  681. else MODIFY_START(file,0,fs);
  682. }
  683. break;
  684. }
  685. if (bad_cluster(fs,walk)) break;
  686. if (read_test) {
  687. if (fs_test(cluster_start(fs,walk),fs->cluster_size)) {
  688. prev = walk;
  689. clusters++;
  690. }
  691. else {
  692. printf("%s\n Cluster %lu (%lu) is unreadable. Skipping it.\n",
  693. path_name(file),clusters,walk);
  694. if (prev) set_fat(fs,prev,next_cluster(fs,walk));
  695. else MODIFY_START(file,next_cluster(fs,walk),fs);
  696. set_fat(fs,walk,-2);
  697. }
  698. }
  699. set_owner(fs,walk,file);
  700. }
  701. /* Revert ownership (for now) */
  702. for (walk = FSTART(file,fs); walk > 0 && walk < fs->clusters+2;
  703. walk = next_cluster(fs,walk))
  704. if (bad_cluster(fs,walk)) break;
  705. else if (get_owner(fs,walk) == file) set_owner(fs,walk,NULL);
  706. else break;
  707. }
  708. static void undelete(DOS_FS *fs,DOS_FILE *file)
  709. {
  710. unsigned long clusters,left,prev,walk;
  711. clusters = left = (CF_LE_L(file->dir_ent.size)+fs->cluster_size-1)/
  712. fs->cluster_size;
  713. prev = 0;
  714. walk = FSTART(file,fs);
  715. while (left && (walk >= 2) && (walk < fs->clusters+2)) {
  716. FAT_ENTRY curEntry;
  717. get_fat(&curEntry, fs->fat, walk, fs);
  718. if (!curEntry.value)
  719. break;
  720. left--;
  721. if (prev) set_fat(fs,prev,walk);
  722. prev = walk;
  723. walk++;
  724. }
  725. if (prev) set_fat(fs,prev,-1);
  726. else MODIFY_START(file,0,fs);
  727. if (left)
  728. printf("Warning: Did only undelete %lu of %lu cluster%s.\n",clusters-left,
  729. clusters,clusters == 1 ? "" : "s");
  730. }
  731. static void new_dir( void )
  732. {
  733. lfn_reset();
  734. }
  735. /**
  736. * Create a description for a referenced dentry and insert it in our dentry
  737. * tree. Then, go check the dentry's cluster chain for bad clusters and
  738. * cluster loops.
  739. *
  740. * @param[inout] fs Information about the filesystem
  741. * @param[out] chain
  742. * @param[in] parent Information about parent directory of this file
  743. * NULL == no parent ('file' is root directory)
  744. * @param[in] offset Partition-relative byte offset of directory entry of interest
  745. * 0 == Root directory
  746. * @param cp
  747. */
  748. static void add_file(DOS_FS *fs,DOS_FILE ***chain,DOS_FILE *parent,
  749. loff_t offset,FDSC **cp)
  750. {
  751. DOS_FILE *new;
  752. DIR_ENT de;
  753. FD_TYPE type;
  754. if (offset)
  755. fs_read(offset,sizeof(DIR_ENT),&de);
  756. else {
  757. /* Construct a DIR_ENT for the root directory */
  758. memcpy(de.name," ",MSDOS_NAME);
  759. de.attr = ATTR_DIR;
  760. de.size = de.time = de.date = 0;
  761. de.start = CT_LE_W(fs->root_cluster & 0xffff);
  762. de.starthi = CT_LE_W((fs->root_cluster >> 16) & 0xffff);
  763. }
  764. if ((type = file_type(cp,de.name)) != fdt_none) {
  765. if (type == fdt_undelete && (de.attr & ATTR_DIR))
  766. die("Can't undelete directories.");
  767. file_modify(cp,de.name);
  768. fs_write(offset,1,&de);
  769. }
  770. if (IS_FREE(de.name)) {
  771. lfn_check_orphaned();
  772. return;
  773. }
  774. if (de.attr == VFAT_LN_ATTR) {
  775. lfn_add_slot(&de,offset);
  776. return;
  777. }
  778. new = qalloc(&mem_queue,sizeof(DOS_FILE));
  779. new->lfn = lfn_get(&de);
  780. new->offset = offset;
  781. memcpy(&new->dir_ent,&de,sizeof(de));
  782. new->next = new->first = NULL;
  783. new->parent = parent;
  784. if (type == fdt_undelete) undelete(fs,new);
  785. **chain = new;
  786. *chain = &new->next;
  787. if (list) {
  788. printf("Checking file %s",path_name(new));
  789. if (new->lfn)
  790. printf(" (%s)", file_name(new->dir_ent.name) ); /* (8.3) */
  791. printf("\n");
  792. }
  793. /* Don't include root directory, '.', or '..' in the total file count */
  794. if (offset &&
  795. strncmp(de.name,MSDOS_DOT,MSDOS_NAME) != 0 &&
  796. strncmp(de.name,MSDOS_DOTDOT,MSDOS_NAME) != 0)
  797. ++n_files;
  798. test_file(fs,new,test); /* Bad cluster check */
  799. }
  800. static int subdirs(DOS_FS *fs,DOS_FILE *parent,FDSC **cp);
  801. static int scan_dir(DOS_FS *fs,DOS_FILE *this,FDSC **cp)
  802. {
  803. DOS_FILE **chain;
  804. int i;
  805. unsigned long clu_num;
  806. chain = &this->first;
  807. i = 0;
  808. clu_num = FSTART(this,fs);
  809. new_dir();
  810. while (clu_num > 0 && clu_num != -1) {
  811. add_file(fs,&chain,this,cluster_start(fs,clu_num)+(i % fs->
  812. cluster_size),cp);
  813. i += sizeof(DIR_ENT);
  814. if (!(i % fs->cluster_size))
  815. if ((clu_num = next_cluster(fs,clu_num)) == 0 || clu_num == -1)
  816. break;
  817. }
  818. lfn_check_orphaned();
  819. if (check_dir(fs,&this->first,this->offset)) return 0;
  820. if (check_files(fs,this->first)) return 1;
  821. return subdirs(fs,this,cp);
  822. }
  823. /**
  824. * Recursively scan subdirectories of the specified parent directory.
  825. *
  826. * @param[inout] fs Information about the filesystem
  827. * @param[in] parent Identifies the directory to scan
  828. * @param[in] cp
  829. *
  830. * @return 0 Success
  831. * @return 1 Error
  832. */
  833. static int subdirs(DOS_FS *fs,DOS_FILE *parent,FDSC **cp)
  834. {
  835. DOS_FILE *walk;
  836. for (walk = parent ? parent->first : root; walk; walk = walk->next)
  837. if (walk->dir_ent.attr & ATTR_DIR)
  838. if (strncmp(walk->dir_ent.name,MSDOS_DOT,MSDOS_NAME) &&
  839. strncmp(walk->dir_ent.name,MSDOS_DOTDOT,MSDOS_NAME))
  840. if (scan_dir(fs,walk,file_cd(cp,walk->dir_ent.name))) return 1;
  841. return 0;
  842. }
  843. /**
  844. * Scan all directory and file information for errors.
  845. *
  846. * @param[inout] fs Information about the filesystem
  847. *
  848. * @return 0 Success
  849. * @return 1 Error
  850. */
  851. int scan_root(DOS_FS *fs)
  852. {
  853. DOS_FILE **chain;
  854. int i;
  855. root = NULL;
  856. chain = &root;
  857. new_dir();
  858. if (fs->root_cluster) {
  859. add_file(fs,&chain,NULL,0,&fp_root);
  860. }
  861. else {
  862. for (i = 0; i < fs->root_entries; i++)
  863. add_file(fs,&chain,NULL,fs->root_start+i*sizeof(DIR_ENT),&fp_root);
  864. }
  865. lfn_check_orphaned();
  866. (void) check_dir(fs,&root,0);
  867. if (check_files(fs,root)) return 1;
  868. return subdirs(fs,NULL,&fp_root);
  869. }