18d3397bedc28e49ad4d6749c0d603f71418b8fa
[gd/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26   return a file_id which gives a unique ID for a file given the device and
27   inode numbers
28  */
29 struct file_id file_id_create(SMB_DEV_T dev, SMB_INO_T inode)
30 {
31         struct file_id key;
32         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
33          * blob */
34         ZERO_STRUCT(key);
35         key.devid = dev;
36         key.inode = inode;
37         return key;
38 }
39
40 /*
41   generate a file_id from a stat structure
42  */
43 struct file_id file_id_sbuf(const SMB_STRUCT_STAT *sbuf)
44 {
45         return file_id_create(sbuf->st_dev, sbuf->st_ino);
46 }
47
48
49 /*
50   return True if two file_id structures are equal
51  */
52 BOOL file_id_equal(const struct file_id *id1, const struct file_id *id2)
53 {
54         return id1->inode == id2->inode && id1->devid == id2->devid;
55 }
56
57 /*
58   a static string for a file_id structure
59  */
60 const char *file_id_static_string(const struct file_id *id)
61 {
62         static char buf[32];
63         snprintf(buf, sizeof(buf), "%llx:%llx", 
64                  (unsigned long long)id->devid, 
65                  (unsigned long long)id->inode);
66         return buf;
67 }
68
69 /*
70   a 2nd static string for a file_id structure so we can print 2 at once
71  */
72 const char *file_id_static_string2(const struct file_id *id)
73 {
74         static char buf[32];
75         snprintf(buf, sizeof(buf), "%llx:%llx", 
76                  (unsigned long long)id->devid, 
77                  (unsigned long long)id->inode);
78         return buf;
79 }
80
81 /*
82   push a 16 byte version of a file id into a buffer
83  */
84 void push_file_id_16(char *buf, const struct file_id *id)
85 {
86         SIVAL(buf,  0, id->devid&0xFFFFFFFF);
87         SIVAL(buf,  4, id->devid>>32);
88         SIVAL(buf,  8, id->inode&0xFFFFFFFF);
89         SIVAL(buf, 12, id->inode>>32);
90 }
91
92 /*
93   pul a 16 byte version of a file id from a buffer
94  */
95 void pull_file_id_16(char *buf, struct file_id *id)
96 {
97         ZERO_STRUCTP(id);
98         id->devid  = IVAL(buf,  0);
99         id->devid |= ((uint64_t)IVAL(buf,4))<<32;
100         id->inode  = IVAL(buf,  8);
101         id->inode |= ((uint64_t)IVAL(buf,12))<<32;
102 }