a92cca08fa5f06da5d9d51c292bf9d528d35dab9
[samba.git] / source3 / lib / file_id.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    file_id structure handling
5
6    Copyright (C) Andrew Tridgell 2007
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 /*
25   return a file_id which gives a unique ID for a file given the device and
26   inode numbers
27  */
28 struct file_id file_id_create_dev(SMB_DEV_T dev, SMB_INO_T inode)
29 {
30         struct file_id key;
31         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
32          * blob */
33         ZERO_STRUCT(key);
34         key.devid = dev;
35         key.inode = inode;
36         return key;
37 }
38
39 /*
40   generate a file_id from a stat structure
41  */
42 struct file_id vfs_file_id_from_sbuf(connection_struct *conn, const SMB_STRUCT_STAT *sbuf)
43 {
44         return SMB_VFS_FILE_ID_CREATE(conn, sbuf->st_dev, sbuf->st_ino);
45 }
46
47 /*
48   return True if two file_id structures are equal
49  */
50 BOOL file_id_equal(const struct file_id *id1, const struct file_id *id2)
51 {
52         return id1->inode == id2->inode && id1->devid == id2->devid;
53 }
54
55 /*
56   a static string for a file_id structure
57  */
58 const char *file_id_static_string(const struct file_id *id)
59 {
60         static char buf[32];
61         snprintf(buf, sizeof(buf), "%llx:%llx", 
62                  (unsigned long long)id->devid, 
63                  (unsigned long long)id->inode);
64         return buf;
65 }
66
67 /*
68   a 2nd static string for a file_id structure so we can print 2 at once
69  */
70 const char *file_id_static_string2(const struct file_id *id)
71 {
72         static char buf[32];
73         snprintf(buf, sizeof(buf), "%llx:%llx", 
74                  (unsigned long long)id->devid, 
75                  (unsigned long long)id->inode);
76         return buf;
77 }
78
79 /*
80   push a 16 byte version of a file id into a buffer
81  */
82 void push_file_id_16(char *buf, const struct file_id *id)
83 {
84         SIVAL(buf,  0, id->devid&0xFFFFFFFF);
85         SIVAL(buf,  4, id->devid>>32);
86         SIVAL(buf,  8, id->inode&0xFFFFFFFF);
87         SIVAL(buf, 12, id->inode>>32);
88 }
89
90 /*
91   pul a 16 byte version of a file id from a buffer
92  */
93 void pull_file_id_16(char *buf, struct file_id *id)
94 {
95         ZERO_STRUCTP(id);
96         id->devid  = IVAL(buf,  0);
97         id->devid |= ((uint64_t)IVAL(buf,4))<<32;
98         id->inode  = IVAL(buf,  8);
99         id->inode |= ((uint64_t)IVAL(buf,12))<<32;
100 }