source: trunk/minix/commands/i386/mtools-3.9.7/directory.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.4 KB
RevLine 
[9]1#include "sysincludes.h"
2#include "msdos.h"
3#include "stream.h"
4#include "mtools.h"
5#include "file.h"
6#include "fs.h"
7
8/* #define DEBUG */
9
10/*
11 * Read a directory entry into caller supplied buffer
12 */
13struct directory *dir_read(direntry_t *entry, int *error)
14{
15 int n;
16 *error = 0;
17 if((n=force_read(entry->Dir, (char *) (&entry->dir),
18 (mt_off_t) entry->entry * MDIR_SIZE,
19 MDIR_SIZE)) != MDIR_SIZE) {
20 if (n < 0) {
21 *error = -1;
22 }
23 return NULL;
24 }
25 return &entry->dir;
26}
27
28/*
29 * Make a subdirectory grow in length. Only subdirectories (not root)
30 * may grow. Returns a 0 on success, 1 on failure (disk full), or -1
31 * on error.
32 */
33
34int dir_grow(Stream_t *Dir, int size)
35{
36 Stream_t *Stream = GetFs(Dir);
37 DeclareThis(FsPublic_t);
38 int ret;
39 int buflen;
40 char *buffer;
41
42 if (!getfreeMinClusters(Dir, 1))
43 return -1;
44
45 buflen = This->cluster_size * This->sector_size;
46
47 if(! (buffer=malloc(buflen)) ){
48 perror("dir_grow: malloc");
49 return -1;
50 }
51
52 memset((char *) buffer, '\0', buflen);
53 ret = force_write(Dir, buffer, (mt_off_t) size * MDIR_SIZE, buflen);
54 free(buffer);
55 if(ret < buflen)
56 return -1;
57 return 0;
58}
59
60
61void low_level_dir_write(direntry_t *entry)
62{
63 force_write(entry->Dir,
64 (char *) (&entry->dir),
65 (mt_off_t) entry->entry * MDIR_SIZE, MDIR_SIZE);
66}
67
68
69/*
70 * Make a directory entry. Builds a directory entry based on the
71 * name, attribute, starting cluster number, and size. Returns a pointer
72 * to a static directory structure.
73 */
74
75struct directory *mk_entry(const char *filename, char attr,
76 unsigned int fat, size_t size, time_t date,
77 struct directory *ndir)
78{
79 struct tm *now;
80 time_t date2 = date;
81 unsigned char hour, min_hi, min_low, sec;
82 unsigned char year, month_hi, month_low, day;
83
84 now = localtime(&date2);
85 strncpy(ndir->name, filename, 8);
86 strncpy(ndir->ext, filename + 8, 3);
87 ndir->attr = attr;
88 ndir->ctime_ms = 0;
89 hour = now->tm_hour << 3;
90 min_hi = now->tm_min >> 3;
91 min_low = now->tm_min << 5;
92 sec = now->tm_sec / 2;
93 ndir->ctime[1] = ndir->time[1] = hour + min_hi;
94 ndir->ctime[0] = ndir->time[0] = min_low + sec;
95 year = (now->tm_year - 80) << 1;
96 month_hi = (now->tm_mon + 1) >> 3;
97 month_low = (now->tm_mon + 1) << 5;
98 day = now->tm_mday;
99 ndir -> adate[1] = ndir->cdate[1] = ndir->date[1] = year + month_hi;
100 ndir -> adate[0] = ndir->cdate[0] = ndir->date[0] = month_low + day;
101
102 set_word(ndir->start, fat & 0xffff);
103 set_word(ndir->startHi, fat >> 16);
104 set_dword(ndir->size, size);
105 return ndir;
106}
Note: See TracBrowser for help on using the repository browser.