[9] | 1 | /*
|
---|
| 2 | * mmd.c
|
---|
| 3 | * Makes an MSDOS directory
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | #define LOWERCASE
|
---|
| 8 |
|
---|
| 9 | #include "sysincludes.h"
|
---|
| 10 | #include "msdos.h"
|
---|
| 11 | #include "mtools.h"
|
---|
| 12 | #include "vfat.h"
|
---|
| 13 | #include "mainloop.h"
|
---|
| 14 | #include "plain_io.h"
|
---|
| 15 | #include "nameclash.h"
|
---|
| 16 | #include "file.h"
|
---|
| 17 | #include "fs.h"
|
---|
| 18 |
|
---|
| 19 | /*
|
---|
| 20 | * Preserve the file modification times after the fclose()
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | typedef struct Arg_t {
|
---|
| 24 | char *target;
|
---|
| 25 | MainParam_t mp;
|
---|
| 26 |
|
---|
| 27 | Stream_t *SrcDir;
|
---|
| 28 | int entry;
|
---|
| 29 | ClashHandling_t ch;
|
---|
| 30 | Stream_t *targetDir;
|
---|
| 31 | } Arg_t;
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | typedef struct CreateArg_t {
|
---|
| 35 | Stream_t *Dir;
|
---|
| 36 | Stream_t *NewDir;
|
---|
| 37 | unsigned char attr;
|
---|
| 38 | time_t mtime;
|
---|
| 39 | } CreateArg_t;
|
---|
| 40 |
|
---|
| 41 | /*
|
---|
| 42 | * Open the named file for read, create the cluster chain, return the
|
---|
| 43 | * directory structure or NULL on error.
|
---|
| 44 | */
|
---|
| 45 | int makeit(char *dosname,
|
---|
| 46 | char *longname,
|
---|
| 47 | void *arg0,
|
---|
| 48 | direntry_t *targetEntry)
|
---|
| 49 | {
|
---|
| 50 | Stream_t *Target;
|
---|
| 51 | CreateArg_t *arg = (CreateArg_t *) arg0;
|
---|
| 52 | int fat;
|
---|
| 53 | direntry_t subEntry;
|
---|
| 54 |
|
---|
| 55 | /* will it fit? At least one cluster must be free */
|
---|
| 56 | if (!getfreeMinClusters(targetEntry->Dir, 1))
|
---|
| 57 | return -1;
|
---|
| 58 |
|
---|
| 59 | mk_entry(dosname, ATTR_DIR, 1, 0, arg->mtime, &targetEntry->dir);
|
---|
| 60 | Target = OpenFileByDirentry(targetEntry);
|
---|
| 61 | if(!Target){
|
---|
| 62 | fprintf(stderr,"Could not open Target\n");
|
---|
| 63 | return -1;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /* this allocates the first cluster for our directory */
|
---|
| 67 |
|
---|
| 68 | initializeDirentry(&subEntry, Target);
|
---|
| 69 |
|
---|
| 70 | subEntry.entry = 1;
|
---|
| 71 | GET_DATA(targetEntry->Dir, 0, 0, 0, &fat);
|
---|
| 72 | if (fat == fat32RootCluster(targetEntry->Dir)) {
|
---|
| 73 | fat = 0;
|
---|
| 74 | }
|
---|
| 75 | mk_entry(".. ", ATTR_DIR, fat, 0, arg->mtime, &subEntry.dir);
|
---|
| 76 | dir_write(&subEntry);
|
---|
| 77 |
|
---|
| 78 | FLUSH((Stream_t *) Target);
|
---|
| 79 | subEntry.entry = 0;
|
---|
| 80 | GET_DATA(Target, 0, 0, 0, &fat);
|
---|
| 81 | mk_entry(". ", ATTR_DIR, fat, 0, arg->mtime, &subEntry.dir);
|
---|
| 82 | dir_write(&subEntry);
|
---|
| 83 |
|
---|
| 84 | mk_entry(dosname, ATTR_DIR | arg->attr, fat, 0, arg->mtime,
|
---|
| 85 | &targetEntry->dir);
|
---|
| 86 | arg->NewDir = Target;
|
---|
| 87 | return 0;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | static void usage(void)
|
---|
| 92 | {
|
---|
| 93 | fprintf(stderr,
|
---|
| 94 | "Mtools version %s, dated %s\n", mversion, mdate);
|
---|
| 95 | fprintf(stderr,
|
---|
| 96 | "Usage: %s [-D clash_option] file targetfile\n", progname);
|
---|
| 97 | fprintf(stderr,
|
---|
| 98 | " %s [-D clash_option] file [files...] target_directory\n",
|
---|
| 99 | progname);
|
---|
| 100 | exit(1);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | Stream_t *createDir(Stream_t *Dir, const char *filename, ClashHandling_t *ch,
|
---|
| 104 | unsigned char attr, time_t mtime)
|
---|
| 105 | {
|
---|
| 106 | CreateArg_t arg;
|
---|
| 107 | int ret;
|
---|
| 108 |
|
---|
| 109 | arg.Dir = Dir;
|
---|
| 110 | arg.attr = attr;
|
---|
| 111 | arg.mtime = mtime;
|
---|
| 112 |
|
---|
| 113 | if (!getfreeMinClusters(Dir, 1))
|
---|
| 114 | return NULL;
|
---|
| 115 |
|
---|
| 116 | ret = mwrite_one(Dir, filename,0, makeit, &arg, ch);
|
---|
| 117 | if(ret < 1)
|
---|
| 118 | return NULL;
|
---|
| 119 | else
|
---|
| 120 | return arg.NewDir;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | static int createDirCallback(direntry_t *entry, MainParam_t *mp)
|
---|
| 124 | {
|
---|
| 125 | Stream_t *ret;
|
---|
| 126 | time_t now;
|
---|
| 127 |
|
---|
| 128 | ret = createDir(mp->File, mp->targetName, &((Arg_t *)(mp->arg))->ch,
|
---|
| 129 | ATTR_DIR, getTimeNow(&now));
|
---|
| 130 | if(ret == NULL)
|
---|
| 131 | return ERROR_ONE;
|
---|
| 132 | else {
|
---|
| 133 | FREE(&ret);
|
---|
| 134 | return GOT_ONE;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void mmd(int argc, char **argv, int type)
|
---|
| 140 | {
|
---|
| 141 | Arg_t arg;
|
---|
| 142 | int c;
|
---|
| 143 |
|
---|
| 144 | /* get command line options */
|
---|
| 145 |
|
---|
| 146 | init_clash_handling(& arg.ch);
|
---|
| 147 |
|
---|
| 148 | /* get command line options */
|
---|
| 149 | while ((c = getopt(argc, argv, "D:o")) != EOF) {
|
---|
| 150 | switch (c) {
|
---|
| 151 | case '?':
|
---|
| 152 | usage();
|
---|
| 153 | case 'o':
|
---|
| 154 | handle_clash_options(&arg.ch, c);
|
---|
| 155 | break;
|
---|
| 156 | case 'D':
|
---|
| 157 | if(handle_clash_options(&arg.ch, *optarg))
|
---|
| 158 | usage();
|
---|
| 159 | break;
|
---|
| 160 | default:
|
---|
| 161 | break;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | if (argc - optind < 1)
|
---|
| 166 | usage();
|
---|
| 167 |
|
---|
| 168 | init_mp(&arg.mp);
|
---|
| 169 | arg.mp.arg = (void *) &arg;
|
---|
| 170 | arg.mp.openflags = O_RDWR;
|
---|
| 171 | arg.mp.callback = createDirCallback;
|
---|
| 172 | arg.mp.lookupflags = OPEN_PARENT | DO_OPEN_DIRS;
|
---|
| 173 | exit(main_loop(&arg.mp, argv + optind, argc - optind));
|
---|
| 174 | }
|
---|