Send back talloc allocation information when POOL_USAGE is received.
[samba.git] / source3 / lib / netatalk.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Copyright (C) Andrew Tridgell 1992-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22    netatalk.c : routines for improving interaction between Samba and netatalk.
23    Copyright (C) John D. Blair <jdblair@cobaltnet.com> 1998
24                  Cobalt Networks, Inc.
25 */
26
27 #include "includes.h"
28
29 #ifdef WITH_NETATALK
30  
31 /*****************
32    ntalk_resourcepath: creates the path to the netatalk resource fork for
33                         a given file
34
35    fname:       normal filename
36    doublename:  buffer that will contain the location of the resource fork
37    length:      length of this buffer (to prevent overflows)
38
39    NOTE: doesn't currently gracefully deal with buffer overflows- it just
40          doesn't allow them to occur.
41 ******************/
42 void ntalk_resourcepath(const char *fname, char *doublename, const int length)
43 {
44   int i;
45   int charnum;
46   int lastslash;
47   char appledouble[] = APPLEDOUBLE;
48
49   /* copy fname to doublename and find last slash */
50   for (i = 0; (fname[i] != 0) && (i <= length); i++) {
51     if (fname[i] == '/') {
52       lastslash = i;
53     }
54     doublename[i] = fname[i];
55   }
56   lastslash++; /* location just after last slash */
57
58   /* insert .AppleDouble */
59   charnum = lastslash;
60   for (i = 0; (appledouble[i] != 0) && (i <= length); i++) {
61     doublename[charnum] = appledouble[i];
62     charnum++;
63   }
64
65   /* append last part of file name */
66   for (i = lastslash; (fname[i] != 0) && (i <= length); i++) {
67     doublename[charnum] = fname[i];
68     charnum++;
69   }
70   
71   doublename[charnum] = 0;
72 }
73
74 /**********************
75  ntalk_mkresdir: creates a new .AppleDouble directory (if necessary)
76                  for the resource fork of a specified file
77 **********************/
78 int ntalk_mkresdir(const char *fname)
79 {
80   char fdir[255];
81   int i;
82   int lastslash;
83   SMB_STRUCT_STAT dirstats;
84   char appledouble[] = APPLEDOUBLE;
85
86   /* find directory containing fname */
87   for (i = 0; (fname[i] != 0) && (i <= 254); i++) {
88     fdir[i] = fname[i];
89     if (fdir[i] == '/') {
90       lastslash = i;
91     }
92   }
93   lastslash++;
94   fdir[lastslash] = 0;
95   sys_lstat(fdir, &dirstats);
96
97   /* append .AppleDouble */
98   for (i = 0; (appledouble[i] != 0) && (lastslash <= 254); i++) {
99     fdir[lastslash] = appledouble[i];
100     lastslash++;
101   }
102   fdir[lastslash] = 0;
103
104   /* create this directory */
105   /* silently ignore EEXIST error */
106   if ((mkdir(fdir, dirstats.st_mode) < 0) && (errno != EEXIST)) {
107     return errno;
108   }
109
110   /* set ownership of this dir to the same as its parent */
111   /* holy race condition, batman! */
112   /* warning: this doesn't check for errors */
113   chown(fdir, dirstats.st_uid, dirstats.st_gid);
114
115   printf("%s\n", fdir);
116
117   return 1;
118 }
119
120 /**********************
121  ntalk_unlink: unlink a file and its resource fork
122 **********************/
123 int ntalk_unlink(const char *fname)
124 {
125   char buf[255];
126
127   ntalk_resourcepath(fname, buf, 255);
128   unlink(buf);
129   return unlink(fname);
130 }
131
132 /**********************
133  ntalk_chown: chown a file and its resource fork
134 **********************/
135 int ntalk_chown(const char *fname, const uid_t uid, const gid_t gid)
136 {
137   char buf[255];
138
139   ntalk_resourcepath(fname, buf, 255);
140   chown(buf, uid, gid);
141   return chown(fname, uid, gid);
142 }
143
144 /**********************
145  ntalk_chmod: chmod a file and its resource fork
146 **********************/
147 int ntalk_chmod(const char *fname, mode_t perms)
148 {
149   char buf[255];
150
151   ntalk_resourcepath(fname, buf, 255);
152   chmod(buf, perms);
153   return chmod(fname, perms);
154 }
155
156 #endif /* WITH_NETATALK */