1 | /* create an entry for the file name.ext in parent directory */
|
2 | WorkingDirectory *create_file_entry(const char *name, const char *ext,
|
3 | WorkingDirectory *parent, BYTE attributes)
|
4 | {
|
5 | BYTE buf[SECTORSIZE];
|
6 | WorkingDirectory *wd;
|
7 | int de;
|
8 | UINT32 firstcluster;
|
9 | UINT32 filesize;
|
10 | UINT32 de_sector;
|
11 | UINT32 de_offset;
|
12 | DirectoryEntry dir_entry;
|
13 | PartitionInfo *pi = parent->pi;
|
14 |
|
15 | /* find a free directory entry */
|
16 | if ((de = find_in_directory(parent, NULL, NULL, &firstcluster,
|
17 | &filesize, &de_sector, &de_offset)) == 0)
|
18 | return 0; /* no free entries available */
|
19 | /*=================================================================
|
20 | GENAU HIER BEI "return 0;" SETZT ER DEN FEHLER (s.o.) AN
|
21 | ==================================================================
|
22 | */
|
23 |
|
24 | filesize = firstcluster = 0;
|
25 |
|
26 | #ifdef CONFIG_DIR_SUPPORT
|
27 | /* when creating a directory, two special directories (. & ..) within
|
28 | * that directory should always be created
|
29 | */
|
30 | if ((attributes & ATTR_DIRECTORY) == ATTR_DIRECTORY) {
|
31 | UINT32 sector, parent_cluster;
|
32 | int i;
|
33 |
|
34 | firstcluster = pi->op->allocate_one_cluster(pi);
|
35 | debug_xil_printf("firstcluster for dir: %ld\n", firstcluster);
|
36 | if (firstcluster == 0)
|
37 | return 0; /* no free space */
|
38 |
|
39 | /* zero out all sectors in the allotted cluster */
|
40 | memset(buf, 0, sizeof buf);
|
41 | sector = starting_sector(firstcluster, pi);
|
42 | debug_xil_printf("sectors starting from: %ld\n", sector);
|
43 | for (i = 1, sector++; /* the first sector needs to have dot, dotdot entries
|
44 | * and will be filled in later */
|
45 | i < pi->SectorsPerCluster;
|
46 | i++, sector++)
|
47 | if (write_sector(sector, buf) != SECTORSIZE)
|
48 | return 0; /* disk error */
|
49 |
|
50 | /* create dot & dotdot entries for the directory */
|
51 |
|
52 | /* the dot entry points to itself */
|
53 | initialize_de(dotdot_entry, ".", "", firstcluster, 0, pi, ATTR_DIRECTORY);
|
54 |
|
55 | /* the dotdot entry should point to the parent of the new directory */
|
56 | parent_cluster = parent->v.child.FirstCluster;
|
57 | if (parent->parent == NULL) /* root */
|
58 | parent_cluster = 0;
|
59 | debug_xil_printf("parent cluster = %ld\n", parent_cluster);
|
60 | initialize_de(dotdot_entry+1, "..", "", parent_cluster, 0, pi, ATTR_DIRECTORY);
|
61 |
|
62 | memcpy(buf, dotdot_entry, sizeof(DirectoryEntry)*2);
|
63 | sector = starting_sector(firstcluster, pi);
|
64 | if (write_sector(sector, buf) != SECTORSIZE)
|
65 | return 0;
|
66 | }
|
67 | #endif
|
68 | /*=================================================================
|
69 | UND HIER WIRD INITIALIZE_DE AUFGERUFEN
|
70 | ==================================================================
|
71 | */
|
72 | initialize_de(&dir_entry, (char *)name, (char *)ext, firstcluster, filesize,
|
73 | pi, attributes);
|
74 |
|
75 | /* allocate a wd structure to hold the working directory info*/
|
76 | if ((wd = malloc_wd(name)) == 0)
|
77 | return 0;
|
78 |
|
79 | /* 1. read in the whole sector */
|
80 | if (read_sector(de_sector, buf) != SECTORSIZE)
|
81 | return 0;
|
82 |
|
83 | /* 2. write the new directory entry */
|
84 | memcpy(buf + de_offset, (char*)&dir_entry, sizeof dir_entry);
|
85 |
|
86 | /* 3. write out the whole sector */
|
87 | if (write_sector(de_sector, buf) != SECTORSIZE)
|
88 | return 0;
|
89 |
|
90 | wd->v.child.FirstCluster = firstcluster;
|
91 | wd->v.child.FileSize = filesize;
|
92 | wd->v.child.DirLocation.sector = de_sector;
|
93 | wd->v.child.DirLocation.offset = de_offset;
|
94 | wd->pi = parent->pi;
|
95 | wd->parent = parent;
|
96 | return wd;
|
97 | }
|