sys/vfs/smbfs/smbfs_subr.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | /* * Copyright (c) 2000-2001, Boris Popov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Boris Popov. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/fs/smbfs/smbfs_subr.c,v 1.1.2.2 2003/01/17 08:20:26 tjr Exp $ * $DragonFly: src/sys/vfs/smbfs/smbfs_subr.c,v 1.4 2003/08/07 21:54:36 dillon Exp $ */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <machine/clock.h> #include <sys/time.h> #include <sys/vnode.h> #include <sys/sysctl.h> #include <sys/iconv.h> #include <netproto/smb/smb.h> #include <netproto/smb/smb_conn.h> #include <netproto/smb/smb_subr.h> #include <netproto/smb/smb_rq.h> #include <netproto/smb/smb_dev.h> #include "smbfs.h" #include "smbfs_node.h" #include "smbfs_subr.h" MALLOC_DEFINE(M_SMBFSDATA, "SMBFS data", "SMBFS private data"); /* * Time & date conversion routines taken from msdosfs. Although leap * year calculation is bogus, it's sufficient before 2100 :) */ /* * This is the format of the contents of the deTime field in the direntry * structure. * We don't use bitfields because we don't know how compilers for * arbitrary machines will lay them out. */ #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */ #define DT_2SECONDS_SHIFT 0 #define DT_MINUTES_MASK 0x7E0 /* minutes */ #define DT_MINUTES_SHIFT 5 #define DT_HOURS_MASK 0xF800 /* hours */ #define DT_HOURS_SHIFT 11 /* * This is the format of the contents of the deDate field in the direntry * structure. */ #define DD_DAY_MASK 0x1F /* day of month */ #define DD_DAY_SHIFT 0 #define DD_MONTH_MASK 0x1E0 /* month */ #define DD_MONTH_SHIFT 5 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */ #define DD_YEAR_SHIFT 9 /* * Total number of days that have passed for each month in a regular year. */ static u_short regyear[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; /* * Total number of days that have passed for each month in a leap year. */ static u_short leapyear[] = { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; /* * Variables used to remember parts of the last time conversion. Maybe we * can avoid a full conversion. */ static u_long lasttime; static u_long lastday; static u_short lastddate; static u_short lastdtime; void smb_time_local2server(struct timespec *tsp, int tzoff, u_long *seconds) { *seconds = tsp->tv_sec - tzoff * 60 /*- tz.tz_minuteswest * 60 - (wall_cmos_clock ? adjkerntz : 0)*/; } void smb_time_server2local(u_long seconds, int tzoff, struct timespec *tsp) { tsp->tv_sec = seconds + tzoff * 60; /*+ tz.tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0)*/; } /* * Number of seconds between 1970 and 1601 year */ int64_t DIFF1970TO1601 = 11644473600ULL; /* * Time from server comes as UTC, so no need to use tz */ void smb_time_NT2local(int64_t nsec, int tzoff, struct timespec *tsp) { smb_time_server2local(nsec / 10000000 - DIFF1970TO1601, 0, tsp); } void smb_time_local2NT(struct timespec *tsp, int tzoff, int64_t *nsec) { u_long seconds; smb_time_local2server(tsp, 0, &seconds); *nsec = (((int64_t)(seconds) & ~1) + DIFF1970TO1601) * (int64_t)10000000; } void smb_time_unix2dos(struct timespec *tsp, int tzoff, u_int16_t *ddp, u_int16_t *dtp, u_int8_t *dhp) { u_long t, days, year, month, inc; u_short *months; /* * If the time from the last conversion is the same as now, then * skip the computations and use the saved result. */ smb_time_local2server(tsp, tzoff, &t); t &= ~1; if (lasttime != t) { lasttime = t; lastdtime = (((t / 2) % 30) << DT_2SECONDS_SHIFT) + (((t / 60) % 60) << DT_MINUTES_SHIFT) + (((t / 3600) % 24) << DT_HOURS_SHIFT); /* * If the number of days since 1970 is the same as the last * time we did the computation then skip all this leap year * and month stuff. */ days = t / (24 * 60 * 60); if (days != lastday) { lastday = days; for (year = 1970;; year++) { inc = year & 0x03 ? 365 : 366; if (days < inc) break; days -= inc; } months = year & 0x03 ? regyear : leapyear; for (month = 0; days >= months[month]; month++) ; if (month > 0) days -= months[month - 1]; lastddate = ((days + 1) << DD_DAY_SHIFT) + ((month + 1) << DD_MONTH_SHIFT); /* * Remember dos's idea of time is relative to 1980. * unix's is relative to 1970. If somehow we get a * time before 1980 then don't give totally crazy * results. */ if (year > 1980) lastddate += (year - 1980) << DD_YEAR_SHIFT; } } if (dtp) *dtp = lastdtime; if (dhp) *dhp = (tsp->tv_sec & 1) * 100 + tsp->tv_nsec / 10000000; *ddp = lastddate; } /* * The number of seconds between Jan 1, 1970 and Jan 1, 1980. In that * interval there were 8 regular years and 2 leap years. */ #define SECONDSTO1980 (((8 * 365) + (2 * 366)) * (24 * 60 * 60)) static u_short lastdosdate; static u_long lastseconds; void smb_dos2unixtime(u_int dd, u_int dt, u_int dh, int tzoff, struct timespec *tsp) { u_long seconds; u_long month; u_long year; u_long days; u_short *months; if (dd == 0) { tsp->tv_sec = 0; tsp->tv_nsec = 0; return; } seconds = (((dt & DT_2SECONDS_MASK) >> DT_2SECONDS_SHIFT) << 1) + ((dt & DT_MINUTES_MASK) >> DT_MINUTES_SHIFT) * 60 + ((dt & DT_HOURS_MASK) >> DT_HOURS_SHIFT) * 3600 + dh / 100; /* * If the year, month, and day from the last conversion are the * same then use the saved value. */ if (lastdosdate != dd) { lastdosdate = dd; days = 0; year = (dd & DD_YEAR_MASK) >> DD_YEAR_SHIFT; days = year * 365; days += year / 4 + 1; /* add in leap days */ if ((year & 0x03) == 0) days--; /* if year is a leap year */ months = year & 0x03 ? regyear : leapyear; month = (dd & DD_MONTH_MASK) >> DD_MONTH_SHIFT; if (month < 1 || month > 12) { month = 1; } if (month > 1) days += months[month - 2]; days += ((dd & DD_DAY_MASK) >> DD_DAY_SHIFT) - 1; lastseconds = (days * 24 * 60 * 60) + SECONDSTO1980; } smb_time_server2local(seconds + lastseconds, tzoff, tsp); tsp->tv_nsec = (dh % 100) * 10000000; } static int smb_fphelp(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *np, int caseopt) { struct smbmount *smp= np->n_mount; struct smbnode **npp = smp->sm_npstack; int i, error = 0; /* simple_lock(&smp->sm_npslock);*/ i = 0; while (np->n_parent) { if (i++ == SMBFS_MAXPATHCOMP) { /* simple_unlock(&smp->sm_npslock);*/ return ENAMETOOLONG; } *npp++ = np; np = VTOSMB(np->n_parent); } /* if (i == 0) return smb_put_dmem(mbp, vcp, "\\", 2, caseopt);*/ while (i--) { np = *--npp; error = mb_put_uint8(mbp, '\\'); if (error) break; error = smb_put_dmem(mbp, vcp, np->n_name, np->n_nmlen, caseopt); if (error) break; } /* simple_unlock(&smp->sm_npslock);*/ return error; } int smbfs_fullpath(struct mbchain *mbp, struct smb_vc *vcp, struct smbnode *dnp, const char *name, int nmlen) { int caseopt = SMB_CS_NONE; int error; if (SMB_DIALECT(vcp) < SMB_DIALECT_LANMAN1_0) caseopt |= SMB_CS_UPPER; if (dnp != NULL) { error = smb_fphelp(mbp, vcp, dnp, caseopt); if (error) return error; } if (name) { error = mb_put_uint8(mbp, '\\'); if (error) return error; error = smb_put_dmem(mbp, vcp, name, nmlen, caseopt); if (error) return error; } error = mb_put_uint8(mbp, 0); return error; } int smbfs_fname_tolocal(struct smb_vc *vcp, char *name, int nmlen, int caseopt) { /* if (caseopt & SMB_CS_UPPER) iconv_convmem(vcp->vc_toupper, name, name, nmlen); else if (caseopt & SMB_CS_LOWER) iconv_convmem(vcp->vc_tolower, name, name, nmlen);*/ if (vcp->vc_tolocal) iconv_convmem(vcp->vc_tolocal, name, name, nmlen); return 0; } |